chapter 4

54
Chapter 4 Inheritance

Upload: edie

Post on 19-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Chapter 4. Inheritance. Two Relations. is-a relationship Examples: Car is a vehicle, PickUpTruck is a Truck. has a relationship (composed-of) Examples: Car has a wheel. Inheritance. Inheritance Used to model the IS-A relationship Used to reuse code among related classes - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 4

Chapter 4

Inheritance

Page 2: Chapter 4

Two Relations

is-a relationshipExamples:

Car is a vehicle,

PickUpTruck is a Truck.has a relationship (composed-of)

Examples:

Car has a wheel

Page 3: Chapter 4

Inheritance

Inheritance Used to model the IS-A relationship Used to reuse code among related classes

Containment Used to model the has-a relationship

Page 4: Chapter 4

Inheritance Mechanism1. class Base2. {3. int n;4. public Base() {n=1;}5. }

1. class Derived extends Base2. {3. int n;4. public Derived() { n=10 ;}5. public int getValue() {return n ;}6. }

To define a subclass – keyword extendsclass FullName extends Name

Think about the memory layout with inheritance. What is the memory layout without

“extends Base” statement?

Page 5: Chapter 4

1. class Base2. {3. int n;4. public Base() {n=1;}5. }

1. class Derived2. {3. int n;4. public Derived() { n=10 ;}5. public int getValue() {return n ;}6. }

Example 1 : Inheritance

Page 6: Chapter 4

1. public class TestInheritance {2. public static void main(String [] args)3. {4. Base b = new Base();5. Derived d1 = new Derived();

6. System.out.println("base value "+b.n);7. System.out.println("derived value "+d1.n);8. System.out.println("derived value "+d1.getValue());9. }10. }

Example 1 : Inheritance

Page 7: Chapter 4

Example 2 : Inheritance

1. class Base2. {3. int n;4. public Base() {n=1;}5. }

1. class Derived extends Base2. {3. int n;4. public Derived() { n=10 ;}5. public int getValue() {return n ;}6. }

Page 8: Chapter 4

1. public class TestInheritance {2. public static void main(String [] args)3. {4. Base b = new Base();5. Derived d2 = new Derived();

6. System.out.println("base value "+b.n);7. System.out.println("derived value "+d2.n);8. System.out.println("derived value "+d2.getValue());9. }10. }

Example 1 and Example 2 has the same output, but the memory layouts of Object d1 and d2 are different.

Example 2 : Inheritance

Page 9: Chapter 4

Example 3 : Inheritance

1. class Base2. {3. int n;4. public Base() {n=1;}5. }

1. class Derived extends Base2. {3. int n;4. public Derived() { n=10 ;}5. public int getValue1() {return n ;}6. public int getValue2() {return super.n ;}7. }

Super key word allows you to access members of Base class

Page 10: Chapter 4

1. public class TestInheritance {2. public static void main(String [] args)3. {4. Base b = new Base();5. Derived d = new Derived();

6. System.out.println("base value "+b.n);7. System.out.println("derived value "+d.n);8. System.out.println("derived value "+d.getValue1());9. System.out.println("derived value "+d.getValue2());10. }11. }12. }

Example 3 : Inheritance

Page 11: Chapter 4

Base Class Initialization: Implicit Method 1

1. class Base2. { int n;3. Base() { n = 1 ; }4. }

1. class Derived extends Base2. {3. int n;4. public Derived() { n=10 ;}5. }

The default constructor Base() is called before line 4 is executed.

If the user-defined default constructor Base() is not given, a system-defined default constructor is used.

Page 12: Chapter 4

Base Class Initialization: Implicit Method 2

1. class Base2. { int n;3. Base(int nn) { n = nn ; }4. }

1. class Derived extends Base2. {3. int n;4. public Derived() { n=10 ;}5. }

Can you find what is wrong with this example?

Page 13: Chapter 4

1. class Base2. { int n;3. Base() { n = 0 ; }4. Base(int n) { this.n = n;} 5. }

1. class Derived extends Base2. {3. int n;4. public Derived() { super(-1); n=10 ;}

5. public int getValue1() {return n ;}

6. public int getValue2() {return super.n ;}7. }

