inheritance

19
Inheritance Inheritance

Upload: dyan

Post on 19-Jan-2016

22 views

Category:

Documents


0 download

DESCRIPTION

Inheritance. Inheritance. New class (derived class) is created from another class (base class). Ex. Employee HourlyEmployee SalariedEmployee Some things like name and hireDate are common to all employees. Belong in a base class What else can you imagine belongs in the base class?. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Inheritance

InheritanceInheritance

Page 2: Inheritance

InheritanceInheritance

New class (derived class) is created from New class (derived class) is created from another class (base class).another class (base class).

Ex.Ex.• EmployeeEmployee

HourlyEmployeeHourlyEmployee SalariedEmployeeSalariedEmployee

• Some things like name and hireDate are Some things like name and hireDate are common to all employees.common to all employees.

Belong in a base classBelong in a base class What else can you imagine belongs in the base class?What else can you imagine belongs in the base class?

Page 3: Inheritance

InheritanceInheritance

EmployeeEmployee• HourlyEmployeeHourlyEmployee• SalariedEmployeeSalariedEmployee

Some things are relevant to only a Some things are relevant to only a salaried employee or an hourly salaried employee or an hourly employee.employee.• Can you think of anything?Can you think of anything?

Page 4: Inheritance

EmployeeEmployeepublic class Employee {public class Employee {

private Stringprivate String mName;mName;private Dateprivate Date mHireDate;mHireDate;

public Employee ( ) { … }public Employee ( ) { … }public Employee ( String name, Date date ) { … }public Employee ( String name, Date date ) { … }public Employee ( Employee original ) { … }public Employee ( Employee original ) { … }

public String getName ( ) { … }public String getName ( ) { … }public Date getHireDate ( ) { … }public Date getHireDate ( ) { … }

public void setName ( String name ) { … }public void setName ( String name ) { … }public void setHireDate ( Date date ) { … }public void setHireDate ( Date date ) { … }

public String toString ( ) { … }public String toString ( ) { … }public boolean equals ( Employee other ) { … }public boolean equals ( Employee other ) { … }

}}

Page 5: Inheritance

Inheritance syntaxInheritance syntax

public class DerivedClassNamepublic class DerivedClassName

extends BaseClassNameextends BaseClassName

{{

DeclarationsOfAddedStaticVariablesDeclarationsOfAddedStaticVariables

DeclarationsOfAddedInstanceVariablesDeclarationsOfAddedInstanceVariables

DefinitionsOfAddedAndOverriddenMethodsDefinitionsOfAddedAndOverriddenMethods

}}

Page 6: Inheritance

Inheritance stepsInheritance steps

1.1. Define base class (e.g., Employee)Define base class (e.g., Employee)

2.2. Define derived classDefine derived class

public HourlyEmployee extends Employee {public HourlyEmployee extends Employee {

……

}}

Page 7: Inheritance

InheritanceInheritance

Base class = parent class = ancestor Base class = parent class = ancestor = superclass= superclass

Derived class = child class = Derived class = child class = descendent = subclassdescendent = subclass

Public and protected attributes and Public and protected attributes and methods in the base class are methods in the base class are available to (inherited by) the available to (inherited by) the derived class.derived class.

Page 8: Inheritance

Overriding methodsOverriding methods

Say our HourlyEmployee class adds Say our HourlyEmployee class adds an hourlyRate attribute.an hourlyRate attribute.

Say the base class has toString and Say the base class has toString and equals methods.equals methods.

The derived class inherits these but The derived class inherits these but they aren’t 100% sufficient for the they aren’t 100% sufficient for the derived class.derived class.

What do we do?What do we do?

Page 9: Inheritance

Overriding methodsOverriding methods

What do we do?What do we do?• Reimplement the equals and toString Reimplement the equals and toString

methods in the derived class.methods in the derived class.• These reimplemented methods override These reimplemented methods override

the methods in the base class.the methods in the base class.

Page 10: Inheritance

Overriding methodsOverriding methods

Note:Note:• What does the keyword final mean when used What does the keyword final mean when used

with instance variables?with instance variables?

• What does the keyword final mean when used What does the keyword final mean when used with methods?with methods?

• What does the keyword final mean when used What does the keyword final mean when used with the class definition?with the class definition?

• How does overriding differ from overloading?How does overriding differ from overloading?

Page 11: Inheritance

Covariant return typeCovariant return type

In general, one cannot override a In general, one cannot override a method and change the return type.method and change the return type.

Exception: (Java 5 or greater)Exception: (Java 5 or greater)• If the return type is a class type, it can If the return type is a class type, it can

be changed to a descendent type of the be changed to a descendent type of the original class type.original class type.

Page 12: Inheritance

Public and privatePublic and private

Derived class can “loosen” access Derived class can “loosen” access restrictions.restrictions.• Derived class can change private to Derived class can change private to

