Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng [email protected].
-
Upload
stella-williamson -
Category
Documents
-
view
225 -
download
5
Transcript of Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng [email protected].
![Page 2: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/2.jpg)
Overview
• Inheritance
• Creating subclasses
• Interface
• Constructor
• Hierarchies
• Redefying methods
![Page 3: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/3.jpg)
Inheritance
![Page 4: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/4.jpg)
Inheritance
![Page 5: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/5.jpg)
Inheritance
• Reuse code!
• Common methods or attributes
![Page 6: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/6.jpg)
InheritanceObject
Boolean Throwable
Exception
IOException
Dictionary
HashTable
Collection
ArryLists Vector
AbstractList
Serializable
![Page 7: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/7.jpg)
Inheritance
• Define common attributes in the higher classes
• Define common methods in the higher classes
• Inheritance• Derivation of attributes and method by one class from another
![Page 8: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/8.jpg)
Inheritance
• Object
• public String toString()
• public void finalize() throws Throwable
![Page 9: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/9.jpg)
Inheritance
• Throwable
• public String getMessage()
• public void printStackTrace()
![Page 10: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/10.jpg)
Inheritance
• Exception
• Constructors
• All other methods inherited!
![Page 11: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/11.jpg)
Creating subclasses
• Key word:• Extends
Public class MyClass
{
}
Public class mySubClass extends MyClass
{
}
![Page 12: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/12.jpg)
Quiz
• Define inheritance in Java
• What key word is used in java to implement inheritance?
![Page 13: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/13.jpg)
Creating subclasses
• All classes inherit directly or indirectly from Object
• Java allows only one superclass
Class1
SubClass
Class2
![Page 14: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/14.jpg)
Interface
• Interface• Defines the methods
• Implements• In the subclass
![Page 15: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/15.jpg)
Interface
interface MyInterface
{
void InterfaceMethod();
}
public class MyClass implements MyInterface
{
void InterfaceMethod()
{
…
}
}
![Page 16: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/16.jpg)
Interface
public interface Collection<E> extends Iterable
boolean add(E e)
Iterator<E> iterator()
boolean remove(Object o)
![Page 17: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/17.jpg)
Constructor
• Constructors are not inherited, even if public
• Key word• super
• Super is a call to the above class’ constructor
• Must be first line of code in the constructor.
![Page 18: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/18.jpg)
Constructorpublic class MyClass
{
public MyClass()
{
}
}
public class MySubclass extends MyClass
{
public mySubclass()
{
super();
}
}
![Page 19: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/19.jpg)
Quiz
• How does java handle multiple inheritance?
• How does a subclass call the constructor of the class it inherits from?
![Page 20: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/20.jpg)
Hierarchies
• Subclasses can have subclasses
• Common code higher up the hierarchies
• Add a few attributes / methods at a time
• Design / implantation is cyclic
• No one design fits all purposes design
implementation
![Page 21: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/21.jpg)
Redefying methods
• Redefine a method• Same name but different functions
• Overloading
• Overriding
![Page 22: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/22.jpg)
Redefying methods
• Overloading occurs in the same class
• Methods with the same name but …
• Different number of parameters or …
• Different type of parameters
• Happens at compile time
![Page 23: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/23.jpg)
Redefying methods
public class MyClass
{
public void myMethod() {};
public void myMethod(int nVal) {};
public void myMethod(float fVal) {};
public void myMethod(int nVal, float fVal) {};
}
![Page 24: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/24.jpg)
Redefying methods
• Override occurs in the subclass
• Can have same name, parameters and types
• Differs in implementation
• Occurs at run time
![Page 25: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/25.jpg)
Redefying methods
public class MyClass
{
public void myMethod() { /* do something */}
}
public class MySubclass extends MyClass
{
public void myMethod() () { /* do something else */}
}
![Page 26: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/26.jpg)
Redefying methods
• @Override
• Tells the compiler you intend to override a method
• Just in case you make a spelling error
@Override
public void myMethod()
![Page 27: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/27.jpg)
Redefying methods
• Super class has a static method
• Subclass overrides the superclass then …
• The subclass hides the superclass method
• Call to a hidden method calls the superclass or subclass method depending on reference
![Page 28: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/28.jpg)
Redefying methodspublic class MyClass
{
public static void myMethod(){};
public void myOtherMethod(){};
}
public class MySubclass extends MyCass
{
public static void myMethod(){};
public void myOtherMethod(){};
}
MySubClass mySubclass = new MySubclass();MyClass myClass = mySubClass;
mySubclass.myMethod(); // calls subclassmySubclass.myOtherMethod(); // calls subclass
myClass.myMethod(); // calls super classmyClass.myOtherMethod(); // calls subclass
![Page 29: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/29.jpg)
Examples
Component MenuComponent
MenuContainer ImageObserver Serializable
Button CanvasTextComponent
![Page 30: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/30.jpg)
Examples
• public abstract class Component extends Object implements ImageObserver, MenuContainer, Serializable
• public static float CENTER_ALLIGMENT
• public boolean action(Event evt, Object what)
• public void addComponentListener(ComponetListerner)
• Swing
![Page 31: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/31.jpg)
Examples
• public Interface ImageObserver
• boolean imageUpdate(…)
• Implemented in class Component
![Page 32: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/32.jpg)
Examples
RectangularShape
Shape Cloneable
Arc2D Ellipse2D Rectangular2D
![Page 33: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/33.jpg)
Examples
• public interface Shape
• boolean Contains(…)
• Rectangle getBounds()
![Page 34: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/34.jpg)
Examples
• public abstract class RectangularShape extends object implements Shape, Cloneable
• public Rectangle getBounds()• Class Rectangle inherits from Rectangle2D
• public void setFrame(…)
![Page 35: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/35.jpg)
Quiz
• Name two ways Java allows you to redefine methods
• What is the difference between hidden and override?
![Page 36: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se.](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bf7d1a28abf838c8473a/html5/thumbnails/36.jpg)
Questions?