Java Inheritance
After learning
you should be
able to do..
Introduction
About inheritance
Types of inheritance
And its programs.
2
Introduction to Inheritance..
Presentation title 3
Definition
• When you construct a new class from existing class in such a way that the
new class access all the features and properties of existing class is called
inheritance.
• In java extends keyword is used to perform inheritance.
• It provides code reusability.
• We can’t access private members of class through inheritance.
• A subclass contains all the features of superclass. So, we should create the
object of subclass.
• Method overriding only possible through inheritance.
4
SYNTAX FOR USING INHERITANCE
Class A
{
________________ //code
_____________
____
}
class B extends A //subclass
{
_____________ //code
____________
} 6
Types of inheritance
1. Simple
inheritance
Simple inheritance is nothing but which contain only one super class and only one sub class.
Syntax- class Super
{
____________
____________
}
Class Sub extends Super
{
______________
_____________
}
8
9
• Class Student
• {
• int roll , marks;
• String name;
• void input()
• {
• System . out . println (“enter roll , name , and marks”);
• }
• }
• Class Ankit extends Student //sub class
• {
• Void disp ()
• {
• Roll=1; name=“Rahul”; marks=89;
• System . out . println (“roll+” ”+ name+” ”+marks”);
• }
• public static void main(String [] args)
• {
• Ankit r=new Ankit();
• r . input();
• r . disp();
• }
• }
If we declare method or data member as private,
it will show error because private member can only
access only inside same class but not in other class object
. But in inheritance declare as public or protected to
access it easily.
Presentation title
Multilevel Inheritance
In multilevel inheritance , we have only one superclass and multiple sub classes.
Presentation title 11
Syntax for multilevel
inheritance
Presentation title 12
• Class Super
• {
• __________
• ___________
• ______________
• }
• class sub1 extends Super
• {
• _______
• __________
• }
• class sub2 extends sub1
• {
• __________
• _______________
• }
• class A
• {
• int a , b , c ;
• void add()
• {
• a=10 ; b=20 ;
• c=a+b;
• System.out.println(“sum of two numbers”+c);
• }
• void sub()
• {
13
• a=200;b=100;
• c=a-b;
• System.out.println(“sub of two numbers”+c);
• }}
• class B extends A
• {
• void multi()
• {
• a=10;b=20;
• C=a*b;
• System.out.println(“multiplication of two numbers
”+c);
• }
• void div()
• {
• a=10;b=2;
• c=a/b;
• System.out.println(“division of the two numbers”+c);
• } }
• class C extends B
• {
• void rem()
• {
• A=10;b=3;
• C=a%b;
• System.out.println(“remainder of two numbers”+c);
• } }
• Class Test
• {
• Public static void main(String[]args)
• {
• C r=new C;
• r . add(); r . sub(); r . multi(); r . div(); r . rem();
• } }
14
Multiple inheritance
whenever a sub class wants to inherit the property of two or more
superclasses that have same method , java compiler can’t decide which class
method it should inherit . Then their might be a chance of memory
duplication i.e a reason java doesn’t support multiple inheritance through
classes.
Presentation title 15
Hierchical
inheritance
A inheritance which contain only one superclass and multiple
subclass and all sub class directly extends super class is called
hierchical inheritance.
SYNTAX-class A
{
____________
_______________ //code
}
Class B extends A
{
____________
_______________ //code
}
Class C extends A {
___________________ //code
}
• class A
• {
• void input()
• {
• System.out.println(“enter your name”);
• } }
• class B extends A
• {
• void show()
• {
• System.out.println(“my name is Ankit”);
• } }
• class C extends A
• {
• void disp()
• {
• System.out.println(“my name is
Ankush”);
• } }
• class Demo
• {
• Public static void main(String[]args)
• {
• B r=new B();
• C r2=new C();
• r.input();
• r.show();
• r2.input();
• r2.disp();
• }
• }
Presentation title 17

Java Inheritance.pdf of java in computer language

  • 1.
  • 2.
    After learning you shouldbe able to do.. Introduction About inheritance Types of inheritance And its programs. 2
  • 3.
  • 4.
    Definition • When youconstruct a new class from existing class in such a way that the new class access all the features and properties of existing class is called inheritance. • In java extends keyword is used to perform inheritance. • It provides code reusability. • We can’t access private members of class through inheritance. • A subclass contains all the features of superclass. So, we should create the object of subclass. • Method overriding only possible through inheritance. 4
  • 5.
    SYNTAX FOR USINGINHERITANCE
  • 6.
    Class A { ________________ //code _____________ ____ } classB extends A //subclass { _____________ //code ____________ } 6
  • 7.
  • 8.
    1. Simple inheritance Simple inheritanceis nothing but which contain only one super class and only one sub class. Syntax- class Super { ____________ ____________ } Class Sub extends Super { ______________ _____________ } 8
  • 9.
    9 • Class Student •{ • int roll , marks; • String name; • void input() • { • System . out . println (“enter roll , name , and marks”); • } • } • Class Ankit extends Student //sub class • { • Void disp () • { • Roll=1; name=“Rahul”; marks=89; • System . out . println (“roll+” ”+ name+” ”+marks”); • } • public static void main(String [] args) • { • Ankit r=new Ankit(); • r . input(); • r . disp(); • } • }
  • 10.
    If we declaremethod or data member as private, it will show error because private member can only access only inside same class but not in other class object . But in inheritance declare as public or protected to access it easily. Presentation title
  • 11.
    Multilevel Inheritance In multilevelinheritance , we have only one superclass and multiple sub classes. Presentation title 11
  • 12.
    Syntax for multilevel inheritance Presentationtitle 12 • Class Super • { • __________ • ___________ • ______________ • } • class sub1 extends Super • { • _______ • __________ • } • class sub2 extends sub1 • { • __________ • _______________ • }
  • 13.
    • class A •{ • int a , b , c ; • void add() • { • a=10 ; b=20 ; • c=a+b; • System.out.println(“sum of two numbers”+c); • } • void sub() • { 13
  • 14.
    • a=200;b=100; • c=a-b; •System.out.println(“sub of two numbers”+c); • }} • class B extends A • { • void multi() • { • a=10;b=20; • C=a*b; • System.out.println(“multiplication of two numbers ”+c); • } • void div() • { • a=10;b=2; • c=a/b; • System.out.println(“division of the two numbers”+c); • } } • class C extends B • { • void rem() • { • A=10;b=3; • C=a%b; • System.out.println(“remainder of two numbers”+c); • } } • Class Test • { • Public static void main(String[]args) • { • C r=new C; • r . add(); r . sub(); r . multi(); r . div(); r . rem(); • } } 14
  • 15.
    Multiple inheritance whenever asub class wants to inherit the property of two or more superclasses that have same method , java compiler can’t decide which class method it should inherit . Then their might be a chance of memory duplication i.e a reason java doesn’t support multiple inheritance through classes. Presentation title 15
  • 16.
    Hierchical inheritance A inheritance whichcontain only one superclass and multiple subclass and all sub class directly extends super class is called hierchical inheritance. SYNTAX-class A { ____________ _______________ //code } Class B extends A { ____________ _______________ //code } Class C extends A { ___________________ //code }
  • 17.
    • class A •{ • void input() • { • System.out.println(“enter your name”); • } } • class B extends A • { • void show() • { • System.out.println(“my name is Ankit”); • } } • class C extends A • { • void disp() • { • System.out.println(“my name is Ankush”); • } } • class Demo • { • Public static void main(String[]args) • { • B r=new B(); • C r2=new C(); • r.input(); • r.show(); • r2.input(); • r2.disp(); • } • } Presentation title 17