javacontents.kocw.net/kocw/test/document/2013/skku/navrati... · 2016-09-11 · inheritance basic:...

31
JAVA Lab-9 : Inheritance Prof. Navrati Saxena TA- Rochak Sachan

Upload: others

Post on 20-Jan-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

JAVA

Lab-9 : Inheritance

Prof. Navrati Saxena TA- Rochak Sachan

Page 2: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Chapter Outline:

Inheritance Basic: Introduction

Member Access and Inheritance

Using super

Creating a Multilevel Hierarchy

Method Overriding

Dynamic Method Dispatch

Using Abstract Class

Using final with Inheritance

2

Java lab by Rochak Sachan

Page 3: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Inheritance Basic: Introduction

Java lab by Rochak Sachan

3

Java Inheritance defines an is-a relationship

between a super class and its subclasses.

This means that an object of a subclass can be used

wherever an object of the super class can be used.

Class Inheritance in java mechanism is used to

build new classes from existing classes.

The inheritance relationship is transitive: if class x

extends class y, then a class z, which extends class x,

will also inherit from class y.

Page 4: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Inheritance Basic:

Java lab by Rochak Sachan

4

For example a car class can inherit some properties from a

General vehicle class.

Here we find that the base class is the vehicle class and the

subclass is the more specific car class.

A subclass must use extends clause to derive from a super class

which must be written in the header of the subclass definition.

The subclass inherits members of the super class and hence

promotes code reuse.

The subclass itself can add its own new behavior and

properties.

Page 5: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Inheritance Basic:

Java lab by Rochak Sachan

5

The following kinds of inheritance are there in java.

Simple Inheritance

Multilevel Inheritance

Vehicle

Car

Vehicle

Car

Racing Car Simple Inheritance

Multilevel Inheritance

Page 6: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Inheritance Basic…

Java lab by Rochak Sachan

6

// A simple example of inheritance.

// Create a superclass.

class A {

int i, j;

void showij() {

System.out.println("i and j: " + i + " " + j);

}}

// Create a subclass by extending class A.

class B extends A {

int k;

void showk() {

System.out.println("k: " + k);

}

void sum() {

System.out.println("i+j+k: " + (i+j+k));

}}

class SimpleInheritance {

public static void main(String args[]) {

A superOb = new A();

B subOb = new B();

// The superclass may be used by itself.

superOb.i = 10;

superOb.j = 20;

System.out.println("Contents of superOb: ");

superOb.showij();

System.out.println();

/* The subclass has access to all public

members of

its superclass. */

subOb.i = 7;

subOb.j = 8;

subOb.k = 9;

Page 7: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Inheritance Basic…

Java lab by Rochak Sachan

7

System.out.println("Contents of subOb: ");

subOb.showij();

subOb.showk();

System.out.println();

System.out.println("Sum of i, j and k in

subOb:");

subOb.sum();

}

}

OUTPUT:

Contents of superOb:

i and j: 10 20

Contents of subOb:

i and j: 7 8

k: 9

Sum of i, j and k in subOb:

i+j+k: 24

Page 8: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Member Access and Inheritance

Java lab by Rochak Sachan

8

a subclass includes all of the members of its super

class

it cannot access those members of the super class

that have been declared as private.

For example, consider the following simple class

hierarchy:

Page 9: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Member Access and Inheritance…

Java lab by Rochak Sachan

9

/* In a class hierarchy, private members

remain

private to their class.

This program contains an error and will not

compile.

*/

// Create a superclass.

class A {

int i; // public by default

private int j; // private to A

void 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

}}

class Access {

public static void main(String args[]) {

B subOb = new B();

subOb.setij(10, 12);

subOb.sum();

System.out.println("Total is " + subOb.total);

}}

A class member that has been declared as private will remain private to

its class. It is not accessible by any code outside its class, including subclasses.

Page 10: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Java lab by Rochak Sachan

10

// This program uses inheritance to extend

Box.

class Box {

double width;

double height;

double depth;

// construct clone of an object

Box(Box ob) { // pass object to constructor

width = ob.width;

height = ob.height;

depth = ob.depth;

}

// constructor used when all dimensions

specified

Box(double w, double h, double d) {

width = w;

height = h;

depth = d;

}

// constructor used when no dimensions

specified

Box() {

width = -1; // use -1 to indicate

height = -1; // an uninitialized

depth = -1; // box

}

// constructor used when cube is created

Box(double len) {

width = height = depth = len;

}

Let’s take a another Example…

Page 11: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Java lab by Rochak Sachan

11

// compute and return volume

double volume() {

return width * height * depth;

}}

// Here, Box is extended to include

weight.

class BoxWeight extends Box {

double weight; // weight of box

// constructor for BoxWeight

BoxWeight(double w, double h, double

d, double m) {

width = w;

height = h;

depth = d;

weight = m;

}

}