Using super() key word, you can explicit choose which super class constructor you want to use.

Base Class Initialization: Explicit Method

Page 14: Chapter 4

1. class Base2. { int n; }3. class Derived extends Base4. {5. int n;6. public Derived() { n=10 ;}7. public int getValue1() {return n ;}8. public int getValue2() {return super.n ;}9. }

10. public class TestInheritance {11. public static void main(String [] args)12. {13. Base b = new Base();14. Derived d = new Derived();15. System.out.println("base value "+b.n);16. System.out.println("derived value "+d.n);17. System.out.println("derived value "+d.getValue1());18. System.out.println("derived value "+d.getValue2());19. }20. }

Page 15: Chapter 4

Liskov’s Substitution Principle

If for each object O1 of type S there is an

object O2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when O1 is substituted for O2 then S is a subtype of T.

- Barbara Liskov, “Data Abstraction and Hierarchy”, SIGPLAN Notices, 23, 5 (May, 1988)

Page 16: Chapter 4

Example : LSP

1. class Person2. {3. public void getName() {-----------}4. }5. class Student extends Person6. {7. public void getName() { -----------}8. public double getGPA() {------------}9. }

Instead of Person p1 = new Person();p1.getName();

we can usePerson p2 = new Student();p2.getName();

Page 17: Chapter 4

Binding

Binding is a two step operationStep 1:

Static Overloading/Static BindingStep 2:

Dynamic Binding

Page 18: Chapter 4

Binding: Static

Type (Class) of an object determines which method/action you can take on the object.

This is a compile time decision.

1. Complex a;

2. a.add(b); // O.K.

3. a.add(“Hello”); // Error

Static overloading means that the parameters to a method are always deduced statically, at compile time.

1.

Page 19: Chapter 4

Binding : Static1. class Base2. {private int n;3. public Base(int n) { this.n = n ;}4. public void showValue() 5. { System.out.println("Base Class"); }6. }

1. class Derived extends Base2. {private int n;3. public Derived(int n) { super(-1); this.n =n ;}4. public void showValue() 5. { System.out.println("Derived Class");}6. }

1. public class TestInheritance {2. public static void main(String [] args)3. {Base b = new Base(10);4. Base d = new Derived(10);5. b.showValue();6. d.showValue();7. }8. }

Page 20: Chapter 4

Binding : Static In the previous example, you can have both

1. b.showValue();2. d.showValue();

It is because

both b and d are of type Base Type Base allows showValue() action

Q: Why the output is “Base Class” for b.showValue()

and “Derived Class” for d.showValue()? A:

Page 21: Chapter 4

Dynamic Binding

Check what is the run-time type of the object.

The virtual machine walks up the inheritance hierarchy until it finds a method of matching signature.

The first method of the appropriate signature is used.

Page 22: Chapter 4

Example : Dynamic Binding

1. class Base2. {3. public void foo(Base x)4. {System.out.println("Base:Base");}5. public void foo(Derived x)6. {System.out.println("Base:Derived");}7. }

8. class Derived extends Base9. {10. public void foo(Base x)11. { System.out.println("Derived:Base"); }12. public void foo(Derived x)13. { System.out.println("Derived:Derived");}14. }

Page 23: Chapter 4

Example : Dynamic Binding

1. public class TestInheritance2. {3. public static void main(String [] args)4. {5. Base b = new Base();6. Base d = new Derived();

7. b.foo(b);8. b.foo(d);9. d.foo(b);10. d.foo(d);11. }12. }

Page 24: Chapter 4

Dynamic Binding

All methods and parameters are bound at

run time in Java except :

static methods final methods private methods

Page 25: Chapter 4

Downcasting

Changing the static type of an expression from a base class to a class farther down in the inheritance hierarchy.

1. double mygpa ;2. Person p1 = new Person();3. Person p2 = new Student();4. mygpa = p2.getGPA(); //

Error5. mygpa = ((Student) p2).getGPA() ; // O.K.

Page 26: Chapter 4

Overriding

