Top Banner
1 Basics of Inheritance http:// improvejava.blogspot.in/
24

Basics of inheritance .22

Apr 16, 2017

Download

Documents

myrajendra
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: Basics of inheritance .22

1

Basics of Inheritance

http://improvejava.blogspot.in/

Page 2: Basics of inheritance .22

Objectives

On completion of this period, you would be able to learn

• Inheritance• Inheritance basics• Visibility modifiers

http://improvejava.blogspot.in/

Page 3: Basics of inheritance .22

Recap

In the previous lesson, you have learnt • Command-line arguments

http://improvejava.blogspot.in/

Page 4: Basics of inheritance .22

http://improvejava.blogspot.in/

• Inheritance is process by which one class gets the

properties from an existing class

• Object-oriented programming allows classes to inherit

• Commonly used state and behavior from other

classes

• Inheritance is one of the principles of object oriented

programming

• Benefit of inheritance is the software reuse

Inheritance

Page 5: Basics of inheritance .22

Inheritance Basics• Inheritance allows a software developer to

derive a new class from an existing one• The existing class is called the parent class or

superclass• The derived class is called the child class or

subclass• Creates an is-a relationship

The subclass is a more specific version of the original

5http://improvejava.blogspot.in/

Page 6: Basics of inheritance .22

6

• In Java a class that is inheriting is called a super class

• A class that is inherited is called sub class

• Sub class is a specialized version of super class

• Sub class inherits all instance variables and methods

defined by super class and can have its own elements

• In Java one direct superclass, and each superclass has

the potential for an unlimited number of subclasses

http://improvejava.blogspot.in/

Inheritance Basics contd..

Page 7: Basics of inheritance .22

• In Fig. 22.1 Book is the existing class• Dictionary and Novel are

sub classes of Book• Mystery and Romance are sub classes of Novel

Book

NovelDictionary

Mystery Romance

7

Fig 22.1 Book inheritance

Inheritance Basics contd..

http://improvejava.blogspot.in/

Page 8: Basics of inheritance .22

http://improvejava.blogspot.in/

Fig. 22.2 Bicycle example

Discussion

• What are the super and sub classes in Fig. 22.2

• Super class

• Sub classes

Bicycle

MountBike RoadBike TandemBike

Page 9: Basics of inheritance .22

http://improvejava.blogspot.in/

Object

Class A Class D

Class B Class CClass E

Fig. 22.3 Another example

Discussion contd..

• What are the super and sub classes in Fig. 22.3

• Super classes

• Sub classes

Object, Calls A, Class D

Class A, Class B, Calss C, Class C, Class D

Page 10: Basics of inheritance .22

http://improvejava.blogspot.in/

Syntax

• To inherit a class from another class use keyword

“extends”Syntax

class subclass-name extends superclass-name {( body of the class )

}If a class B is inherited from class A class B extends A // A inherits B

// Super class –A, Subclass- B

Page 11: Basics of inheritance .22

http://improvejava.blogspot.in/

• Consider the above Bicycle example • Using the above syntax the inheritance can be shown as

below

class MountainBike extends Bicycle { // new fields and methods defining a mountain bike would go here

}

Syntax contd...

Page 12: Basics of inheritance .22

http://improvejava.blogspot.in/

Example

class A {

int i,j;

void showij() {

System.out.println(“ i and j :” + I + “ “ + j);

}

}

Page 13: Basics of inheritance .22

http://improvejava.blogspot.in/

class B extends A {// creating subclass B from super class A

int k;

void showk() {

System.out.println(“ k:” +k);

}

void sum() {

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

}

}

Example contd..

Page 14: Basics of inheritance .22

http://improvejava.blogspot.in/

class SimpleInheritance {

public static void main( String args[]) {

A superOb = new A();

B subOb = new B();

superOb.i = 10;// super class may be used by itself

superOb.j = 20;

System.out.println( “ Contents of superOb:”);

superOb.showij();

System.out.println();

Example contd..

Page 15: Basics of inheritance .22

http://improvejava.blogspot.in/

subOb.i = 7; // sub class has access to all public members

subOb.j = 8; // of its super class

subOj.k = 9;System.out.println(“ Contents of subOj:” );subOb.showij();subOj.showk();System.out.println();System.out.println( “ sum of i,j and k in

subOb:”);subOb.sum();

} }

Example contd..

Page 16: Basics of inheritance .22

http://improvejava.blogspot.in/

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

Output

Page 17: Basics of inheritance .22

17

Some Inheritance Details

• An instance of a child class does not rely on an instance of a parent class• Hence we could create a Dictionary object without

having to create a Book object first

• Inheritance is a one-way street• The Book class cannot use variables or methods

declared explicitly in the Dictionary class

http://improvejava.blogspot.in/

Page 18: Basics of inheritance .22

Summary

• In this class, we have discussed• Inheritance• Inheritance principle• Inheritance syntax• Super class• Sub class• Public, private and protected modifiers

http://improvejava.blogspot.in/

Page 19: Basics of inheritance .22

Quiz

http://improvejava.blogspot.in/

1. A class which is inherited from other class is called a) Super classb) Sub classc) Anonymous classd) Object

Page 20: Basics of inheritance .22

Quiz contd...

2. A class which used to inherits a class is called a) Sub classb) Super classc) Anonymous classd) object

http://improvejava.blogspot.in/

Page 21: Basics of inheritance .22

3. What is the keyword used to inherit a class

a) create

b) new

c) this

d) extends

http://improvejava.blogspot.in/

Quiz contd...

Page 22: Basics of inheritance .22

22

4.Public variables volatile the principle ofa) Polymorphismb) Encapsulationc) Inheritance d) Overloading

Quiz contd...

Page 23: Basics of inheritance .22

Quiz Contd..

5. Variables and methods declared with following are inherited

a) private b) public c) protected d) abstract

http://improvejava.blogspot.in/

Page 24: Basics of inheritance .22

Frequently Asked Questions• What is inheritance ?

• Mention the key word to inherit a class from others

• What is super class ?

• What is sub class ?• Write about public modifier• Write about private modifier• What protected modifier• Write a program in Java using modifiers

• Write a program in Java for inheriting a class from others

http://improvejava.blogspot.in/