class DemoBoxWeight {

public static void main(String args[]) {

BoxWeight mybox1 = new BoxWeight(10, 20, 15,

34.3);

BoxWeight mybox2 = new BoxWeight(2, 3, 4,

0.076);

double vol;

vol = mybox1.volume();

System.out.println("Volume of mybox1 is " + vol);

System.out.println("Weight of mybox1 is " +

mybox1.weight);

System.out.println();

vol = mybox2.volume();

System.out.println("Volume of mybox2 is " + vol);

System.out.println("Weight of mybox2 is " +

mybox2.weight);

}}

Page 12: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Java lab by Rochak Sachan

12

OUTPUT:

Volume of mybox1 is 3000.0

Weight of mybox1 is 34.3

Volume of mybox2 is 24.0

Weight of mybox2 is 0.076

Page 13: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Using super

Java lab by Rochak Sachan

13

The super is java keyword.

As the name suggest super is used to access the members of the super class.

It is used for two purposes in java: The first use of keyword super is to access the hidden data

variables of the super class hidden by the sub class.

Ex. Suppose class A is the super class that has two instance variables as int a and float b. class B is the subclass that also contains its own data members named a and b. then we can access the super class (class A) variables a and b inside the subclass class B just by calling the following command.

The second use super to Call Superclass Constructors

Page 14: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

1.super.member;

Java lab by Rochak Sachan

14

class A{

int a;

float b;

void Show(){

System.out.println("b in super class: " + b);

}

}

class B extends A{

int a;

float b;

B( int p, float q){

a = p;

super.b = q;

}

void Show(){

super.Show();

System.out.println("b in super class: " + super.b);

System.out.println("a in sub class: " + a);

}

public static void main(String[] args){

B subobj = new B(1, 5);

subobj.Show();

}

}

OUTPUT:

b in super class: 5.0

b in super class: 5.0

a in sub class: 1

Page 15: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

2.super(parameter-list);

Java lab by Rochak Sachan

15

class A{

int a;

int b;

int c;

A(int p, int q, int r){

a=p;

b=q;

c=r;

}

}

class B extends A{

int d;

B(int l, int m, int n, int o)

{

super(l,m,n);

d=o;

}

void Show(){

System.out.println("a = " + a);

System.out.println("b = " + b);

System.out.println("c = " + c);

System.out.println("d = " + d);

}

public static void main(String args[]){

B b = new B(4,3,8,7);

b.Show();

}

}

OUTPUT:

a = 4

b = 3

c = 8

d = 7

Page 16: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Creating a Multilevel Hierarchy

Java lab by Rochak Sachan

16

simple class hierarchies that consist of only a super

class and a subclass.

And in Multilevel Hierarchy contains many layers of

inheritance.

For example, given three classes called A, B, and C, C

can be a subclass of B, which is a subclass of A.

In this case, C inherits all aspects of B and A.

Page 17: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Let’s take Multilevel Hierarchy

Java lab by Rochak Sachan

17

class Box {

private double width;

private double height;

private double depth;

Box(Box ob) { // pass object to constructor

width = ob.width;

height = ob.height;

depth = ob.depth; }

Box(double w, double h, double d) {

width = w;

height = h;

depth = d; }

Box() {

width = -1; // use -1 to indicate

height = -1; // an uninitialized

depth = -1; // box

}

Box(double len) {

width = height = depth = len; }

double volume() {

return width * height * depth;

}}

class BoxWeight extends Box

{

double weight; // weight of box

BoxWeight(BoxWeight ob) { // pass object to co

nstructor

super(ob);

weight = ob.weight;}

BoxWeight(double w, double h, double d, doubl

e m) {

super(w, h, d); // call superclass constructor

weight = m;

}

Page 18: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Java lab by Rochak Sachan

18

BoxWeight() {

super();

weight = -1;}

BoxWeight(double len, double m) {

super(len);

weight = m;

}}

class Shipment extends BoxWeight {

double cost;

Shipment(Shipment ob) { // pass object to c

onstructor

super(ob);

cost = ob.cost;}

Shipment(double w, double h, double d, do

uble m, double c) {

super(w, h, d, m); // call superclass construc

tor

cost = c; }

Shipment() {

super();

cost = -1;

}

Shipment(double len, double m, double c) {

super(len, m);

cost = c;

}

}

class DemoShipment {

public static void main(String args[]) {

Shipment shipment1 = new Shipment(10, 20, 15,

10, 3.41);

Shipment shipment2 = new Shipment(2, 3, 4, 0.7

6, 1.28);

double vol;

Page 19: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Java lab by Rochak Sachan

19

vol = shipment1.volume();

System.out.println("Volume of shipment1 is " + vol);

System.out.println("Weight of shipment1 is " + shipment1.weight);

System.out.println("Shipping cost: $" + shipment1.cost);

System.out.println();

vol = shipment2.volume();

System.out.println("Volume of shipment2 is " + vol);

System.out.println("Weight of shipment2 is " + shipment2.weight);

System.out.println("Shipping cost: $" + shipment2.cost);

}}

