SlideShare a Scribd company logo
2
Most read
3
Most read
Java/J2EE Programming Training
Java Inheritance
Page 2Classification: Restricted
Agenda
• Encapsulation
• Inheritance
Page 3Classification: Restricted
• Implement encapsulation
• by marking the field as private.
• by marking the member function as private.
Encapsulation
Page 4Classification: Restricted
• avoids reinventing the wheel
• promotes code reusability
• derives a new class from the existing class
• use the existing features
• enhance the existing features
• add new features.
• Existing class is called as “base class”
• new class derived from existing class is called as “Child class”
Inheritance
Page 5Classification: Restricted
• child class will have all the features from the parent class
• plus it will have its own additional features
• additional features:
• new member variable ;
• new member functions
• What can you access in child class from parent class
• non private member variables
• non private member variables can be access via setter and getter
methods of parent class.
• non private member functions
• What can you do in Child class
• access non private data from parent class directly.
• inherited methods can be accessed directly.
• declare new methods in child class.
• override parent class methods in child class.
Inheritance
Page 6Classification: Restricted
Sms
+ String to
+ String text
+ void sendSms()
PictureSms
+ String to
+ String text
+ String pic
+ void sendSms()
+void sendPicSms()
Page 7Classification: Restricted
Rectangle
+ int length
+ int breadth
+ void area()
Cuboid
+ int length
+ int breadth
+ int height
+ void surfaceArea()
+void volume()
Page 8Classification: Restricted
Student
String username
String passwd
String course
Int score
login()
logoff
writeExam()
viewResult
Employee
String username
String passwd
String desig
Int salary
login()
logoff
evaluate()
Page 9Classification: Restricted
Student
String username
String password
String course
Int marks
Login()
Logoff()
takeExam()
Employee
String username
String password
String desig
Int salary
Login()
Logoff()
evaluate()
User
String username
String password
Login()
Logoff()
Page 10Classification: Restricted
Question
+ String question
+ String optionA
+ String optionB
+ char answer
+ void addQuestion()
+ void deleteQuestion
ImageQuestion
+ String question
+ String optionA
+ String optionB
+ char answer
+ Image img
+ void addQuestion()
+ void deleteQuestion
+ void addImage()
VideoQuestion
+ String question
+ String optionA
+ String optionB
+ char answer
+ Video video
+ void addQuestion()
+ void deleteQuestion
+ void addVideo()
Page 11Classification: Restricted
Constructors….revisited
• Constructor are not inherited
• When a child class object is created,
• derive class constructor invokes its immediate super class constructor,
all the way up the hierarchy.
• How can a child class constructor call its parent class constructor.
• using super keyword
How constructors are invoked Rectangle
+ int length
+ int breadth
+ void area()
Cuboid
+ int length
+ int breadth
+ int height
+ void surfaceArea()
+void volume()
Cuboid c = new Cuboid();
Rectangle
+ int length
+ int breadth
+ void area()
length = 0
breadth= 0 Cuboid
+ int length
+ int breadth
+ int height
+ void surfaceArea()
+void volume()
height = 0
Cuboid c= new Cuboid() class Rectangle
{
public int length;breadth;
public Rectangle()
{
length = 0;
breadth =0;
}
}
class Cuboid
{
//private int length;breadth;
public int height;
public Cuboid()
{
super();
height = 0;
}
}
length=0
breadth=0
height =0
call to super is
inserted by the
compiler implicitly f
default constructor
Cuboid c= new Cuboid(10, 20,30) class Rectangle
{
public int length;breadth;
public Rectangle(int l, int
b)
{
length = l;
breadth =b;
}
}
class Cuboid
{
//private int length;breadth;
public int height;
public Cuboid(int l, int b,int
h)
{
super(l, b );
height = h;
}
}
length=10
breadth=20
height =30
call to super is explicit
Page 15Classification: Restricted
Super
• super keyword is used to call immediate parent class constructor
• should be the first statement of the constructor
• If the programmer does not provide call to super(), then the compiler
inserts the call to super.
• programmer must call super() explicitly for constructor with arguments.
Page 16Classification: Restricted
Thank You

