SlideShare a Scribd company logo
1 of 16
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

CMSC 202 - Lec16 - Polymorphisms(1).pptx
CMSC 202 - Lec16 - Polymorphisms(1).pptxCMSC 202 - Lec16 - Polymorphisms(1).pptx
CMSC 202 - Lec16 - Polymorphisms(1).pptx
deathlyfire321
 

Similar to Java Encapsulation and Inheritance (20)

29c
29c29c
29c
 
Constructor
ConstructorConstructor
Constructor
 
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
 
SystemVerilog_Classes.pdf
SystemVerilog_Classes.pdfSystemVerilog_Classes.pdf
SystemVerilog_Classes.pdf
 
Intro To C++ - Class #22: Inheritance, Part 1
Intro To C++ - Class #22: Inheritance, Part 1Intro To C++ - Class #22: Inheritance, Part 1
Intro To C++ - Class #22: Inheritance, Part 1
 
Java - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptxJava - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptx
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritance
 
OOP
OOPOOP
OOP
 
Lecturespecial
LecturespecialLecturespecial
Lecturespecial
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Intro To C++ - Class #23: Inheritance, Part 2
Intro To C++ - Class #23: Inheritance, Part 2Intro To C++ - Class #23: Inheritance, Part 2
Intro To C++ - Class #23: Inheritance, Part 2
 
Write First C++ class
Write First C++ classWrite First C++ class
Write First C++ class
 
CPP15 - Inheritance
CPP15 - InheritanceCPP15 - Inheritance
CPP15 - Inheritance
 
Pi j2.2 classes
Pi j2.2 classesPi j2.2 classes
Pi j2.2 classes
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
 
Static.18
Static.18Static.18
Static.18
 
CMSC 202 - Lec16 - Polymorphisms(1).pptx
CMSC 202 - Lec16 - Polymorphisms(1).pptxCMSC 202 - Lec16 - Polymorphisms(1).pptx
CMSC 202 - Lec16 - Polymorphisms(1).pptx
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 

More from AathikaJava

More from AathikaJava (17)

Java While Loop
Java While LoopJava While Loop
Java While Loop
 
Java Webservices
Java WebservicesJava Webservices
Java Webservices
 
Java Type Casting
Java Type Casting Java Type Casting
Java Type Casting
 
Spring Web MVC
Spring Web MVCSpring Web MVC
Spring Web MVC
 
Java Session
Java SessionJava Session
Java Session
 
Java Servlet Lifecycle
Java Servlet LifecycleJava Servlet Lifecycle
Java Servlet Lifecycle
 
Java Rest
Java Rest Java Rest
Java Rest
 
Java Request Dispatcher
Java Request DispatcherJava Request Dispatcher
Java Request Dispatcher
 
Java Polymorphism Part 2
Java Polymorphism Part 2Java Polymorphism Part 2
Java Polymorphism Part 2
 
Java MVC
Java MVCJava MVC
Java MVC
 
Java Polymorphism
Java PolymorphismJava Polymorphism
Java Polymorphism
 
Java Spring
Java SpringJava Spring
Java Spring
 
Mapping Classes with Relational Databases
Mapping Classes with Relational DatabasesMapping Classes with Relational Databases
Mapping Classes with Relational Databases
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
Hibernate basics
Hibernate basicsHibernate basics
Hibernate basics
 
Java Filters
Java FiltersJava Filters
Java Filters
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 

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.