OOPS stands for
“Object Oriented Programming System”
That is, OOPS itself is not a technology, some technology or language needs to follow
the standards of in order to become Object-Oriented
Behaviour
Parent Class
Variable
s
Method
To inherit the Generic Properties
In java , Inheritance can be implemented using two keywords
Extends keyword supports only one class to inherit at a time
Class Example{
}
Class Sample extends Example{
}
class Parent{
}
int a=10;
int b=20;
void m1(){
System.out.println("m1");
}
class Child extends Parent{
}
public static void main(String args[]){
}
Child c=new Child();
System.out.println(c.a);
System.out.println(c.b);
c.m1();
Output
10
20
m1
They can ’ t be inherited
As they are allowed to access only in the in class in which they are defined
class Parent{
}
int a=10;
int b=20;
void m1(){
System.out.println("m1");
}
class Private_Var extends Parent{
}
public static void main(String args[]){
}
Child c=new Child();
System.out.println(c.a);
System.out.println(c.b);
c.m1();
privat
e
private
Nothing to worry , they can be inherited
class Parent{
}
int a=10;
int b=20;
void m1(){
System.out.println("m1");
}
class Final_Var extends Parent{
}
public static void main(String args[]){
}
Child c=new Child();
System.out.println(c.a);
System.out.println(c.b);
c.m1();
final
final
Output
10
20
m1
By Declaring it as
final class Parent{
}
int a=10;
int b=20;
void m1(){
System.out.println("m1");
}
class Final extends Parent{
}
public static void main(String args[]){
}
Child c=new Child();
System.out.println(c.a);
System.out.println(c.b);
c.m1();
Private accessibility modifier is not
allowed for a class
The only allowed accessibility
modifiers for class are
 public
 <default>
