1 of 30

UNIT-II�(PART-I)

2 of 30

INHERITANCE (EXTENDING AND IMPLEMENTING)

TOPICS:

  • Introduction
  • Derived Classes
  • Advantages and Types of Inheritance
  • Member Accessibility.
  • Overriding
  • Super Keyword
  • Abstract classes and Methods
  • Final Classes and Final Methods
  • Polymorphism
  • Dynamic Binding.

3 of 30

INHERITANCE

Definition :The mechanism in which one object acquires all the properties and behaviors of a parent object.

  • i.e, mechanism of Deriving a New Class from an Old Class is called Inheritance.
  • It is an Important principle in OOPs.

4 of 30

  • A Class that is Inherited(Old Class) is called a “Super Class” or “Base Class” or “Parent Class”.
  • A Class that does the inheriting(New Class) is called a “Sub Class” or “ Derived Class” or “Child Class”
  • Inheritance allows sub classes to inherit all the Variables and Methods of their Parent Class.

5 of 30

Derived Class : A Derived (Sub Class) is defined as

Syntax :

  • The extends keyword indicates that you are making a new class that derives from an existing class.
  • The meaning of "extends" is to increase the functionality.

6 of 30

Advantages of Inheritance:

  1. Code Reusability
  2. Method Overriding

Code Reusability :

  • As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class.
  • You can use the same fields and methods already defined in the previous class.

7 of 30

Eg: /* Inheritance Sample Example */

O/P : Programmer salary is:40000.0

Bonus of programmer is:10000

8 of 30

Types of Inheritances : There are various types of Inheritances in Java.

  1. Single Inheritance
  2. Multi Level Inheritance
  3. Hierarchical Inheritance
  4. Multiple Inheritance
  5. Hybrid Inheritance
  6. In java only Three types of Inheritances supported .
  7. Those are : single, multi level, hierarchical

9 of 30

10 of 30

11 of 30

12 of 30

1. Single Inheritance: Derivation of sub class from only one super class is called “ Single Inheritance “.

13 of 30

Eg-1: /* Single Inheritance Example */

O/P : barking

eating

14 of 30

Eg-2: /* Single Inheritance Example */

15 of 30

Contd…

16 of 30

Contd…

17 of 30

/* SINGLE INHERITANCE */

class A

{

int a=10;

void Adisplay()

{

System.out.println("CLASS A");

}

}

class B extends A

{

int b=20;

void Bdisplay()

{

System.out.println("CLASS A");

}

}

18 of 30

class Single

{

public static void main(String a[])

{

B obj2=new B();

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

obj2.Adisplay();

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

obj2.Bdisplay();

}

}

19 of 30

2. Multi Level Inheritance: Derivation of a class from another derived class is called “ Multi Level Inheritance “.

20 of 30

Eg-2: /* Multi Level Inheritance Example */

21 of 30

Contd…

22 of 30

/* MULTILEVEL INHERITANCE*/

class P

{

int a=10;

void display()

{

System.out.println("A");

}

}

class C extends P

{

int b=20;

void dis()

{

System.out.println("B");

}

}

23 of 30

class CChild extends C

{

int c=30;

void show()

{

System.out.println("C");

}

}

class MultilevelInherit

{

public static void main(String ar[])

{

CChild obj=new CChild(); System.out.println(obj.c); obj.show(); System.out.println(obj.b); obj.dis(); System.out.println(obj.a); obj.display();

}

}

24 of 30

3. Hierarichal Inheritance: Derivation of several classes from a Single Super Class is called “Hierarchical Inheritance”.

25 of 30

class A

{

int a=10;

void Adisplay()

{

System.out.println("CLASS A");

}

}

class B extends A

{

int b=20;

void Bdisplay()

{

System.out.println("CLASS B");

}

 

}

26 of 30

class C extends A

{

int c=30;

void Cdisplay()

{

System.out.println("CLASS C");

}

}

class Hierarchy

{

public static void main(String a[])

{

C obj3=new C();

System.out.println("obj1.a="+obj3.a); obj3.Adisplay(); System.out.println("obj2.b="+obj3.b); obj3.Bdisplay(); System.out.println("obj3.c="+obj3.c); obj3.Cdisplay();

}

}

27 of 30

SUPER KEYWORD

Definition :The super keyword in Java is a reference variable which is used to refer immediate parent class object.

  • super can be used to refer immediate parent class instance variable.
  • super can be used to invoke immediate parent class method.
  • super() can be used to invoke immediate parent class constructor.

28 of 30

29 of 30

/* SUPER KEYWORD */�

class S

{

final int i=10;

void display()

{

i=i+1;

System.out.println(i);

}

}

class C extends S

{

void display()

{

int i=20; System.out.println("Child"); System.out.println(super.i); super.display();

}

}

30 of 30

class SKeyword

{

public static void main(String ar[])

{

S obj1=new S();

Cobj2=newC(); obj1.display();

}

}