SlideShare une entreprise Scribd logo
1  sur  25
INHERITANCE BASICS
1. Reusability is achieved by INHERITANCE
2. Java classes Can be Reused by extending a class. Extending
an existing class is nothing but reusing properties of the
existing classes.
3. The class whose properties are extended is known as super
or base or parent class.
4. The class which extends the properties of super class is
known as sub or derived or child class
5. A class can either extends another class or can implement an
interface
A
B
class B extends A { ….. }
A super class
B sub class
A
B
<<class>>
<<class>>
<<class>>
<<interface>>class B implements A { ….. }
A interface
B sub class
Various Forms of Inheritance
A
B
Single Inheritance
A
B
Hierarchical Inheritance
X
A B C
X
A B C
MultiLevel Inheritance
A
B
C
A
B
C
A B
C
Multiple Inheritance
NOT SUPPORTED BY JAVA
A B
C
SUPPORTED BY JAVA
Forms of Inheritance
• Mulitiple Inheritance can be implemented by
implementing multiple interfaces not by extending
multiple classes
Example :
class Z extends A implements C , D
{ …………}
OK
class Z extends A ,B class Z extends A extends B
{ {
OR
} }
A C D
Z
WRONG WRONG
Defining a Subclass
Syntax :
class <subclass name> extends <superclass name>
{
variable declarations;
method declarations;
}
• Extends keyword signifies that properties of the super
class are extended to sub class
• Sub class will not inherit private members of super class
Access Control
Access Modifiers
Access Location
public protected friendly private
Same Class Yes Yes Yes Yes
sub classes in same
package
Yes Yes Yes No
Other Classes in
Same package
Yes Yes Yes No
Subclasses in other
packages
Yes Yes No No
Non-subclasses in
other packages
Yes No No No
1. Whenever a sub class object is created ,super class
constructor is called first.
2. If super class constructor does not have any
constructor of its own OR has an unparametrized
constructor then it is automatically called by Java Run
Time by using call super()
3. If a super class has a parameterized constructor then it
is the responsibility of the sub class constructor to call
the super class constructor by call
super(<parameters required by super class>)
4. Call to super class constructor must be the first
statement in sub class constructor
Inheritance Basics
Inheritance Basics
When super class has a Unparametrized constructor
class A
{
A()
{
System.out.println("This is constructor of class A");
}
} // End of class A
class B extends A
{
B()
{
super();
System.out.println("This is constructor of class B");
}
} // End of class B
Optional
Cont…..
class inhtest
{
public static void main(String args[])
{
B b1 = new B();
}
}
OUTPUT
This is constructor of class A
This is constructor of class B
class A
{
A()
{
System.out.println("This is class A");
}
}
class B extends A
{
B()
{
System.out.println("This is class B");
}
}
class inherit1
{
public static void main(String args[])
{
B b1 = new B();
}
}
/*
E:Java>java inherit1
This is class A
This is class B
E:Java>
*/
File Name is xyz.java
class A
{
private A()
{
System.out.println("This is class A");
}
}
class B extends A
{
B()
{
System.out.println("This is class B");
}
}
class inherit2
{
public static void main(String args[])
{
B b1 = new B();
}
}
/*
E:Java>javac xyz1.java
xyz1.java:12: A() has private access
in A
{
^
1 error
class A
{
private A()
{
System.out.println("This is class A");
}
A()
{
System.out.println("This is class A");
}
}
class B extends A
{
B()
{
System.out.println("This is class B");
}
}
class inherit2
{
public static void main(String args[])
{
B b1 = new B();
} }
/*
E:Java>javac xyz2.java
xyz2.java:7: A() is already defined in
A
A()
^
xyz2.java:16: A() has private access
in A
{
^
2 errors
*/
When Super class has a parametrized constructor.
class A
{
private int a;
A( int a)
{
this.a =a;
System.out.println("This is constructor of class A");
}
}
class B extends A
{
private int b;
private double c;
B(int b,double c)
{
this.b=b;
this.c=c;
System.out.println("This is constructor of class B");
}
}
D:javabin>javac
inhtest.java
inhtest.java:15: cannot find
symbol
symbol : constructor A()
location: class A
{
^
1 errors
B b1 = new B(10,8.6);
class A
{
private int a;
A( int a)
{
this.a =a;
System.out.println("This is
constructor of class A");
} }
class B extends A
{
private int b;
private double c;
B(int a,int b,double c)
{
super(a);
this.b=b;
this.c=c;
System.out.println("This is
constructor of class B");
} }
B b1 = new B(8,10,8.6);
OUTPUT
This is constructor of class A
This is constructor of class B
class A
{
private int a;
protected String name;
A(int a, String n)
{
this.a = a;
this.name = n;
}
void print()
{
System.out.println("a="+a);
}
}
class B extends A
{
int b;
double c;
B(int a,String n,int b,double c)
{
super(a,n);
this.b=b;
this.c =c;
}
void show()
{
//System.out.println("a="+a);
print();
System.out.println("name="+name);
System.out.println("b="+b);
System.out.println("c="+c);
}
}
a is private in
class A
Call to print()
from super
class A Accessing name field from
super class (super.name)
class xyz3
{
public static void main(String args[])
{
B b1 = new B(10,"OOP",8,10.56);
b1.show();
}
}
E:Java>java xyz3
a=10
name=OOP
b=8
c=10.56
USE OF super KEYWORD
• Can be used to call super class constrctor
super();
super(<parameter-list>);
• Can refer to super class instance
variables/Methods
super.<super class instance variable/Method>
class A
{
private int a;
A( int a)
{
this.a =a;
System.out.println("This is constructor
of class A");
}
void print()
{
System.out.println("a="+a);
}
void display()
{
System.out.println("hello This is Display
in A");
}
} // End of class A
class B extends A
{
private int b;
private double c;
B(int a,int b,double c)
{
super(a);
this.b=b;
this.c=c;
System.out.println("This is constructor
of class B");
}
void show()
{
print();
System.out.println("b="+b);
System.out.println("c="+c);
}
} // End of class B
class inhtest1
{
public static void main(String args[])
{
B b1 = new B(10,8,4.5);
b1.show();
}
}
/* OutPUt
D:javabin>java inhtest1
This is constructor of class A
This is constructor of class B
a=10
b=8
c=4.5
*/
class A
{
private int a;
A( int a)
{
this.a =a;
System.out.println("This is constructor
of class A");
}
void show()
{
System.out.println("a="+a);
}
void display()
{
System.out.println("hello This is Display
in A");
}
}
class B extends A
{
private int b;
private double c;
B(int a,int b,double c)
{
super(a);
this.b=b;
this.c=c;
System.out.println("This is constructor
of class B");
}
void show()
{
super.show();
System.out.println("b="+b);
System.out.println("c="+c);
display();
}
}
class inhtest1
{
public static void main(String args[])
{
B b1 = new B(10,8,4.5);
b1.show();
}
}
/* OutPut
D:javabin>java inhtest1
This is constructor of class A
This is constructor of class B
a=10
b=8
c=4.5
hello This is Display in A
*/
class A
{
int a;
A( int a)
{ this.a =a; }
void show()
{
System.out.println("a="+a);
}
void display()
{
System.out.println("hello This is Display
in A");
}
}
class B extends A
{
int b;
double c;
B(int a,int b,double c)
{
super(a);
this.b=b;
this.c=c;
}
void show()
{
//super.show();
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
}
}
class inhtest2
{
public static void main(String args[])
{
B b1 = new B(10,20,8.4);
b1.show();
}
}
/*
D:javabin>java inhtest2
a=10
b=20
c=8.4
*/
class A
{
int a;
A( int a)
{ this.a =a; }
}
class B extends A
{
// super class variable a hides here
int a;
int b;
double c;
B(int a,int b,double c)
{
super(100);
this.a = a;
this.b=b;
this.c=c;
}
void show()
{
System.out.println("Super class a="+super.a);
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
}
}
class inhtest2
{
public static void main(String args[])
{
B b1 = new B(10,20,8.4);
b1.show();
}
}
/* Out Put
D:javabin>java inhtest2
Super class a=100
a=10
b=20
c=8.4
*/

Contenu connexe

Tendances

Unit3 packages &amp; interfaces
Unit3 packages &amp; interfacesUnit3 packages &amp; interfaces
Unit3 packages &amp; interfacesKalai Selvi
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfacesmadhavi patil
 
Lecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVMLecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVMmanish kumar
 
Core Java- An advanced review of features
Core Java- An advanced review of featuresCore Java- An advanced review of features
Core Java- An advanced review of featuresvidyamittal
 
Interfaces in JAVA !! why??
Interfaces in JAVA !!  why??Interfaces in JAVA !!  why??
Interfaces in JAVA !! why??vedprakashrai
 
Java non access modifiers
Java non access modifiersJava non access modifiers
Java non access modifiersSrinivas Reddy
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiersKhaled Adnan
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5Sayed Ahmed
 
Reflection in java
Reflection in javaReflection in java
Reflection in javaupen.rockin
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)Prof. Erwin Globio
 
Java Interview Questions Answers Guide
Java Interview Questions Answers GuideJava Interview Questions Answers Guide
Java Interview Questions Answers GuideDaisyWatson5
 
Java Reflection Explained Simply
Java Reflection Explained SimplyJava Reflection Explained Simply
Java Reflection Explained SimplyCiaran McHale
 
Introduction to java
Introduction to  javaIntroduction to  java
Introduction to javaKalai Selvi
 

Tendances (20)

Unit3 packages &amp; interfaces
Unit3 packages &amp; interfacesUnit3 packages &amp; interfaces
Unit3 packages &amp; interfaces
 
Access modifiers
Access modifiersAccess modifiers
Access modifiers
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
OOP
OOPOOP
OOP
 
Inheritance
InheritanceInheritance
Inheritance
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Lecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVMLecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVM
 
Core Java- An advanced review of features
Core Java- An advanced review of featuresCore Java- An advanced review of features
Core Java- An advanced review of features
 
Interfaces in JAVA !! why??
Interfaces in JAVA !!  why??Interfaces in JAVA !!  why??
Interfaces in JAVA !! why??
 
Java non access modifiers
Java non access modifiersJava non access modifiers
Java non access modifiers
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Java interface
Java interfaceJava interface
Java interface
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
 
Reflection in java
Reflection in javaReflection in java
Reflection in java
 
Java reflection
Java reflectionJava reflection
Java reflection
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
 
Java Interview Questions Answers Guide
Java Interview Questions Answers GuideJava Interview Questions Answers Guide
Java Interview Questions Answers Guide
 
Java Reflection Explained Simply
Java Reflection Explained SimplyJava Reflection Explained Simply
Java Reflection Explained Simply
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Introduction to java
Introduction to  javaIntroduction to  java
Introduction to java
 

En vedette (9)

Visie op Sectoren Retail 2012
Visie op Sectoren Retail 2012Visie op Sectoren Retail 2012
Visie op Sectoren Retail 2012
 
Erin Gallagher - Hawker College
Erin Gallagher - Hawker CollegeErin Gallagher - Hawker College
Erin Gallagher - Hawker College
 
Egypt Religion
Egypt   ReligionEgypt   Religion
Egypt Religion
 
Talmid
TalmidTalmid
Talmid
 
Cory McDonald - Callaghan College Waratah
Cory McDonald - Callaghan College Waratah Cory McDonald - Callaghan College Waratah
Cory McDonald - Callaghan College Waratah
 
Nitro Max Presentation
Nitro Max PresentationNitro Max Presentation
Nitro Max Presentation
 
Belinda Guidice - Merrylands High School
Belinda Guidice - Merrylands High SchoolBelinda Guidice - Merrylands High School
Belinda Guidice - Merrylands High School
 
Bruce Stavert - DERNSW Literature Review
Bruce Stavert - DERNSW Literature ReviewBruce Stavert - DERNSW Literature Review
Bruce Stavert - DERNSW Literature Review
 
Reabilitacion pfp
Reabilitacion pfpReabilitacion pfp
Reabilitacion pfp
 

Similaire à Lecture 14 (inheritance basics) (20)

Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Java tutoria part 2
Java tutoria part 2Java tutoria part 2
Java tutoria part 2
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interface
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
 
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Inheritance
InheritanceInheritance
Inheritance
 
Java tutorial part 2
Java tutorial part 2Java tutorial part 2
Java tutorial part 2
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Class loader basic
Class loader basicClass loader basic
Class loader basic
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 

Plus de Abhishek Khune (16)

07 java collection
07 java collection07 java collection
07 java collection
 
Clanguage
ClanguageClanguage
Clanguage
 
Java Notes
Java NotesJava Notes
Java Notes
 
Threads
ThreadsThreads
Threads
 
Sorting
SortingSorting
Sorting
 
Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01Slide8appletv2 091028110313-phpapp01
Slide8appletv2 091028110313-phpapp01
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Week0 introduction
Week0 introductionWeek0 introduction
Week0 introduction
 
Binary trees
Binary treesBinary trees
Binary trees
 
Applets
AppletsApplets
Applets
 
Clanguage
ClanguageClanguage
Clanguage
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Java unit3
Java unit3Java unit3
Java unit3
 
Java unit2
Java unit2Java unit2
Java unit2
 
Linux introduction
Linux introductionLinux introduction
Linux introduction
 
Shared memory
Shared memoryShared memory
Shared memory
 

Dernier

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 

Dernier (20)

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 

Lecture 14 (inheritance basics)

  • 1. INHERITANCE BASICS 1. Reusability is achieved by INHERITANCE 2. Java classes Can be Reused by extending a class. Extending an existing class is nothing but reusing properties of the existing classes. 3. The class whose properties are extended is known as super or base or parent class. 4. The class which extends the properties of super class is known as sub or derived or child class 5. A class can either extends another class or can implement an interface
  • 2. A B class B extends A { ….. } A super class B sub class A B <<class>> <<class>> <<class>> <<interface>>class B implements A { ….. } A interface B sub class
  • 3. Various Forms of Inheritance A B Single Inheritance A B Hierarchical Inheritance X A B C X A B C MultiLevel Inheritance A B C A B C A B C Multiple Inheritance NOT SUPPORTED BY JAVA A B C SUPPORTED BY JAVA
  • 4. Forms of Inheritance • Mulitiple Inheritance can be implemented by implementing multiple interfaces not by extending multiple classes Example : class Z extends A implements C , D { …………} OK class Z extends A ,B class Z extends A extends B { { OR } } A C D Z WRONG WRONG
  • 5. Defining a Subclass Syntax : class <subclass name> extends <superclass name> { variable declarations; method declarations; } • Extends keyword signifies that properties of the super class are extended to sub class • Sub class will not inherit private members of super class
  • 6. Access Control Access Modifiers Access Location public protected friendly private Same Class Yes Yes Yes Yes sub classes in same package Yes Yes Yes No Other Classes in Same package Yes Yes Yes No Subclasses in other packages Yes Yes No No Non-subclasses in other packages Yes No No No
  • 7. 1. Whenever a sub class object is created ,super class constructor is called first. 2. If super class constructor does not have any constructor of its own OR has an unparametrized constructor then it is automatically called by Java Run Time by using call super() 3. If a super class has a parameterized constructor then it is the responsibility of the sub class constructor to call the super class constructor by call super(<parameters required by super class>) 4. Call to super class constructor must be the first statement in sub class constructor Inheritance Basics
  • 8. Inheritance Basics When super class has a Unparametrized constructor class A { A() { System.out.println("This is constructor of class A"); } } // End of class A class B extends A { B() { super(); System.out.println("This is constructor of class B"); } } // End of class B Optional Cont…..
  • 9. class inhtest { public static void main(String args[]) { B b1 = new B(); } } OUTPUT This is constructor of class A This is constructor of class B
  • 10. class A { A() { System.out.println("This is class A"); } } class B extends A { B() { System.out.println("This is class B"); } } class inherit1 { public static void main(String args[]) { B b1 = new B(); } } /* E:Java>java inherit1 This is class A This is class B E:Java> */ File Name is xyz.java
  • 11. class A { private A() { System.out.println("This is class A"); } } class B extends A { B() { System.out.println("This is class B"); } } class inherit2 { public static void main(String args[]) { B b1 = new B(); } } /* E:Java>javac xyz1.java xyz1.java:12: A() has private access in A { ^ 1 error
  • 12. class A { private A() { System.out.println("This is class A"); } A() { System.out.println("This is class A"); } } class B extends A { B() { System.out.println("This is class B"); } } class inherit2 { public static void main(String args[]) { B b1 = new B(); } } /* E:Java>javac xyz2.java xyz2.java:7: A() is already defined in A A() ^ xyz2.java:16: A() has private access in A { ^ 2 errors */
  • 13. When Super class has a parametrized constructor. class A { private int a; A( int a) { this.a =a; System.out.println("This is constructor of class A"); } } class B extends A { private int b; private double c; B(int b,double c) { this.b=b; this.c=c; System.out.println("This is constructor of class B"); } } D:javabin>javac inhtest.java inhtest.java:15: cannot find symbol symbol : constructor A() location: class A { ^ 1 errors B b1 = new B(10,8.6);
  • 14. class A { private int a; A( int a) { this.a =a; System.out.println("This is constructor of class A"); } } class B extends A { private int b; private double c; B(int a,int b,double c) { super(a); this.b=b; this.c=c; System.out.println("This is constructor of class B"); } } B b1 = new B(8,10,8.6); OUTPUT This is constructor of class A This is constructor of class B
  • 15. class A { private int a; protected String name; A(int a, String n) { this.a = a; this.name = n; } void print() { System.out.println("a="+a); } } class B extends A { int b; double c; B(int a,String n,int b,double c) { super(a,n); this.b=b; this.c =c; } void show() { //System.out.println("a="+a); print(); System.out.println("name="+name); System.out.println("b="+b); System.out.println("c="+c); } } a is private in class A Call to print() from super class A Accessing name field from super class (super.name)
  • 16. class xyz3 { public static void main(String args[]) { B b1 = new B(10,"OOP",8,10.56); b1.show(); } } E:Java>java xyz3 a=10 name=OOP b=8 c=10.56
  • 17. USE OF super KEYWORD • Can be used to call super class constrctor super(); super(<parameter-list>); • Can refer to super class instance variables/Methods super.<super class instance variable/Method>
  • 18. class A { private int a; A( int a) { this.a =a; System.out.println("This is constructor of class A"); } void print() { System.out.println("a="+a); } void display() { System.out.println("hello This is Display in A"); } } // End of class A class B extends A { private int b; private double c; B(int a,int b,double c) { super(a); this.b=b; this.c=c; System.out.println("This is constructor of class B"); } void show() { print(); System.out.println("b="+b); System.out.println("c="+c); } } // End of class B
  • 19. class inhtest1 { public static void main(String args[]) { B b1 = new B(10,8,4.5); b1.show(); } } /* OutPUt D:javabin>java inhtest1 This is constructor of class A This is constructor of class B a=10 b=8 c=4.5 */
  • 20. class A { private int a; A( int a) { this.a =a; System.out.println("This is constructor of class A"); } void show() { System.out.println("a="+a); } void display() { System.out.println("hello This is Display in A"); } } class B extends A { private int b; private double c; B(int a,int b,double c) { super(a); this.b=b; this.c=c; System.out.println("This is constructor of class B"); } void show() { super.show(); System.out.println("b="+b); System.out.println("c="+c); display(); } }
  • 21. class inhtest1 { public static void main(String args[]) { B b1 = new B(10,8,4.5); b1.show(); } } /* OutPut D:javabin>java inhtest1 This is constructor of class A This is constructor of class B a=10 b=8 c=4.5 hello This is Display in A */
  • 22. class A { int a; A( int a) { this.a =a; } void show() { System.out.println("a="+a); } void display() { System.out.println("hello This is Display in A"); } } class B extends A { int b; double c; B(int a,int b,double c) { super(a); this.b=b; this.c=c; } void show() { //super.show(); System.out.println("a="+a); System.out.println("b="+b); System.out.println("c="+c); } }
  • 23. class inhtest2 { public static void main(String args[]) { B b1 = new B(10,20,8.4); b1.show(); } } /* D:javabin>java inhtest2 a=10 b=20 c=8.4 */
  • 24. class A { int a; A( int a) { this.a =a; } } class B extends A { // super class variable a hides here int a; int b; double c; B(int a,int b,double c) { super(100); this.a = a; this.b=b; this.c=c; } void show() { System.out.println("Super class a="+super.a); System.out.println("a="+a); System.out.println("b="+b); System.out.println("c="+c); } }
  • 25. class inhtest2 { public static void main(String args[]) { B b1 = new B(10,20,8.4); b1.show(); } } /* Out Put D:javabin>java inhtest2 Super class a=100 a=10 b=20 c=8.4 */