Inheritance Dr Andrew Wallace Ph D BEnghons Eur
Inheritance Dr. Andrew Wallace Ph. D BEng(hons) Eur. Ing andrew@cs. umu. se
Overview • Inheritance • Creating subclasses • Interface • Constructor • Hierarchies • Redefying methods
Inheritance
Inheritance
Inheritance • Reuse code! • Common methods or attributes
Inheritance Object Serializable Boolean Throwable Dictionary Collection Exception Hash. Table Abstract. List IOException Arry. Lists Vector
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
Inheritance • Object • public String to. String() • public void finalize() throws Throwable
Inheritance • Throwable • public String get. Message() • public void print. Stack. Trace()
Inheritance • Exception • Constructors • All other methods inherited!
Creating subclasses • Key word: • Extends Public class My. Class { } Public class my. Sub. Class extends My. Class { }
Quiz • Define inheritance in Java • What key word is used in java to implement inheritance?
Creating subclasses • All classes inherit directly or indirectly from Object • Java allows only one superclass Class 1 Class 2 Sub. Class
Interface • Defines the methods • Implements • In the subclass
Interface interface My. Interface { void Interface. Method(); } public class My. Class implements My. Interface { void Interface. Method() { … } }
Interface public interface Collection<E> extends Iterable boolean Iterator<E> boolean add(E e) iterator() remove(Object o)
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.
Constructor public class My. Class { public My. Class() { } } public class My. Subclass extends My. Class { public my. Subclass() { super(); } }
Quiz • How does java handle multiple inheritance? • How does a subclass call the constructor of the class it inherits from?
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
Redefying methods • Redefine a method • Same name but different functions • Overloading • Overriding
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
Redefying methods public class My. Class { public void my. Method() {}; public void my. Method(int n. Val) {}; public void my. Method(float f. Val) {}; public void my. Method(int n. Val, float f. Val) {}; }
Redefying methods • Override occurs in the subclass • Can have same name, parameters and types • Differs in implementation • Occurs at run time
Redefying methods public class My. Class { public void my. Method() { /* do something */} } public class My. Subclass extends My. Class { public void my. Method() () { /* do something else */} }
Redefying methods • @Override • Tells the compiler you intend to override a method • Just in case you make a spelling error @Override public void my. Method()
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
Redefying methods public class My. Class { public static void my. Method(){}; public void my. Other. Method(){}; } public class My. Subclass extends My. Cass { public static void my. Method(){}; public void my. Other. Method(){}; } My. Sub. Class My. Class my. Subclass = new My. Subclass(); my. Class = my. Sub. Class; my. Subclass. my. Method(); // calls subclass my. Subclass. my. Other. Method(); // calls subclass my. Class. my. Method(); // calls super class my. Class. my. Other. Method(); // calls subclass
Examples Menu. Container Menu. Component Text. Component Button Image. Observer Canvas Serializable
Examples • public abstract class Component extends Object implements Image. Observer, Menu. Container, Serializable • public static float CENTER_ALLIGMENT • public boolean action(Event evt, Object what) • public void add. Component. Listener(Componet. Listerner) • Swing
Examples • public Interface Image. Observer • boolean image. Update(…) • Implemented in class Component
Examples Shape Cloneable Rectangular. Shape Arc 2 D Ellipse 2 D Rectangular 2 D
Examples • public interface Shape • boolean Contains(…) • Rectangle get. Bounds()
Examples • public abstract class Rectangular. Shape extends object implements Shape, Cloneable • public Rectangle get. Bounds() • Class Rectangle inherits from Rectangle 2 D • public void set. Frame(…)
Quiz • Name two ways Java allows you to redefine methods • What is the difference between hidden and override?
Questions?
- Slides: 36