Overriding – implementing a method in a subclass that has the same signature as a method in the superclass

Overriding is based on Dynamic Binding

Don’t confuse with Overloading – methods with same name in a class but with different signatures

Page 27: Chapter 4

Example : Overriding

1. class Base2. {private int n;3. public Base(int n) { this.n = n ;}4. public int getValue() { return n ;}5. }6. class Derived extends Base7. {private int n;8. public Derived(int n) {super(5); this.n =n ;}9. public int getValue() {return n ;}10. }11. public class TestInheritance {12. public static void main(String [] args)13. {14. Base b = new Base(10);15. Base d = new Derived(10);16. System.out.println("base value "+ b.getValue());17. System.out.println("derived value "+d.getValue());18. }19. }

Page 28: Chapter 4

Use this programming technique, when you want to make a small change in a base class method.

Super class Worker has a method doWork(), but sub class Workaholic is making a small change

1. public class Workaholic extends Worker2. {3. public void doWork()4. {5. super.doWork();6. drinkCoffee();7. super.doWork();8. }9. }

Example : Partial Overriding

Page 29: Chapter 4

final

final methods can not be overridden.final methods is a more efficient way of

programming.static binding is used for final methods.final class can not be extended.Any attempt to override a final method is

a compile time error.All methods in a final class are final

methods.

Page 30: Chapter 4

Polymorphism

Polymorphism – the same message can evoke different behavior depending on the receiver

A reference to the superclass can also reference instances of any subclass

NOTE: The reverse is NOT true

Page 31: Chapter 4

Polymorphism

Polymorphism – the same message can evoke different behavior depending on the receiver

A reference to the superclass can also reference instances of any subclass

NOTE: The reverse is NOT true

Page 32: Chapter 4

The Object class

Object is the ultimate super class

Every class in Java is directly or indirectly derived from the Object class

Some methods provided by Object: toString() equals() clone() hashcode()

Page 33: Chapter 4

1. public class Object 2. {3. public native int hashCode(); 4. public boolean equals(Object obj) 5. {6. return (this == obj);7. } 8. protected native Object clone() throws CloneNotSupportedException;1. public String toString() {2. return getClass().getName() + "@" + 3. Integer.toHexString(hashCode());4. }5. }

Page 34: Chapter 4

Abstract Class

Abstract class A class which can not be instantiatedA class which must be extended to be

usedAn abstract class generally contains one or

more abstract methods. Such a class must be declared as abstract.

Page 35: Chapter 4

Example : Abstract Class

1. public abstract class Shape2. {3. public abstract double area();4. public abstract double perimeter();5. public double semiperimeter()6. {7. return perimeter()/2;8. }9. }

Page 36: Chapter 4

Interface

Interface Declares features but provide no

implementation.Can be used to define a set of constants.Every thing is abstractUsed to implement multiple inheritance

(No side effect of multiple inheritance) A class implements an interface

Page 37: Chapter 4

Example : Interface

