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

29c
29c29c
Constructor
ConstructorConstructor
Constructor
abhay singh
 
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
NithyaN19
 
SystemVerilog_Classes.pdf
SystemVerilog_Classes.pdfSystemVerilog_Classes.pdf
SystemVerilog_Classes.pdf
ssusere9cd04
 
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
Blue Elephant Consulting
 
Java - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptxJava - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptx
Vikash Dúbēy
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
yugandhar vadlamudi
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
GaneshKumarKanthiah
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritance
Nurhanna Aziz
 
Lecturespecial
LecturespecialLecturespecial
Lecturespecial
karan saini
 
OOP
OOPOOP
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
RatnaJava
 
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
Blue Elephant Consulting
 
Write First C++ class
Write First C++ classWrite First C++ class
Write First C++ class
Learn By Watch
 
CPP15 - Inheritance
CPP15 - InheritanceCPP15 - Inheritance
CPP15 - Inheritance
Michael Heron
 
Pi j2.2 classes
Pi j2.2 classesPi j2.2 classes
Pi j2.2 classes
mcollison
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
Static.18
Static.18Static.18
Static.18
myrajendra
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 

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
 
Lecturespecial
LecturespecialLecturespecial
Lecturespecial
 
OOP
OOPOOP
OOP
 
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
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
 

More from AathikaJava

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

Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 

Recently uploaded (20)

Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 

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.