More Related Content

Similar to Java Encapsulation and Inheritance (20)

PPT
java training faridabad
Woxa Technologies
 
PPTX
Inheritance and its types In Java
MD SALEEM QAISAR
 
PPTX
Review Session and Attending Java Interviews
RatnaJava
 
PDF
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
PPSX
Review Session and Attending Java Interviews
Hitesh-Java
 
PPSX
OOP with Java - Continued
Hitesh-Java
 
PPTX
Session 18 - Review Session and Attending Java Interviews
PawanMM
 
PPT
Java Tutorials
Woxa Technologies
 
PPT
Inheritance & Polymorphism - 1
PRN USM
 
PPTX
OOP with Java - Abstract Classes and Interfaces
RatnaJava
 
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
PPT
Chapter 8 Inheritance
OUM SAOKOSAL
 
PPTX
Java Polymorphism Part 2
AathikaJava
 
PPTX
Session 08 - OOP with Java - continued
PawanMM
 
PPT
Java Programming - Inheritance
Oum Saokosal
 
PPTX
Detailed_description_on_java_ppt_final.pptx
technicaljd3
 
PPT
03-inheritance.ppt
SaiM947604
 
PPTX
Session 06 - Java Basics
SiddharthSelenium
 
PDF
Core-java-materiallgfffdfdhgfjfghfchgfhg
komalbhagat8803
 
java training faridabad
Woxa Technologies
 
Inheritance and its types In Java
MD SALEEM QAISAR
 
Review Session and Attending Java Interviews
RatnaJava
 
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
Review Session and Attending Java Interviews
Hitesh-Java
 
OOP with Java - Continued
Hitesh-Java
 
Session 18 - Review Session and Attending Java Interviews
PawanMM
 
Java Tutorials
Woxa Technologies
 
Inheritance & Polymorphism - 1
PRN USM
 
OOP with Java - Abstract Classes and Interfaces
RatnaJava
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Chapter 8 Inheritance
OUM SAOKOSAL
 
Java Polymorphism Part 2
AathikaJava
 
Session 08 - OOP with Java - continued
PawanMM
 
Java Programming - Inheritance
Oum Saokosal
 
Detailed_description_on_java_ppt_final.pptx
technicaljd3
 
03-inheritance.ppt
SaiM947604
 
Session 06 - Java Basics
SiddharthSelenium
 
Core-java-materiallgfffdfdhgfjfghfchgfhg
komalbhagat8803
 

More from AathikaJava (16)

PPTX
Java While Loop
AathikaJava
 
PPTX
Java Webservices
AathikaJava
 
PPTX
Java Type Casting
AathikaJava
 
PPTX
Spring Web MVC
AathikaJava
 
PPTX
Java Session
AathikaJava
 
PPTX
Java Servlet Lifecycle
AathikaJava
 
PPTX
Java Rest
AathikaJava
 
PPTX
Java Request Dispatcher
AathikaJava
 
PPTX
Java MVC
AathikaJava
 
PPTX
Java Polymorphism
AathikaJava
 
PPTX
Java Spring
AathikaJava
 
PPTX
Mapping Classes with Relational Databases
AathikaJava
 
PPTX
Introduction to Java
AathikaJava
 
PPT
Hibernate basics
AathikaJava
 
PPTX
Java Filters
AathikaJava
 
PPTX
Encapsulation
AathikaJava
 
Java While Loop
AathikaJava
 
Java Webservices
AathikaJava
 
Java Type Casting
AathikaJava
 
Spring Web MVC
AathikaJava
 
Java Session
AathikaJava
 
Java Servlet Lifecycle
AathikaJava
 
Java Rest
AathikaJava
 
Java Request Dispatcher
AathikaJava
 
Java MVC
AathikaJava
 
