SlideShare a Scribd company logo
1 of 39
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();
}

More Related Content

What's hot

Lecture 3 java basics
Lecture 3 java basicsLecture 3 java basics
Lecture 3 java basicsthe_wumberlog
 
Understanding Java Dynamic Proxies
Understanding Java Dynamic ProxiesUnderstanding Java Dynamic Proxies
Understanding Java Dynamic ProxiesRafael Luque Leiva
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Javabackdoor
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Kernel Training
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
Unit2 java
Unit2 javaUnit2 java
Unit2 javamrecedu
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism JavaM. Raihan
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 InterfaceOUM SAOKOSAL
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For SyntaxPravinYalameli
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Javaparag
 

What's hot (20)

Lecture 3 java basics
Lecture 3 java basicsLecture 3 java basics
Lecture 3 java basics
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Java Class Loading
Java Class LoadingJava Class Loading
Java Class Loading
 
Understanding Java Dynamic Proxies
Understanding Java Dynamic ProxiesUnderstanding Java Dynamic Proxies
Understanding Java Dynamic Proxies
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Java Classloaders
Java ClassloadersJava Classloaders
Java Classloaders
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Java Class Loader
Java Class LoaderJava Class Loader
Java Class Loader
 
Unit2 java
Unit2 javaUnit2 java
Unit2 java
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
inheritance
inheritanceinheritance
inheritance
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
 
C progrmming
C progrmmingC progrmming
C progrmming
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Java
 

Viewers also liked

Viewers also liked (9)

Código
CódigoCódigo
Código
 
Toss_Up
Toss_UpToss_Up
Toss_Up
 
santana mallick-resume
santana mallick-resumesantana mallick-resume
santana mallick-resume
 
FewWorkHighlights
FewWorkHighlightsFewWorkHighlights
FewWorkHighlights
 
Suggestion To GLASS MANAGEMENT
Suggestion To GLASS MANAGEMENTSuggestion To GLASS MANAGEMENT
Suggestion To GLASS MANAGEMENT
 
"행동변화를 위한 보건사업 기획모델"
"행동변화를 위한 보건사업 기획모델" "행동변화를 위한 보건사업 기획모델"
"행동변화를 위한 보건사업 기획모델"
 
Tugas makalah bahasa indonesia
Tugas makalah bahasa indonesiaTugas makalah bahasa indonesia
Tugas makalah bahasa indonesia
 
Mousumi Haldar - Resume
Mousumi Haldar - ResumeMousumi Haldar - Resume
Mousumi Haldar - Resume
 
真剣10代学び場
真剣10代学び場真剣10代学び場
真剣10代学び場
 

Similar to Inheritance in Java

SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxRudranilDas11
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritanceteach4uin
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritanceteach4uin
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interfaceShubham Sharma
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance SlidesAhsan Raja
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING ManpreetSingh1387
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaAriful Islam
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2Sherihan Anver
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satyaSatya Johnny
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Abid Kohistani
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritanceKalai Selvi
 

Similar to Inheritance in Java (20)

Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interface
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 

Inheritance in Java

  • 1. 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
  • 2.
  • 3.
  • 5. To inherit the Generic Properties
  • 6.
  • 7.
  • 8. In java , Inheritance can be implemented using two keywords Extends keyword supports only one class to inherit at a time
  • 9. Class Example{ } Class Sample extends Example{ }
  • 10. 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
  • 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; 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
  • 13. Nothing to worry , they can be inherited
  • 14. 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
  • 16. 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();
  • 17. Private accessibility modifier is not allowed for a class The only allowed accessibility modifiers for class are  public  <default>
  • 18. 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();
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. Extends keyword supports only one class to inherit at a time
  • 24. 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
  • 25. 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
  • 26. } 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
  • 27. Super class SB Subclass SB main Super class NSB Super Cons Subclass NSB Sub Cons
  • 28. How memory is created for super class when object is created for sub-class?? With the help of Super()
  • 29. 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
  • 30. 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(){ } }
  • 31.
  • 32. class First{ } class Second extends First{ } compile r Compiles to class First{ } class Second extends First{ } super(); First(){ } Second(){ super(); }
  • 33. 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
  • 34. class Sup2{ Sup2(int a){ } } Super(); In this case the compiler can’t be place the default constructor
  • 35. Let us see , some cases
  • 36. class Sup3{ class Sub3 extends 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”); } } Class Sub4 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 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(); }
  • 39. 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(); }