Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance.

Similar presentations


Presentation on theme: "Inheritance."— Presentation transcript:

1 Inheritance

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

3 Inheritance New class (derived class) is created from another class (base class). Ex. Employee HourlyEmployee SalariedEmployee

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

5 Inheritance Employee HourlyEmployee SalariedEmployee Some things are relevant to only a salaried employee or an hourly employee. Can you think of anything?

6 Employee public class Employee { private String mName;
private Date mHireDate; public Employee ( ) { … } public Employee ( String name, Date date ) { … } public Employee ( Employee original ) { … } public String getName ( ) { … } public Date getHireDate ( ) { … } public void setName ( String name ) { … } public void setHireDate ( Date date ) { … } public String toString ( ) { … } public boolean equals ( Employee other ) { … } } What are these called?

7 Employee public class Employee { private String mName;
private Date mHireDate; public Employee ( ) { … } public Employee ( String name, Date date ) { … } public Employee ( Employee original ) { … } public String getName ( ) { … } public Date getHireDate ( ) { … } public void setName ( String name ) { … } public void setHireDate ( Date date ) { … } public String toString ( ) { … } public boolean equals ( Employee other ) { … } } What are these called?

8 Employee public class Employee { private String mName;
private Date mHireDate; public Employee ( ) { … } public Employee ( String name, Date date ) { … } public Employee ( Employee original ) { … } public String getName ( ) { … } public Date getHireDate ( ) { … } public void setName ( String name ) { … } public void setHireDate ( Date date ) { … } public String toString ( ) { … } public boolean equals ( Employee other ) { … } } What are these called?

9 Employee public class Employee { private String mName;
private Date mHireDate; public Employee ( ) { … } public Employee ( String name, Date date ) { … } public Employee ( Employee original ) { … } public String getName ( ) { … } public Date getHireDate ( ) { … } public void setName ( String name ) { … } public void setHireDate ( Date date ) { … } public String toString ( ) { … } public boolean equals ( Employee other ) { … } } What are these called?

10 Employee public class Employee { private String mName;
private Date mHireDate; public Employee ( ) { … } public Employee ( String name, Date date ) { … } public Employee ( Employee original ) { … } public String getName ( ) { … } public Date getHireDate ( ) { … } public void setName ( String name ) { … } public void setHireDate ( Date date ) { … } public String toString ( ) { … } public boolean equals ( Employee other ) { … } } The usual stuff!

11 Inheritance syntax public class DerivedClassName extends BaseClassName
{ DeclarationsOfAddedStaticVariables DeclarationsOfAddedInstanceVariables DefinitionsOfAddedAndOverriddenMethods }

12 Inheritance steps Define base class (e.g., Employee)
Define derived class public class HourlyEmployee extends Employee { }

13 Inheritance Base class = parent class = ancestor = superclass

14 Inheritance Base class = parent class = ancestor = superclass
Derived class = child class = descendent = subclass

15 Inheritance Base class = parent class = ancestor = superclass
Derived class = child class = descendent = subclass Public and protected attributes and methods in the base class are available to (inherited by) the derived class.

16 Overriding methods Say our HourlyEmployee class adds an hourlyRate attribute. Say the base class has toString and equals methods. The derived class inherits these but they aren’t 100% sufficient for the derived class. What do we do?

17 Overriding methods What do we do?
Reimplement the equals and toString methods in the derived class. These reimplemented methods override the methods in the base class.

18 Overriding methods Note:
What does the keyword final mean when used with instance variables? What does the keyword final mean when used with methods? What does the keyword final mean when used with the class definition? How does overriding differ from overloading?

19 Covariant return type In general, one cannot override a method and change the return type. Exception: (Java 5 or greater) If the return type is a class type, it can be changed to a descendent type of the original class type.

20 Public and private Derived class can “loosen” access restrictions.
Derived class can change private to public.

