inheritance dwight deugo ([email protected]) nesa matic ([email protected])

46
Inheritance Inheritance Dwight Deugo ([email protected]) Dwight Deugo ([email protected]) Nesa Matic ([email protected]) Nesa Matic ([email protected]) www.espirity.com

Upload: sylvia-king

Post on 03-Jan-2016

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

InheritanceInheritance

Dwight Deugo ([email protected])Dwight Deugo ([email protected])Nesa Matic ([email protected])Nesa Matic ([email protected])

www.espirity.com

Page 2: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

2 © 2003-2004, Espirity Inc.

Additional Contributors

None as of September, 2004

Page 3: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

3 © 2003-2004, Espirity Inc.

Module Overview

1. Inheritance2. Interfaces3. Type Hierarchy in Eclipse

Page 4: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

4 © 2003-2004, Espirity Inc.

Module Road Map

1. Inheritance What is inheritance? Class hierarchies Method lookup in Java Use of this and super Constructors and inheritance Abstract classes and methods

2. Interfaces3. Type Hierarchy in Eclipse

Page 5: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

5 © 2003-2004, Espirity Inc.

What is Inheritance? Inheritance is one of the main object-

oriented principals It promotes reuse It is a mechanism for sharing commonalities

between classes Commonalities are stored in a parent

class Also called superclass

Commonalities are shared between children classes Also called subclasses

Page 6: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

6 © 2003-2004, Espirity Inc.

Specialization and GeneralizationPolicy

clientpremiumpolicyNumbergetClientsetClient…

HomePolicyhouse

getHousesetHouse

AutoPolicyauto

getAutosetAuto

LifePolicy

gen

era

lizati

on

specia

lizatio

n

Page 7: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

7 © 2003-2004, Espirity Inc.

Defining Inheritance

In Java, inheritance is supported by using keyword extends It is said that subclass extends superclass If class definition does not specify explicit

