Top Banner
Inheritance
32

Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Jan 01, 2016

Download

Documents

Emery Newton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Inheritance

Page 2: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Introduction

• Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

• Using inheritance, you can create a general class that defines traits common to a set of related items.

• Class Inheritance in java mechanism is used to build new classes from existing classes.

Page 3: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Introduction

• A class that is derived from another class is called a subclass .

• The class from which the subclass is derived is called a superclass .

• A subclass inherits all the members from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Page 4: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Syntax

• class Subclass-name extends Superclass-name

{ //methods and fields }

Page 5: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Object Class

• The Object class, defined in the java.lang package, defines and implements behavior common to all classes

Page 6: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Types of Inheritance

Page 7: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Types of Inheritance

Multiple Inheritance is not allowed in Java

Page 8: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Member Access and Inheritance

• A subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as private.

Page 9: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Example/ /Create a superclass.class A {

int i; // public by defaultprivate int j; // private to Avoid setij(int x, int y) {

i = x;j = y;

}

}// A's j is not accessible here.class B extends A {

int total;void sum() {

total = i + j; // ERROR, j is not accessible here

}

}

Page 10: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Practical Example

• Box Example

Page 11: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

A Superclass Variable Can Reference a Subclass Object

• Areference variable of a superclass can be assigned a reference to any subclass derived from that superclass.

Page 12: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

A Superclass Variable Can Reference a Subclass Object

BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37);Box plainbox = new Box();double vol;vol = weightbox.volume();System.out.println("Volume of weightbox is " + vol);System.out.println("Weight of weightbox is " +weightbox.weight);System.out.println();// assign BoxWeight reference to Box referenceplainbox = weightbox;vol = plainbox.volume(); // OK, volume() defined in BoxSystem.out.println("Volume of plainbox is " + vol);/* The following statement is invalid because plainboxdoes not define a weight member. */// System.out.println("Weight of plainbox is " + plainbox.weight);

Page 13: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Using super

• Using super to Call Superclass Constructors– super(arg-list);

• Here, arg-list specifies any arguments needed by the constructor in the superclass.

• super( ) must always be the first statement executed inside a subclass’ constructor.

• Since constructors can be overloaded, super( ) can be called using any form defined by the superclass.

• The constructor executed will be the one that matches the arguments

Page 14: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Example

A complete implementation of BoxWeight.

Page 15: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

A Second Use for super

• The second form of super acts somewhat like this, except that it always refers to the superclass of the subclass in which it is used.

• super.member• Here, member can be either a method or an

instance variable.• This second form of super is most applicable to

situations in which member names of a subclass hide members by the same name in the superclass.

Page 16: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

A Second Use for super// Using super to overcome name hiding.class A {

int i;}// Create a subclass by extending class A.class B extends A {

int i; // this i hides the i in AB(int a, int b) {

super.i = a; // i in Ai = b; // i in B

}void show() {

System.out.println("i in superclass: " + super.i);System.out.println("i in subclass: " + i);

}}

Page 17: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Creating a Multilevel Hierarchy

Page 18: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Creating a Multilevel Hierarchy

class A{…..}class B extends A{…….}class C extends B{……}

Page 19: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

When Constructors Are Called

class A {A() {System.out.println("Inside A's constructor.");}}class B {B() {System.out.println("Inside B's constructor.");}}class C {C() {System.out.println("Inside C's constructor.");}}

Page 20: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

When Constructors Are Called

class CallingCons {public static void main(String args[]) {C c = new C();}}

Output:Inside A’s constructorInside B’s constructorInside C’s constructor

Page 21: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Method Overriding

• In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.

Page 22: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Method Overridingclass A {void show() {System.out.println(“Class A”);}}class B extends A {void show() {System.out.println(“Class B”);}}class Override {public static void main(String args[]) {B subOb = new B();subOb.show(); // this calls show() in B}}

Page 23: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Method Overriding

• If you wish to access the superclass version of an overridden method, you can do so by using super.

• In class B of previous example:void show() {super.show(); // this calls A's show()System.out.println(“Class B”);}

Page 24: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Methods with differing type signatures are overloaded – not overridden.

class A {void show() {System.out.println("i and j: " + i + " " + j);}}// Create a subclass by extending class A.class B extends A {// overload show()void show(String msg) {System.out.println(msg );}}class Override {public static void main(String args[]) {B subOb = new B();subOb.show("This is in B"); // this calls show() in BsubOb.show(); // this calls show() in A}}

Page 25: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Dynamic Method Dispatch

• Method overriding forms the basis for one of Java’s most powerful concepts: dynamic method dispatch.

• Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time.

• Dynamic method dispatch is important because this is how Java implements run-time polymorphism.

Page 26: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Dynamic Method Dispatchclass A {void callme() {System.out.println("Inside A's callme method");}}class B extends A {void callme() {System.out.println("Inside B's callme method");}}class Dispatch {public static void main(String args[]) {A a = new A(); // object of type AB b = new B(); // object of type BA r; // obtain a reference of type Ar = a; // r refers to an A objectr.callme(); // calls A's version of callmer = b; // r refers to a B objectr.callme(); // calls B's version of callme}}

Page 27: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Applying Method Overriding

• Example of run-time polymorphism.

Page 28: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Using Abstract Classes

• Define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method.

Page 29: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Using Abstract Classesabstract class A {abstract void callme();// concrete methods are still allowed in abstract classesvoid callmetoo() {System.out.println("This is a concrete method.");}}class B extends A {void callme() {System.out.println("B's implementation of callme.");}}class AbstractDemo {public static void main(String args[]) {B b = new B();b.callme();b.callmetoo();}}

Page 30: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Using final with Inheritance

• Using final to Prevent Overridingclass A {final void meth() {System.out.println("This is a final method.");}}class B extends A {void meth() { // ERROR! Can't override.System.out.println("Illegal!");}}

Page 31: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Using final with Inheritance

• Using final to Prevent Inheritancefinal class A {// ...}// The following class is illegal.class B extends A { // ERROR! Can't subclass A// ...}

Page 32: Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.

Thank You