1. interface MyInterface2. {3. void aMethod(int i); // an abstract method4. }

1. class MyClass implements MyInterface2. {3. public void aMethod(int i)4. {5. // implementation details6. }7. }

aMethod(int i) is always public static.

Page 38: Chapter 4

1. public interface Constants{2. int ONE=1 ;3. public static final int TWO=2;4. public static final int THREE=3;5. }

The constants ONE, TWO, THREE can be used anywhere in the class implementing Constants.

Constants defined inside interface becomes automatically public static final.

System.out.println(Constants.ONE); // O.K.

Example : Interface

Page 39: Chapter 4

Example : Interface

Find an error in the following declaration of Constants interface.

1. interface Constants

2. {

3. public final int ONE ;

4. public static final int TWO=2;

5. public static final int THREE=3;

6. }

Page 40: Chapter 4

Why Interface? (1)

1. interface Student{2. float getGPA();3. }4. interface Employee{5. float getSalary();6. }7. public class StudentEmploy implements Student, Employee{8. public float getGPA()9. { // implementation }10. public float getSalary()11. { // implementation }12. }

Now,

StudentEmploy is

both Student and Employee.

Page 41: Chapter 4

Why Interface? (2)

Consider.

Student[] students = new Student[10]; Employee[] employees = new Employee[10];

Since a StudentEmployee can be viewed as both a Student and an

Employee, we can use the following statements.

students[0] = new StudentEmployee(); employees[0] = new StudentEmployee();

Page 42: Chapter 4

Why Interface? (3)Marker Interface

Marker Interfaces are empty interfaces, that is, interfaces that declare no methods or constants.

They are intended to mark classes as having certain properties.

Cloneable interface is a marker interface. If your class does not implement Cloneable interface,

you can not define clone method properly.

Page 43: Chapter 4

Interface : Comparable

1. public interface Comparable

2. {

3. public int compareTo(Object o);

4. }

Page 44: Chapter 4

Insertion Sort Integer Array

Insertion-Sort (int [] a)1. for (int p=1; p< inarray.length; p++)

2. {3. int tmp = inarray[p];4. int j;5. for (j=p; j>0 && inarray[j-1]>tmp; j--)6. inarray[j]= inarray[j-1];7. inarray[j]=tmp;8. }

Page 45: Chapter 4

Insertion Sort Comparable Array

Insertion-Sort (Comparable [] a)1. for( int p=1 ; p < a.length ; p++ )

2. {

3. Comparable tmp = a[p];

4. int j ;

5. for (j=p ; j>0 && tmp.compareTo(a[j-1])<0; j--)

6. a[j] = a[j-1];

7. a[j] = tmp;

8. }

Page 46: Chapter 4

Four Kinds of Methods

final – cannot be overridden (bound at compile-time)

abstract – must be overridden (bound at run-time)

static – no controlling object (bound at compile-time)

other – bound at run-time

Page 47: Chapter 4

Merge Sort, p293

Merge_Sort(A,p,r) if p < r

{

q [(p+r)/r]

Merge_Sort(A,p,q)

Merge_Sort(A,q+1,r)

Merge(A,p,q,r)

}

In here, [x] means the largest integer less than or equal to x.

Page 48: Chapter 4

Quick Sort, p295

QuickSort(A,p,r) if p<r { q Partition(A,p,r) QuickSort(A,p,q) QuickSort(A,q+1,r) }

Page 49: Chapter 4

Wrapper Class

Incompatible Types; Comparable [] inarray = new Comparable[10];

inarray[0] = 10; // compile error

inarray[1] = 20; // compile error

CorrectionInteger n1 = new Integer(10);

inarray[0] = n1 ;

System.out.println(inarray[0]);

Page 50: Chapter 4

List of Wrapper Class

All eight primitive types have wrapper classes. Short, Integer, LongFloat, DoubleBooleanByte, Character

Wrapper objects are immutable.

Page 51: Chapter 4

Wrapper Class : Integerpublic final class Short

extends Number implements Comparable Static Field

MAX_VALUE MIN_VALUE SIZE

public methods parseInt(String s) toBinaryString(int i) toHexString(int i) intValue()

Example System.out.println(Integer.toBinaryString(10)); System.out.println(Integer.toHexString(10));

Page 52: Chapter 4

Adapters:

In order to use an interface, you have to implement all the methods. Even if you need only a single method of them.

Example : Member Methods of interface WindowListener

1. windowActivated(WindowEvent e) 2. windowClosed(WindowEvent e) 3. windowClosing(WindowEvent e) 4. windowDeactivated(WindowEvent e) 5. windowDeiconified(WindowEvent e) 6. windowIconified(WindowEvent e) 7. windowOpened(WindowEvent e)

Page 53: Chapter 4

Problem. Do you want to implement all the methods of WindowListener interface when you need only one of them?

Adapter class can be used to overcome this problem.

Look at the exampleTestWindowListener1.java

Adapters:

Page 54: Chapter 4

Anonymous class is a class with no name. To understand the syntax of anonymous class, compare the following

examples. Example 1

mylabel.addWindowListener(new MyWindowAdapter());

Example 2

mylabel.addWindowListener(new WindowAdapter(){ public void windowClosing() { System.out.println ("System Exit"); System.exit(0); } });

Anonymous Class