Volume of shipment1 is 3000.0

Weight of shipment1 is 10.0

Shipping cost: $3.41

Volume of shipment2 is 24.0

Weight of shipment2 is 0.76

Shipping cost: $1.28

Page 20: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Method Overriding

Java lab by Rochak Sachan

20

When you extends a class, you can change the

behavior of a method in the parent class.

This is called method overriding.

This happens when you write in a subclass a method

that has the same signature as a method in the

parent class.

If only the name is the same but the list of

arguments is not, then it is method overloading.

Page 21: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Java lab by Rochak Sachan

21

class A {

int i;

A(int a, int b) {

i = a+b;

}

void add() {

System.out.println("Sum of a and b is: " + i);

}

}

class B extends A {

int j;

B(int a, int b, int c) {

super(a, b);

j = a+b+c;

}

void add() {

super.add();

System.out.println("Sum of a, b and c is: " + j);

}

}

class MethodOverriding {

public static void main(String args[]) {

B b = new B(10, 20, 30);

b.add();

}

}

OUTPUT:

Sum of a and b is: 30

Sum of a, b and c is: 60

Method Overriding

Page 22: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Dynamic Method Dispatch

Java lab by Rochak Sachan

22

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 23: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Dynamic Method Dispatch

Java lab by Rochak Sachan

23

// Dynamic Method Dispatch

class A {

void callme() {

System.out.println("Inside A's callme method");

}}

class B extends A {

// override callme()

void callme() {

System.out.println("Inside B's callme method");

}}

class C extends A {

// override callme()

void callme() {

System.out.println("Inside C's callme method");

}

}

class Dispatch {

public static void main(String args[]) {

A a = new A(); // object of type A

B b = new B(); // object of type B

C c = new C(); // object of type C

A r; // obtain a reference of type A

r = a; // r refers to an A object

r.callme(); // calls A's version of callme

r = b; // r refers to a B object

r.callme(); // calls B's version of callme

r = c; // r refers to a C object

r.callme(); // calls C's version of callme

}}

OUTPUT:

Inside A’s callme method

Inside B’s callme method

Inside C’s callme method

Page 24: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Java Abstract Class

Java lab by Rochak Sachan

24

An abstract class is a class that is declared by using the abstract keyword.

It may or may not have abstract methods.

Abstract classes cannot be instantiated, but they can be extended into sub-classes.

Points of abstract class : Abstract class contains abstract methods.

Program can't instantiate an abstract class.

Abstract classes contain mixture of non-abstract and abstract methods.

If any class contains abstract methods then it must implements all the abstract methods of the abstract class.

Page 25: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Java Abstract Class

Java lab by Rochak Sachan

25

// A Simple demonstration of abstract.

abstract class A {

abstract void callme();

// concrete methods are still allowed in

abstract classes

void 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 26: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Using final with Inheritance

Java lab by Rochak Sachan

26

A java variable can be declared using the keyword

final. Then the final variable can be assigned only once.

A variable that is declared as final and not initialized is

called a blank final variable. A blank final variable

forces the constructors to initialise it.

Java classes declared as final cannot be extended.

Restricting inheritance!

Methods declared as final cannot be overridden. In

methods private is equal to final, but in variables it is

not.

Page 27: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Using final with Inheritance

Java lab by Rochak Sachan

27

final parameters – values of the parameters cannot

be changed after initialization. Do a small java

exercise to find out the implications of final

parameters in method overriding.

Java local classes can only reference local variables

and parameters that are declared as final.

A visible advantage of declaring a java variable

as static final is, the compiled java class results in

faster performance.

Page 28: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

1.Using final to Prevent Overriding

Java lab by Rochak Sachan

28

class 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!");

}

}

Java resolves calls to methods dynamically, at run time. This is called late binding.

However, since final methods cannot be overridden, a call to one can be resolved

at compile time. This is called early binding.

Page 29: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

2.Using final to Prevent Inheritance

Java lab by Rochak Sachan

29

final class A {

// ...

}

// The following class is illegal.

class B extends A { // ERROR! Can't subclass A

// ...

}

As the comments imply, it is illegal for B to inherit A since A is declared as final.

Page 30: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Java lab by Rochak Sachan

30

References

• Herbert Schildt; "Java The Complete

Reference, Seventh Edition (Osborne Complete

Reference Series)", McGraw-Hill, 2007

• http://www.java2s.com/Tutorial/Java/Catalog

Java.htm

• http://www.freejavaguide.com/corejava.htm

Page 31: JAVAcontents.kocw.net/KOCW/test/document/2013/skku/Navrati... · 2016-09-11 · Inheritance Basic: Java lab by Rochak Sachan 4 For example a car class can inherit some properties

Java lab by Rochak Sachan

31