Download presentation
Presentation is loading. Please wait.
Published byStella Williamson Modified over 9 years ago
1
Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se
2
Overview Inheritance Creating subclasses Interface Constructor Hierarchies Redefying methods
3
Inheritance
5
Reuse code! Common methods or attributes
6
Inheritance Object BooleanThrowable Exception IOException Dictionary HashTable Collection ArryListsVector AbstractList Serializable
7
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
8
Inheritance Object public String toString() public void finalize() throws Throwable
9
Inheritance Throwable public String getMessage() public void printStackTrace()
10
Inheritance Exception Constructors All other methods inherited!
11
Creating subclasses Key word: Extends Public class MyClass { } Public class mySubClass extends MyClass { }
12
Quiz Define inheritance in Java What key word is used in java to implement inheritance?
13
Creating subclasses All classes inherit directly or indirectly from Object Java allows only one superclass Class1 SubClass Class2
14
Interface Defines the methods Implements In the subclass
15
Interface interface MyInterface { void InterfaceMethod(); } public class MyClass implements MyInterface { void InterfaceMethod() { … } }
16
Interface public interface Collection extends Iterable boolean add(E e) Iterator iterator() boolean remove(Object o)
17
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.
18
Constructor public class MyClass { public MyClass() { } } public class MySubclass extends MyClass { public mySubclass() { super(); } }
19
Quiz How does java handle multiple inheritance? How does a subclass call the constructor of the class it inherits from?
20
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
21
Redefying methods Redefine a method Same name but different functions Overloading Overriding
22
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
23
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) {}; }
24
Redefying methods Override occurs in the subclass Can have same name, parameters and types Differs in implementation Occurs at run time
25
Redefying methods public class MyClass { public void myMethod() { /* do something */} } public class MySubclass extends MyClass { public void myMethod() () { /* do something else */} }
26
Redefying methods @Override Tells the compiler you intend to override a method Just in case you make a spelling error @Override public void myMethod()
27
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
28
Redefying methods public 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(); MyClassmyClass = mySubClass; mySubclass.myMethod(); // calls subclass mySubclass.myOtherMethod(); // calls subclass myClass.myMethod(); // calls super class myClass.myOtherMethod(); // calls subclass
29
Examples Component MenuComponent MenuContainer ImageObserverSerializable Button Canvas TextComponent
30
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
31
Examples public Interface ImageObserver boolean imageUpdate(…) Implemented in class Component
32
Examples RectangularShape ShapeCloneable Arc2DEllipse2DRectangular2D
33
Examples public interface Shape boolean Contains(…) Rectangle getBounds()
34
Examples public abstract class RectangularShape extends object implements Shape, Cloneable public Rectangle getBounds() Class Rectangle inherits from Rectangle2D public void setFrame(…)
35
Quiz Name two ways Java allows you to redefine methods What is the difference between hidden and override?
36
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.