inheritance dr. andrew wallace phd beng(hons) euring andrew@cs.umu.se

Post on 20-Jan-2016

225 Views

Category:

Documents

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

InheritanceDr. Andrew Wallace PhD BEng(hons) EurIng

andrew@cs.umu.se

Overview

• Inheritance

• Creating subclasses

• Interface

• Constructor

• Hierarchies

• Redefying methods

Inheritance

Inheritance

Inheritance

• Reuse code!

• Common methods or attributes

InheritanceObject

Boolean Throwable

Exception

IOException

Dictionary

HashTable

Collection

ArryLists Vector

AbstractList

Serializable

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 toString()

• public void finalize() throws Throwable

Inheritance

• Throwable

• public String getMessage()

• public void printStackTrace()

Inheritance

• Exception

• Constructors

• All other methods inherited!

Creating subclasses

• Key word:• Extends

Public class MyClass

{

}

Public class mySubClass extends MyClass

{

}

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

Class1

SubClass

Class2

Interface

• Interface• Defines the methods

• Implements• In the subclass

Interface

interface MyInterface

{

void InterfaceMethod();

}

public class MyClass implements MyInterface

{

void InterfaceMethod()

{

}

}

Interface

public interface Collection<E> extends Iterable

boolean add(E e)

Iterator<E> iterator()

boolean 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.

Constructorpublic class MyClass

{

public MyClass()

{

}

}

public class MySubclass extends MyClass

{

public mySubclass()

{

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 MyClass

{

public void myMethod() {};

public void myMethod(int nVal) {};

public void myMethod(float fVal) {};

public void myMethod(int nVal, float fVal) {};

}

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 MyClass

{

public void myMethod() { /* do something */}

}

public class MySubclass extends MyClass

{

public void myMethod() () { /* 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 myMethod()

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 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

Examples

Component MenuComponent

MenuContainer ImageObserver Serializable

Button CanvasTextComponent

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

Examples

• public Interface ImageObserver

• boolean imageUpdate(…)

• Implemented in class Component

Examples

RectangularShape

Shape Cloneable

Arc2D Ellipse2D Rectangular2D

Examples

• public interface Shape

• boolean Contains(…)

• Rectangle getBounds()

Examples

• public abstract class RectangularShape extends object implements Shape, Cloneable

• public Rectangle getBounds()• Class Rectangle inherits from Rectangle2D

• public void setFrame(…)

Quiz

• Name two ways Java allows you to redefine methods

• What is the difference between hidden and override?

Questions?

top related