private class Parent{
}
int a=10;
int b=20;
void m1(){
System.out.println("m1");
}
class PrivateDemo extends Parent{
}
public static void main(String args[]){
}
Child c=new Child();
System.out.println(c.a);
System.out.println(c.b);
c.m1();
Types of Inheritance
A B
C D
A A
A A A
B B
B
B
B C
C
C
C
D D
D
Singl
e
Multiple Hierarchical
Multilevel
Hybrid Multi-path
In any type of inheritance, if all the classes has at most one super class then that type is
in java
A
B
A
B C DC
A
B
Single
Multilevel
Hierarchical
A
D
B A
B C
D
A
B C
D
C
Multiple Hybrid Multi-path
In any type of inheritance, if any class has more than one super class then that type is
in java
Extends keyword supports only one class to inherit at a time
When a member is accessed using subclass object , the compiler first searches In subclass , if
it is available then it should be accessed
If it is not available , then it will search in super class
If it is not available in subclass also ,then compiler gives the error
This process is known as
static variables and static blocks are identified
and then executed from super class to sub
class
Non-static variables, Non-static blocks and
constructors are identified and then executed
from super class to subclass
For executing method definition , searching starts from subclass to super class
Class loading order is from super class to subclass
Object creation order is also from super class to subclass
}
Static{
Sopln(“Super class
SB”);
}
Static{
Sopln(“Subclass SB”);
}
class Sub_Control extends
Super_Control{
Super_Control(){
Sopln(“Super
Cons”);
}
Sub_Control(){
Sopln(“Sub Cons”);
}
class Super_Control
{
{
Sopln(“Subclass
NSB”);
}
{
Sopln(“Super class
NSB”);
}
}
Public static void main(S
args[]){
sopln(“main”);
Sub_Control sb=new
Sub_Control
Super class SB
Subclass SB
main
Super class NSB
Super Cons
Subclass NSB
Sub Cons
How memory is created for super class when object is created for sub-class??
With the help of
Super()
This process continues until java.lang.Object is reached
Constructor places super(); statement in each constructor of all the sub-classes
Automatically in compilation phase
class Sup1{
Sup1(){
}
}
class Sub1 extends
Sup1{
Sub1(){
}
}
class Sup1{
Sup1(){
}
}
class Sub1 extends
Sup1{
Sub1(){
}
}
compile
r
Compiles
to
super();
super();
public class Object{
public Object(){
}
}
class First{
}
class Second extends
First{
}
compile
r
Compiles
to
class First{
}
class Second
extends First{
}
super();
First(){
}
Second(){
super();
}
Compiler places the default constructor , only when there is no explicit constructor in the class
But compiler places the super(); in all the constructors in a class
And super(); should be the first statement in the constructor
class Sup2{
Sup2(int a){
}
}
Super();
In this case the compiler can’t be place the default constructor
Let us see , some cases
class Sup3{
class Sub3 extends Sup3{
Sup3(){
super();
}
Sub3(){
super();
}
}
}
public static void main(String args[]){
Sub3 sb=new Sub3();
}
class Sup4{
Sup4(int a){
System.out.println(“int-arg”);
}
}
Class Sub4 extends Sup4{
}
Sub4(){
}
Super();
In this case the developer has two alternatives.
public static void main(String args[]){
Sub4 sb=new Sub4();
}
Developer should explicitly call the super class parameterized
constructor
class Sup5{
}
Class Sub5 extends Sup5{
}
Sub5(){
}
Super(20);
Sup5(int a){
System.out.println(“int-arg”);
}
public static void main(String args[]){
Sub5 sb=new Sub5();
}
class Sup6{
Sup6(int a){
super();
System.out.println(“int-arg”);
}
}
Class Sub6 extends Sup6{
}
Sub6(){
}
Super();
Developer must place the no-arg Constructor in super class
public static void main(String args[]){
Sub6 sb=new Sub6();
}
Sup6(){
super();
}

Inheritance in Java

  • 1.
    OOPS stands for “ObjectOriented Programming System” That is, OOPS itself is not a technology, some technology or language needs to follow the standards of in order to become Object-Oriented
  • 4.
  • 5.
    To inherit theGeneric Properties
  • 8.
    In java ,Inheritance can be implemented using two keywords Extends keyword supports only one class to inherit at a time
  • 9.
  • 10.
    class Parent{ } int a=10; intb=20; void m1(){ System.out.println("m1"); } class Child extends Parent{ } public static void main(String args[]){ } Child c=new Child(); System.out.println(c.a); System.out.println(c.b); c.m1(); Output 10 20 m1
  • 11.
    They can ’t be inherited As they are allowed to access only in the in class in which they are defined
  • 12.
    class Parent{ } int a=10; intb=20; void m1(){ System.out.println("m1"); } class Private_Var extends Parent{ } public static void main(String args[]){ } Child c=new Child(); System.out.println(c.a); System.out.println(c.b); c.m1(); privat e private
  • 13.
    Nothing to worry, they can be inherited
  • 14.
    class Parent{ } int a=10; intb=20; void m1(){ System.out.println("m1"); } class Final_Var extends Parent{ } public static void main(String args[]){ } Child c=new Child(); System.out.println(c.a); System.out.println(c.b); c.m1(); final final Output 10 20 m1
  • 15.
  • 16.
    final class Parent{ } inta=10; int b=20; void m1(){ System.out.println("m1"); } class Final extends Parent{ } public static void main(String args[]){ } Child c=new Child(); System.out.println(c.a); System.out.println(c.b); c.m1();
  • 17.
    Private accessibility modifieris not allowed for a class The only allowed accessibility modifiers for class are  public  <default>
  • 18.
    private class Parent{ } inta=10; int b=20; void m1(){ System.out.println("m1"); } class PrivateDemo extends Parent{ } public static void main(String args[]){ } Child c=new Child(); System.out.println(c.a); System.out.println(c.b); c.m1();
  • 19.
  • 20.
    A B C D AA A A A B B B B B C C C C D D D Singl e Multiple Hierarchical Multilevel Hybrid Multi-path
  • 21.
    In any typeof inheritance, if all the classes has at most one super class then that type is in java A B A B C DC A B Single Multilevel Hierarchical
  • 22.
    A D B A B C D A BC D C Multiple Hybrid Multi-path In any type of inheritance, if any class has more than one super class then that type is in java
  • 23.
    Extends keyword supportsonly one class to inherit at a time
  • 24.
    When a memberis accessed using subclass object , the compiler first searches In subclass , if it is available then it should be accessed If it is not available , then it will search in super class If it is not available in subclass also ,then compiler gives the error This process is known as
  • 25.
    static variables andstatic blocks are identified and then executed from super class to sub class Non-static variables, Non-static blocks and constructors are identified and then executed from super class to subclass For executing method definition , searching starts from subclass to super class Class loading order is from super class to subclass Object creation order is also from super class to subclass
  • 26.
    } Static{ Sopln(“Super class SB”); } Static{ Sopln(“Subclass SB”); } classSub_Control extends Super_Control{ Super_Control(){ Sopln(“Super Cons”); } Sub_Control(){ Sopln(“Sub Cons”); } class Super_Control { { Sopln(“Subclass NSB”); } { Sopln(“Super class NSB”); } } Public static void main(S args[]){ sopln(“main”); Sub_Control sb=new Sub_Control
  • 27.
    Super class SB SubclassSB main Super class NSB Super Cons Subclass NSB Sub Cons
  • 28.
    How memory iscreated for super class when object is created for sub-class?? With the help of Super()
  • 29.
    This process continuesuntil java.lang.Object is reached Constructor places super(); statement in each constructor of all the sub-classes Automatically in compilation phase
  • 30.
    class Sup1{ Sup1(){ } } class Sub1extends Sup1{ Sub1(){ } } class Sup1{ Sup1(){ } } class Sub1 extends Sup1{ Sub1(){ } } compile r Compiles to super(); super(); public class Object{ public Object(){ } }
  • 32.
    class First{ } class Secondextends First{ } compile r Compiles to class First{ } class Second extends First{ } super(); First(){ } Second(){ super(); }
  • 33.
    Compiler places thedefault constructor , only when there is no explicit constructor in the class But compiler places the super(); in all the constructors in a class And super(); should be the first statement in the constructor
  • 34.
    class Sup2{ Sup2(int a){ } } Super(); Inthis case the compiler can’t be place the default constructor
  • 35.
    Let us see, some cases
  • 36.
    class Sup3{ class Sub3extends Sup3{ Sup3(){ super(); } Sub3(){ super(); } } } public static void main(String args[]){ Sub3 sb=new Sub3(); }
  • 37.
    class Sup4{ Sup4(int a){ System.out.println(“int-arg”); } } ClassSub4 extends Sup4{ } Sub4(){ } Super(); In this case the developer has two alternatives. public static void main(String args[]){ Sub4 sb=new Sub4(); }
  • 38.
    Developer should explicitlycall the super class parameterized constructor class Sup5{ } Class Sub5 extends Sup5{ } Sub5(){ } Super(20); Sup5(int a){ System.out.println(“int-arg”); } public static void main(String args[]){ Sub5 sb=new Sub5(); }
  • 39.
    class Sup6{ Sup6(int a){ super(); System.out.println(“int-arg”); } } ClassSub6 extends Sup6{ } Sub6(){ } Super(); Developer must place the no-arg Constructor in super class public static void main(String args[]){ Sub6 sb=new Sub6(); } Sup6(){ super(); }