21 Public and private Derived class can “loosen” access restrictions.
Derived class can change private to public. Derived class can’t “tighten” them. Derived class can’t change public to private.

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

23 Super ctor Subclass ctor may call superclass ctor.
Ex. super( a, b, … ); Number of args may differ Call to super() must be first line in subclass ctor. If derived class doesn’t call a base class ctor, then the no-arg ctor of the base class is automatically invoked (first). How can we demonstrate this?

24 Super ctor Subclass ctor may call superclass ctor.
Ex. super( a, b, … ); Number of args may differ Call to super() must be first line in subclass ctor. If derived class doesn’t call a base class ctor, then the no-arg ctor of the base class is automatically invoked (first). How can we demonstrate this? Typically, a base class ctor should always be called.

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

26 Super ctor If derived class doesn’t call a base class ctor, then the no-arg ctor of the base class is automatically invoked (first). How can we demonstrate this? public class Tester7Superclass { public Tester7Superclass ( ) { System.out.println( "Tester7Superclass() says hello." ); } public class Tester7 extends Tester7Superclass { public Tester7 ( String s ) { System.out.println( "Tester7() says hello." ); public static void main ( String p[] ) { new Tester7( "fred" ); No base class ctor is explicitly called. Yet . . . Test output: Tester7Superclass() says hello. Tester7() says hello.

27 Super ctor How can we make sure that this can’t happen (that the no-arg ctor can’t be called by the subclass)? public class Tester7Superclass { private Tester7Superclass ( ) { System.out.println( "Tester7Superclass() says hello." ); } public class Tester7 extends Tester7Superclass { public Tester7 ( String s ) { System.out.println( "Tester7() says hello." ); public static void main ( String p[] ) { new Tester7( "fred" );

28 Super ctor How can we make sure that this can’t happen (that the no-arg ctor can’t be called by the subclass)? public class Tester7Superclass { private Tester7Superclass ( ) { System.out.println( "Tester7Superclass() says hello." ); } public class Tester7 extends Tester7Superclass { public Tester7 ( String s ) { System.out.println( "Tester7() says hello." ); public static void main ( String p[] ) { new Tester7( "fred" ); Now, this won’t even compile (because this ctor attempts to invoke the superclass ctor which is now private)! (The superclass must provide us with a non-private ctor that we can use, or we are sunk.)

29 The this ctor We can use the this ctor to call another ctor in the same class. Must call the this ctor first. Can’t use both the this ctor and super. Ex. public HourlyEmployee ( ) { this( “no name”, new Date(“January”, 1, 1000), 0, 0 ); }

30 Types An object of a derived class has more than one type.
Not only its type but also the type of every one of its ancestors (all the way back to Object). Object is the base class for classes that don’t extend anything. Object is the ancestor of all classes.

31 Every Employer is a Person.
Every Person is an Object. So every Employer is a Person and an Object.

32 java.lang.Object Many methods.
See

33 Types instanceof operator Object instanceof ClassName
True if Object is of type ClassName; false otherwise.

34 Types instanceof operator Object instanceof ClassName
True if Object is of type ClassName; false otherwise. Example: Integer i = new Integer(12); Object o = i; //won’t compile (not even possible): //System.out.println( (i instanceof String) ); //OK. Compiles; runs; returns false: System.out.println( (o instanceof String) ); //false System.out.println( (o instanceof Object) ); //true System.out.println( (o instanceof Integer) ); //true

35 Types instanceof operator Object instanceof ClassName
True if Object is of type ClassName; false otherwise. Example: Integer i = new Integer(12); Object o = i; //won’t compile (not even possible): //System.out.println( (i instanceof String) ); //OK. Compiles; runs; returns false: System.out.println( (o instanceof String) ); //false System.out.println( (o instanceof Object) ); //true System.out.println( (o instanceof Integer) ); //true


Download ppt "Inheritance."

Similar presentations


Ads by Google