public.public. Derived class can’t “tighten” them.Derived class can’t “tighten” them.

• Derived class can’t change public to Derived class can’t change public to private.private.

Page 13: Inheritance

Super ctorSuper ctor

Subclass ctor may call superclass ctor.Subclass ctor may call superclass ctor.• Ex. super( a, b, … );Ex. super( a, b, … );• Number of args may differNumber of args may differ• Call to super() must be first line in subclass ctor.Call to super() must be first line in subclass ctor.

If derived class doesn’t call a base class If derived class doesn’t call a base class ctor, then the no-arg ctor of the base class ctor, then the no-arg ctor of the base class is automatically invoked (first).is automatically invoked (first).• How can we demonstrate this?How can we demonstrate this?

Typically, a base class ctor should always be Typically, a base class ctor should always be called.called.

Page 14: Inheritance

Super ctorSuper ctor If derived class doesn’t call a base class ctor, then the no-arg ctor of If derived class doesn’t call a base class ctor, then the no-arg ctor of

the base class is automatically invoked (first).the base class is automatically invoked (first).• How can we demonstrate this?How can we demonstrate this?

public class Tester7 extends Tester7Superclass {public class Tester7 extends Tester7Superclass { public Tester7 ( String s ) {public Tester7 ( String s ) {

System.out.println( "Tester7() says hello." );System.out.println( "Tester7() says hello." );}}public static void main ( String p[] ) {public static void main ( String p[] ) {

new Tester7( "fred" );new Tester7( "fred" );}}

}}

class Tester7Superclass {class Tester7Superclass {public Tester7Superclass ( ) {public Tester7Superclass ( ) {

System.out.println( "Tester7Superclass() says hello." );System.out.println( "Tester7Superclass() says hello." );}}

}}How can we make sure that this doesn’t happen?How can we make sure that this doesn’t happen?

Page 15: Inheritance

Super ctorSuper ctor If derived class doesn’t call a base class ctor, then the no-arg ctor of If derived class doesn’t call a base class ctor, then the no-arg ctor of

the base class is automatically invoked (first).the base class is automatically invoked (first).• How can we demonstrate this?How can we demonstrate this?

public class Tester7 extends Tester7Superclass {public class Tester7 extends Tester7Superclass { public Tester7 ( String s ) {public Tester7 ( String s ) {

System.out.println( "Tester7() says hello." );System.out.println( "Tester7() says hello." );}}public static void main ( String p[] ) {public static void main ( String p[] ) {

new Tester7( "fred" );new Tester7( "fred" );}}

}}

class Tester7Superclass {class Tester7Superclass {privateprivate Tester7Superclass ( ) { Tester7Superclass ( ) {

System.out.println( "Tester7Superclass() says hello." );System.out.println( "Tester7Superclass() says hello." );}}

}}How can we make sure that this doesn’t happen? Won’t even compile!How can we make sure that this doesn’t happen? Won’t even compile!

Page 16: Inheritance

The The thisthis ctor ctor

We can use the We can use the thisthis ctor to call another ctor to call another ctor in the same class.ctor in the same class.• Must call the Must call the thisthis ctor first ctor first• Can’t use both the Can’t use both the thisthis ctor and super. ctor and super.• Ex.Ex.

public HourlyEmployee ( ) {public HourlyEmployee ( ) {

this( “no name”,this( “no name”,

new Date(“January”, 1, 1000), 0, new Date(“January”, 1, 1000), 0, 0 );0 );

}}

Page 17: Inheritance

TypesTypes

An object of a derived class has more An object of a derived class has more than one type.than one type.• Not only its type but also the type of Not only its type but also the type of

every one of its ancestors (all the way every one of its ancestors (all the way back to Object).back to Object).

• Object is the base class for classes that Object is the base class for classes that don’t extend anything. Object is the don’t extend anything. Object is the ancestor of all classes.ancestor of all classes.

Page 18: Inheritance

java.lang.Objectjava.lang.Object

Many methods.Many methods. See See

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html..

Page 19: Inheritance

TypesTypes instanceof operatorinstanceof operator

• Object instanceof ClassNameObject instanceof ClassName• True if Object is of type ClassName; false True if Object is of type ClassName; false

otherwiseotherwise• Example:Example:

Integer i = new Integer(12);Integer i = new Integer(12);Object o = i;Object o = i;

//won’t compile (not possible)://won’t compile (not possible)://System.out.println( (i instanceof String) );//System.out.println( (i instanceof String) );

//OK. Compiles; runs; returns false://OK. Compiles; runs; returns false:System.out.println( (o instanceof String) ); //falseSystem.out.println( (o instanceof String) ); //false

System.out.println( (o instanceof Object) ); //trueSystem.out.println( (o instanceof Object) ); //trueSystem.out.println( (o instanceof Integer) ); //trueSystem.out.println( (o instanceof Integer) ); //true