inheritance dr. andrew wallace phd beng(hons) euring [email protected]

36
Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng [email protected]

Upload: stella-williamson

Post on 20-Jan-2016

225 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

InheritanceDr. Andrew Wallace PhD BEng(hons) EurIng

[email protected]

Page 2: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Overview

• Inheritance

• Creating subclasses

• Interface

• Constructor

• Hierarchies

• Redefying methods

Page 3: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Inheritance

Page 4: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Inheritance

Page 5: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Inheritance

• Reuse code!

• Common methods or attributes

Page 6: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

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

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

Inheritance

• Object

• public String toString()

• public void finalize() throws Throwable

Page 9: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Inheritance

• Throwable

• public String getMessage()

• public void printStackTrace()

Page 10: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Inheritance

• Exception

• Constructors

• All other methods inherited!

Page 11: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

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

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

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

Interface

• Interface• Defines the methods

• Implements• In the subclass

Page 15: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Examples

Component MenuComponent

MenuContainer ImageObserver Serializable

Button CanvasTextComponent

Page 30: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

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

Examples

• public Interface ImageObserver

• boolean imageUpdate(…)

• Implemented in class Component

Page 32: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Examples

RectangularShape

Shape Cloneable

Arc2D Ellipse2D Rectangular2D

Page 33: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Examples

• public interface Shape

• boolean Contains(…)

• Rectangle getBounds()

Page 34: Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

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

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

Questions?