Вы находитесь на странице: 1из 20

Pradnya sadigale

Contents
• Introduction of Inheritance 3
• Classes classification 4
 Super class
 Intermediate class
 Child class
• Relation between classes 5
• Super class 6
• Intermediate class 7
• Child class 8
• Types of Inheritance 9
 Single Inheritance
 Multilevel Inheritance
 Hierarchical Inheritance
• Single Inheritance 10
• Multilevel Inheritance 11
• Hierarchical Inheritance 12
• Indirect Mechanism of Inheritance 13
• A program demonstrating inheritance 14
• Limitations 15
INHERITANCE
• One of the most effective features of Oop’s paradigm.

• Establish a link/connectivity between 2 or more


classes.

• Permits sharing and accessing properties from one to


another class.

• to establish this relation Java uses ‘extends’ keyword.


What is Inheritance?
• Inheritance is a mechanism in which one class
acquires the property of another class. For
example, a child inherits the traits of his/her
parents. With inheritance, we can reuse the
fields and methods of the existing class.
Hence, inheritance facilitates Reusability and
is an important.The keyward extends is used
by sub class to inherit the features of super
class
Java Inheritance Syntax:

class subClass extends superClass


{
//methods and fields
}
Category of Classes on the Basis of
Inheritance
Super class
(base/parent/driver/inheritance/
ancestor class).

Intermediate class
(mediating/dual class).

Child class
(sub/associate/derived/inherited class).
Relation between classes
Super class
Top located class

Service provider
(its properties accessed by all its lower level
class).
Intermediate class
Middle located class

Having Dual policy


(obtain properties of upper level class
and transmit properties to lower level
class).
Child class
Bottom located class
much benefitted class
much loaded class
properties of child class as well as
class and parent class can be accessed by
only the object of child class.
Inheritance Example
class Doctor {
void Doctor_Details() {
System.out.println("Doctor Details...");
}
}

class Surgeon extends Doctor {


void Surgeon_Details() {
System.out.println("Surgen Detail...");
}
}

public class Hospital {


public static void main(String args[]) {
Surgeon s = new Surgeon();
s.Doctor_Details();
s.Surgeon_Details();
}
}
TYPES of INHERITANCE
• Single Inheritance

• Multilevel Inheritance

• Hierarchical Inheritance
Single Inheritance
• A structure having one and only one parent as well
as child class.

• Child class is authorized to access the property of


Parent class. Syntax :
Syntax :
Multilevel Inheritance

• Standard structure of Single


Inheritance having one Parent,
one or more intermediate and
one child classes.

• Child class as well as intermediate


class may access the properties of
upper level classes.
Hierarchical Inheritance Syntax :

A structure having one parent


and more child class.

Child classes must be connected


with only Parent class.
Java Supports a special feature called interface.
This feature helps to connect a class with more
than one classes.
For this type of connectivity java uses ‘implements’
keyword. Syntax :
interface A{
……..}
Interface B {
____}
class M {
-------}
class N implements A,B extends
M{ =====
_____------…………}
A Program demonstrating Inheritance in
Java
import java.util.*;
import java.io.*;
interface Prn1{
Output :
void Bits(int x);}
interface Prn2{
void Bytes();}
16 is not modulated by 3.
class Prn3{
void Mega(){
int x=5,y,i=1;
long z=1;
while(i<=10){
y=x*i;
Enter a no. for a rectangle width : 20
i++;
//System.out.println(y);
z=z*y; }
System.out.println("Product of Table of 5 : "+z);}}
class RPT extends Prn3 implements Prn2{
int x,y,z;
void Bits(int a){
Area of rectangle : 10x20=200 Product
if(a%3==0)
System.out.println("\n\n\n"+a+" is modulated by 3.\n\n"); of table of 5 : 35437500000000
else
System.out.println("\n\n\n"+a+" is not modulated by 3.\n\n");}
public void Bytes(){
Scanner S=new Scanner(System.in);
x=10;
System.out.print("Enter a no. for a rectangle width : ");
y=S.nextInt();
z=x*y;
System.out.println("\n\nArea of rectangle : "+x+"x"+y+"="+z);
System.out.println();}}
class Intrfc{
public static void main(String[] Arg){
RPT obj=new RPT();
obj.Bits(16);
obj.Bytes();
obj.Mega();}}
• Link is establish into single direction(Fig).

Java not support Multiple


inheritance as well as
Hybrid inheritance.

The extends keyword permits


to connect a class with only one class.

In Interface, properties are only declared and assined,


but n’ver defined.
Super Keyword
1. The super keyword is similar to "this" keyword.
2. The keyword super can be used to access any data member or methods of the parent
class.
3. Super keyword can be used at variable, method and constructor level.

Syntax:

super.<method-name>();
THANK YOU

Вам также может понравиться