Java Polymorphism
AathikaJava
 
Java Spring
AathikaJava
 
Mapping Classes with Relational Databases
AathikaJava
 
Introduction to Java
AathikaJava
 
Hibernate basics
AathikaJava
 
Java Filters
AathikaJava
 
Encapsulation
AathikaJava
 
Ad

Recently uploaded (20)

PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Ad

Java Encapsulation and Inheritance

  • 2. Page 2Classification: Restricted Agenda • Encapsulation • Inheritance
  • 3. Page 3Classification: Restricted • Implement encapsulation • by marking the field as private. • by marking the member function as private. Encapsulation
  • 4. Page 4Classification: Restricted • avoids reinventing the wheel • promotes code reusability • derives a new class from the existing class • use the existing features • enhance the existing features • add new features. • Existing class is called as “base class” • new class derived from existing class is called as “Child class” Inheritance
  • 5. Page 5Classification: Restricted • child class will have all the features from the parent class • plus it will have its own additional features • additional features: • new member variable ; • new member functions • What can you access in child class from parent class • non private member variables • non private member variables can be access via setter and getter methods of parent class. • non private member functions • What can you do in Child class • access non private data from parent class directly. • inherited methods can be accessed directly. • declare new methods in child class. • override parent class methods in child class. Inheritance
  • 6. Page 6Classification: Restricted Sms + String to + String text + void sendSms() PictureSms + String to + String text + String pic + void sendSms() +void sendPicSms()
  • 7. Page 7Classification: Restricted Rectangle + int length + int breadth + void area() Cuboid + int length + int breadth + int height + void surfaceArea() +void volume()
  • 8. Page 8Classification: Restricted Student String username String passwd String course Int score login() logoff writeExam() viewResult Employee String username String passwd String desig Int salary login() logoff evaluate()
  • 9. Page 9Classification: Restricted Student String username String password String course Int marks Login() Logoff() takeExam() Employee String username String password String desig Int salary Login() Logoff() evaluate() User String username String password Login() Logoff()
  • 10. Page 10Classification: Restricted Question + String question + String optionA + String optionB + char answer + void addQuestion() + void deleteQuestion ImageQuestion + String question + String optionA + String optionB + char answer + Image img + void addQuestion() + void deleteQuestion + void addImage() VideoQuestion + String question + String optionA + String optionB + char answer + Video video + void addQuestion() + void deleteQuestion + void addVideo()
  • 11. Page 11Classification: Restricted Constructors….revisited • Constructor are not inherited • When a child class object is created, • derive class constructor invokes its immediate super class constructor, all the way up the hierarchy. • How can a child class constructor call its parent class constructor. • using super keyword
  • 12. How constructors are invoked Rectangle + int length + int breadth + void area() Cuboid + int length + int breadth + int height + void surfaceArea() +void volume() Cuboid c = new Cuboid(); Rectangle + int length + int breadth + void area() length = 0 breadth= 0 Cuboid + int length + int breadth + int height + void surfaceArea() +void volume() height = 0
  • 13. Cuboid c= new Cuboid() class Rectangle { public int length;breadth; public Rectangle() { length = 0; breadth =0; } } class Cuboid { //private int length;breadth; public int height; public Cuboid() { super(); height = 0; } } length=0 breadth=0 height =0 call to super is inserted by the compiler implicitly f default constructor
  • 14. Cuboid c= new Cuboid(10, 20,30) class Rectangle { public int length;breadth; public Rectangle(int l, int b) { length = l; breadth =b; } } class Cuboid { //private int length;breadth; public int height; public Cuboid(int l, int b,int h) { super(l, b ); height = h; } } length=10 breadth=20 height =30 call to super is explicit
  • 15. Page 15Classification: Restricted Super • super keyword is used to call immediate parent class constructor • should be the first statement of the constructor • If the programmer does not provide call to super(), then the compiler inserts the call to super. • programmer must call super() explicitly for constructor with arguments.