superclass, its superclass is Object classpublic class Policy{…public class HomePolicy extends Policy{…public class AutoPolicy extends Policy{…public class LifePolicy extends Policy{…

public class Policy{… public class Policy extends Object{…

Page 8: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

8 © 2003-2004, Espirity Inc.

Variables and Inheritance

Variables can be declared as generic types, and assigned objects of more specific types Variable declared as of type Policy can be

assigned an instance of any Policy’s subclasses

Policy policy;Policy = new Policy();

Policy policy;Policy = new HomePolicy();

Policy policy;Policy = new AutoPolicy();

Policy policy;Policy = new LifePolicy();

Page 9: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

9 © 2003-2004, Espirity Inc.

Multiple Inheritance

Not supported in Java A class cannot extend more than one class There is only one direct superclass for any

class Object class is exception as it does not have

superclass

Page 10: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

10 © 2003-2004, Espirity Inc.

What is Inherited?

In general all subclasses inherit from superclass: Data Behavior

When we map these to Java it means that subclasses inherit: Fields (instance variables) Methods

Page 11: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

11 © 2003-2004, Espirity Inc.

Inheriting Fields

All fields from superclasses are inherited by a subclass Inheritance goes all the way up the

hierarchy

Policy

HomePolicy

clientpremiumpolicyNumber

clientpremiumpolicyNumber

house

clientpremiumpolicyNumberhouse

Page 12: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

12 © 2003-2004, Espirity Inc.

Inheriting Methods All methods from superclasses are

inherited by a subclass Inheritance goes all the way up the

hierarchygetClientsetClientgetPremiumsetPremiumgetPolicyNumbersetPolicyNumber

getHousesetHouse

getClientsetClientgetPremiumsetPremiumgetPolicyNumbersetPolicyNumber

getClientsetClientgetPremiumsetPremiumgetPolicyNumbersetPolicyNumbergetHousesetHouse

Policy

HomePolicy

Page 13: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

13 © 2003-2004, Espirity Inc.

Method lookup begins in the class of that object that receives a message

If method is not found lookup continues in the superclass

HomePolicygetHousesetHouse

house

Method Lookup

…HomePolicy homePolicy = new HomePolicy();…homePolicy.getPremium();

HomePolicy class – method not found

Policy

getPremiumsetPremium

premiumPolicy class – method found

Page 14: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

14 © 2003-2004, Espirity Inc.

this vs. super

They are both names of the receiver object

The difference is where the method lookup begins: this

Lookup begins in the receiver object’s class super

Lookup begins in the superclass of the class where the method is defined

Page 15: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

15 © 2003-2004, Espirity Inc.

Examplepublic void print(){

System.out.println("A " + getClass().getName() + ", $" + getPremium());}

public void print(){ super.print(); System.out.println("for house " + getHouse().toString();}

policy.print();

homePolicy.print();

Policy

HomePolicy

A Policy, $1,200.00 Console

A HomePolicy, $1,200.00for house 200 Great Street

Console

Page 16: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

16 © 2003-2004, Espirity Inc.

Method Overriding If a class define same method as its

superclass, it is said that the method is overridden Method signatures must match

Policy

HomePolicy

//Method in the Policy classpublic void print(){ System.out.println("A " + getClass().getName() + ", $" +

getPremium());}//Overridden method in the HomePolicy classpublic String toString(){ super.print(); System.out.println("for house " + getHouse().toString();}

Page 17: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

17 © 2003-2004, Espirity Inc.

Overriding Constructors

Similar to overriding methods, applying rules specific to constructors: First line in the constructor must be either

this(parameters) or super(parameters) This eventually leads to the Object class

constructor that creates the object If the call is not coded explicitly then an

implicit zero-argument super() is called If the superclass does not have a zero-

argument constructor, this causes an error

Page 18: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

18 © 2003-2004, Espirity Inc.

Example of Overriding Constructors

public Policy(double premium, Client aClient, String policyNumber){this.premium = premium;this.policyNumber = policyNumber;this.client = aClient;

}

public HomePolicy(double premium,Client aClient,String policyNumber,House aHouse){super(premium, aClient, policyNumber);this.house = aHouse;

}

Policy

HomePolicy

Page 19: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

19 © 2003-2004, Espirity Inc.

Abstracts Classes

Classes that cannot have instances They are designed to hold inherited fields

and methods for subclasses They also define what subclasses

should implement Details are left for concrete

implementation in subclasses Usually specified at the design level

Page 20: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

20 © 2003-2004, Espirity Inc.

Defining Abstract Classes

Modifier abstract is used to indicate abstract classpublic abstract class Policy {…

Policy

HomePolicy AutoPolicy LifePolicy

Page 21: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

21 © 2003-2004, Espirity Inc.

Abstract Methods

Can only be defined in abstract classes Abstract classes can contain concrete methods as

well Declaration of abstract method in concrete class

will result in compile error Declare method signatures

Implementation is left to the subclasess Each subclass must have concrete

implementation of the abstract method Used to impose method implementation on

subclasses

Page 22: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

22 © 2003-2004, Espirity Inc.

Defining Abstract Methods…

Modifier abstract is also used to indicate abstract method

public abstract class Policy{ public abstract void calculateFullPremium();

PolicycalculateFullPremium

HomePolicy AutoPolicy LifePolicy

Page 23: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

23 © 2003-2004, Espirity Inc.

…Defining Abstract Methods

All subclasses must implement all abstract methods

public class HomePolicy{ public void calculateFullPremium(){

//calculation may depend on a criteria about the house}

public class AutoPolicy{ public void calculateFullPremium(){

//calculation may depend on a criteria about the auto}

public class LifePolicy{ public void calculateFullPremium(){

//calculation may depend on a criteria about the client}

Page 24: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

24 © 2003-2004, Espirity Inc.

Module Road Map

1. Inheritance

2. Interfaces What is an interface? Defining interfaces Using Interfaces

3. Type Hierarchy in Eclipse

Page 25: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

25 © 2003-2004, Espirity Inc.

What is Interface? Interfaces define a type and protocol but

do not provide implementation of that protocol Classes that implement interfaces must

provide implementation for defined protocol Interfaces capture common behavior of the

classes that implement them Commonly used to:

Impose protocol on set of classes that implement interface

Indicate certain Java type (marker interfaces)

Page 26: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

26 © 2003-2004, Espirity Inc.

Interfaces Define Types

Interfaces are, in a way, a higher abstraction of Java types They define common protocol Are commonly used where types need to be

imposed but implementation is not important For example in distributed programming

Interfaces are used to impose typing If variable is declared as of interface type,

than instance of any class that implements that interface can be assigned to that variable

Page 27: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

27 © 2003-2004, Espirity Inc.

Defining Interface

Similar to defining classes Keyword interface used instead of class

keyword Defined methods contain signatures only

Interfaces are also stored in .java files

public interface Policyable{public Policy createPolicy();

}

public interface Transferable{}

Page 28: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

28 © 2003-2004, Espirity Inc.

Rules

Interfaces can contain: Only method signatures Only final static fields

Interfaces cannot contain: Any fields other than final static fields Any static methods Any method implementation Any constructors

Page 29: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

29 © 2003-2004, Espirity Inc.

Classes and Interfaces

At the design level interfaces are usually indicated with a stereotype <<interface>>

Instead of solid line, there is a dashed line between a class and interfaces it implements

<<interface>>Policyable

Building

House

Page 30: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

30 © 2003-2004, Espirity Inc.

Implementing Interfaces

Classes implement interfaces Keyword implements is used They must define all methods that

interface they implement declares

public class House extends Building implements Policyable{public Policy createPolicy(){

HomePolicy policy = new HomePolicy(this);return policy;

}}

Page 31: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

31 © 2003-2004, Espirity Inc.

Interfaces are Inherited

It is possible that one interface extends other interfaces Known as sybtyping in Java Multiple inheritance is allowed with

interfaces Inheritance works the same as with

classes All methods defined are inherited There is a hierarchy of interfaces that

exists in parallel to class hierarchy

Page 32: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

32 © 2003-2004, Espirity Inc.

Extending Interfaces

<<interface>Car

<<interface>Color

<<interface>ColoredCar

public interface ColoredCar extends Car, Color {public String getSpeed();public String getBaseColor();

}

public interface Car { public String getSpeed();

}

public interface Color { public String getBaseColor();

}

Page 33: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

33 © 2003-2004, Espirity Inc.

Naming Conventions

There are few conventions when naming interfaces: Suffix able is used for interfaces

Cloneable, Serializable, and Transferable Nouns are used for implementing classes names,

and I + noun for interfaces Interfaces: IColor, ICar, and IColoredCar Classes: Color, Car, and ColoredCar

Nouns are used for interfaces names, and noun+Impl for implementing classes

Interfaces: Color, Car, and ColoredCar Classes: ColorImpl, CarImpl, and ColoredCarImpl

Page 34: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

34 © 2003-2004, Espirity Inc.

Cross-Hierarchy Polymorphism

Allows classes across different hierarchies to share common protocols Supported and imposed by use of

interfaces

<<interface>>Policyable

Building

House

Vehicle

Auto

Page 35: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

35 © 2003-2004, Espirity Inc.

Implementation

public class PolicyFactory{public Policy createPolicy(House aHouse){

return aHouse.createPolicy();}public Policy createPolicy(Auto anAuto){

return aHouse.createPolicy();}…

}

public class PolicyFactory{public Policy createPolicy(Policyable aPolicyable){

return aPolicyable.createPolicy();}

}

Page 36: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

36 © 2003-2004, Espirity Inc.

Module Road Map

1. Inheritance2. Interfaces

3. Type Hierarchy in Eclipse Java types Type casing Eclipse type hierarchy Supertype and subtype hierarchies

Page 37: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

37 © 2003-2004, Espirity Inc.

Java Types

Variable can be declared as: Reference type

Any instance of that class or any of the subclasses can be assigned to the variable

Interface type Any instance of any class that implements that

interface can be assigned to the variable

Policy policy;policy = new AutoPolicy();policy = new HomePolicy();

Policable policableObject;policableObject = new Auto();policableObject = new House();

Page 38: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

38 © 2003-2004, Espirity Inc.

Variables and Messages

If variable is defined as a certain type, only messages defined for that type can be sent to the variable This may cause a problem if variable is

assigned to an instance of a subtype that defines different messages

Policy

AutoPolicy

getAuto()

Policy policy;policy = new AutoPolicy();policy.getAuto(); //ERROR

Page 39: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

39 © 2003-2004, Espirity Inc.

Type Casting

Allows for sending messages not defined by the declared superclass type by casting variable to the actual subclass type

This type cast is temporary The declared type of the variable stays the

same Allows for messages to be sent to subtype

Policy policy;policy = new AutoPolicy();((AutoPolicy)policy).getAuto();

Page 40: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

40 © 2003-2004, Espirity Inc.

Legal Type Casting?

Typecasting can be done on any subtypes of the declared type

A

B

C D

//let’s say that message() is defined//in class A//declare variable of type AA variable;//assign instance of class E to the//variablevariable = new E();((B)variable).message(); //VALID((A)variable).message(); //VALID((D)variable).message(); //VALID((E)variable).message(); //VALID((C)variable).message(); //NOT VALID

E

Page 41: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

41 © 2003-2004, Espirity Inc.

Eclipse and Inheritance

When creating class in Eclipse, wizards can be used to setup class details related to the inheritance Abstract classes and super class specified

Eclipse has built-in support for viewing and browsing different type hierarchies These include supertype and subtype

hierarchies

Page 42: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

42 © 2003-2004, Espirity Inc.

Eclipse Type Hierarchy

Opened by choosing Open Type Hierarchy option from the context menu on the class

Hierarchy View that opens represents a hierarchy of the chosen class

Page 43: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

43 © 2003-2004, Espirity Inc.

Supertype Hierarchy

Opened by click on the Supertype Hierarchy toolbar button of the Hierarchy view Hierarchy View shows the class and its

superclasses

Page 44: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

44 © 2003-2004, Espirity Inc.

Subtype Hierarchy

Opened by click on the Subtype Hierarchy toolbar button of the Hierarchy view Hierarchy View shows the class and its

subclasses

Page 45: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

45 © 2003-2004, Espirity Inc.

Review

In this module we discussed: Inheritance Method lookup in Java Use of this and super Abstract classes and methods Interfaces Classes and interfaces Polymorphism and inheritance Type hierarchies in Eclipse

Page 46: Inheritance Dwight Deugo (dwight@espirity.com) Nesa Matic (nesa@espirity.com)

46 © 2003-2004, Espirity Inc.

Labs!

Lab: Inheritance