SlideShare a Scribd company logo
1 of 21
Inheritance
Dr. M H B Ariyaratne
https://goo.gl/UYe6vN
Main features of OOPS.
1) Encapsulation
2) Inheritance
3) Polymorphism
Inheritance
The ability to create classes that share the attributes and methods of existing
classes, but with more specific features
Mainly used for code reusability
Making use of already written class and further extending on that
Deriving a new class from existing class, it’s called as Inheritance
Inheritance: Introduction
Reusability--building new components by utilising
existing components
More productive when we are reuse something
that is already exists rather than creating the
same all over again
By creating new classes, reusing the properties of
existing classes.
*
Inheritance: Introduction
This mechanism of deriving a new
class from existing/old class is
called “inheritance”.
The old class is known as “base”
class, “superclass or “parent”
class”; and the new class is known
as “subclass”, “derived” class, or
“child” class.
*
Parent
Child
Inherited capability
Inheritance: Introduction
The inheritance allows subclasses to inherit all
properties (variables and methods) of their parent
classes. The different forms of inheritance are:
Single inheritance (only one superclass)
Multiple inheritance (several super classes) - NOT
SUPPORTED IN JAVA
Hierarchical inheritance (one superclass, many sub
classes)
Multi-Level inheritance (derived from a derived class)
*
*Forms of Inheritance
A
B
(a) Single Inheritance
A
C
(b) Multiple Inheritance
B
A
C
(c) Hierarchical Inheritance
B D
A
C
(d) Multi-Level
B
Defining a Sub class
A subclass/child class is defined as follows:
The keyword “extends” signifies that the properties of super class
are extended to the subclass. That means, subclass contains its
own members as well of those of the super class. This kind of
situation occurs when we want to enhance properties of existing
class without actually modifying it.
*
class SubClassName extends SuperClassName
{
fields declaration;
methods declaration;
}
Subclasses and Inheritance
Circle class captures basic properties
For drawing application, need a circle to
draw itself on the screen, GraphicCircle...
This can be realised either by updating
the circle class itself (which is not a good
Software Engineering method) or creating
a new class that builds on the existing
class and add additional properties.
*
Without Inheritance *
public class GraphicCircle {
public Circle c; // keep a copy of a circle
public double area() { return c.area(); }
public double circumference (){ return c.circumference(); }
// new instance variables, methods for this class
public Color outline, fill;
public void draw(DrawWindow dw) { /* drawing code here */ }
}
Subclasses and Inheritance
Circle class captures basic properties
For drawing application need a circle to draw
itself on the screen, GraphicCircle
Java/OOP allows for Circle class code to be
implicitly (re)used in defining a GraphicCircle
GraphicCircle becomes a subclass of Circle,
extending its capabilities
*
*
Circle
x,y,r : double
area ( ) : double
circumference(): double
GraphicCircle
outline, fill : Color
draw (DrawWindow ) : void
Superclass
base class,
Or parent
class
Subclass,
Derived class,
or
Child class
Subclassing Circle
Subclassing
Subclasses created by the keyword extends:
Each GraphicCircle object is also a Circle!
*
public class GraphicCircle extends Circle {
// automatically inherit all the variables and methods
// of Circle, so only need to put in the ‘new stuff’
Color outline, fill;
public void draw(DrawWindow dw) {
dw.drawCircle(x,y,r,outline,fill);
}
}
Final Classes
Declaring class with final modifier prevents it being
extended or subclassed.
Allows compiler to optimize the invoking of methods of
the class
final class Cirlce{
…………
}
*
Subclasses & Constructors
Default constructor automatically calls
constructor of the base class:
*
GraphicCircle drawableCircle = new GraphicCircle();
default constructor
for Circle class is
called
Subclasses & Constructors
Defined constructor can invoke base class constructor
with super:
*
public GraphicCircle(double x, double y, double r,
Color outline, Color fill) {
super(x, y, r);
this.outline = outline;
this fill = fill
}
Shadowed Variables
Subclasses defining variables with the same name as those in the
superclass, shadow them:
*
Shadowed Variables -
Example
*
public class Circle {
public float r = 100;
}
public class GraphicCircle extends Circle {
public float r = 10; // New variable, resolution in dots per inch
}
public class CircleTest {
public static void main(String[] args){
GraphicCircle gc = new GraphicCircle();
Circle c = gc;
System.out.println(“ GraphicCircleRadius= “ + gc.r); // 10
System.out.println (“ Circle Radius = “ + c.r); // 100
}
}
Overriding Methods
Derived/sub classes defining methods with same name, return
type and arguments as those in the parent/super class, override
their parents methods:
*
SummaryInheritance promotes reusability by supporting the creation
of new classes from existing classes.
Various forms of inheritance can be realised in Java.
Child class constructor can be directed to invoke selected
constructor from parent using super keyword.
Variables and Methods from parent classes can be
overridden by redefining them in derived classes.
New Keywords: extends, super, final
*
Thank you

More Related Content

What's hot

What's hot (19)

Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Inheritance
InheritanceInheritance
Inheritance
 
Sdtl assignment 03
Sdtl assignment 03Sdtl assignment 03
Sdtl assignment 03
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Super keyword.23
Super keyword.23Super keyword.23
Super keyword.23
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
JavaYDL11
JavaYDL11JavaYDL11
JavaYDL11
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
C++ classes
C++ classesC++ classes
C++ classes
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Inheritance
InheritanceInheritance
Inheritance
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 

Similar to 10. inheritance

Similar to 10. inheritance (20)

RajLec10.ppt
RajLec10.pptRajLec10.ppt
RajLec10.ppt
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interface
 
Java02
Java02Java02
Java02
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java
JavaJava
Java
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Inheritance
Inheritance Inheritance
Inheritance
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Java
JavaJava
Java
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 

More from M H Buddhika Ariyaratne (17)

14. collections
14. collections14. collections
14. collections
 
13. interfaces
13. interfaces13. interfaces
13. interfaces
 
12. arrays
12. arrays12. arrays
12. arrays
 
11. java methods
11. java methods11. java methods
11. java methods
 
9. strings
9. strings9. strings
9. strings
 
8. objects & classes
8. objects & classes8. objects & classes
8. objects & classes
 
7. flowcharts and pseudocode
7. flowcharts and pseudocode7. flowcharts and pseudocode
7. flowcharts and pseudocode
 
6. java operators
6. java operators6. java operators
6. java operators
 
5. variables & data types
5. variables & data types5. variables & data types
5. variables & data types
 
4. mathematics for computing
4. mathematics for computing4. mathematics for computing
4. mathematics for computing
 
3. introduction to java
3. introduction to java3. introduction to java
3. introduction to java
 
2. data structures & algorithms
2. data structures & algorithms2. data structures & algorithms
2. data structures & algorithms
 
1. introduction mathematics for computing and object oriented programming
1. introduction   mathematics for computing  and  object oriented programming1. introduction   mathematics for computing  and  object oriented programming
1. introduction mathematics for computing and object oriented programming
 
Gis for healthcare introduction
Gis for healthcare   introductionGis for healthcare   introduction
Gis for healthcare introduction
 
Open hospital management information system
Open hospital management information systemOpen hospital management information system
Open hospital management information system
 
Who am i and what i have done
Who am i and what i have doneWho am i and what i have done
Who am i and what i have done
 
Electronic health record system for sri lankan general practitioners
Electronic health record system for sri lankan general practitionersElectronic health record system for sri lankan general practitioners
Electronic health record system for sri lankan general practitioners
 

Recently uploaded

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 

10. inheritance

  • 1. Inheritance Dr. M H B Ariyaratne https://goo.gl/UYe6vN
  • 2. Main features of OOPS. 1) Encapsulation 2) Inheritance 3) Polymorphism
  • 3. Inheritance The ability to create classes that share the attributes and methods of existing classes, but with more specific features Mainly used for code reusability Making use of already written class and further extending on that Deriving a new class from existing class, it’s called as Inheritance
  • 4. Inheritance: Introduction Reusability--building new components by utilising existing components More productive when we are reuse something that is already exists rather than creating the same all over again By creating new classes, reusing the properties of existing classes. *
  • 5. Inheritance: Introduction This mechanism of deriving a new class from existing/old class is called “inheritance”. The old class is known as “base” class, “superclass or “parent” class”; and the new class is known as “subclass”, “derived” class, or “child” class. * Parent Child Inherited capability
  • 6. Inheritance: Introduction The inheritance allows subclasses to inherit all properties (variables and methods) of their parent classes. The different forms of inheritance are: Single inheritance (only one superclass) Multiple inheritance (several super classes) - NOT SUPPORTED IN JAVA Hierarchical inheritance (one superclass, many sub classes) Multi-Level inheritance (derived from a derived class) *
  • 7. *Forms of Inheritance A B (a) Single Inheritance A C (b) Multiple Inheritance B A C (c) Hierarchical Inheritance B D A C (d) Multi-Level B
  • 8. Defining a Sub class A subclass/child class is defined as follows: The keyword “extends” signifies that the properties of super class are extended to the subclass. That means, subclass contains its own members as well of those of the super class. This kind of situation occurs when we want to enhance properties of existing class without actually modifying it. * class SubClassName extends SuperClassName { fields declaration; methods declaration; }
  • 9. Subclasses and Inheritance Circle class captures basic properties For drawing application, need a circle to draw itself on the screen, GraphicCircle... This can be realised either by updating the circle class itself (which is not a good Software Engineering method) or creating a new class that builds on the existing class and add additional properties. *
  • 10. Without Inheritance * public class GraphicCircle { public Circle c; // keep a copy of a circle public double area() { return c.area(); } public double circumference (){ return c.circumference(); } // new instance variables, methods for this class public Color outline, fill; public void draw(DrawWindow dw) { /* drawing code here */ } }
  • 11. Subclasses and Inheritance Circle class captures basic properties For drawing application need a circle to draw itself on the screen, GraphicCircle Java/OOP allows for Circle class code to be implicitly (re)used in defining a GraphicCircle GraphicCircle becomes a subclass of Circle, extending its capabilities *
  • 12. * Circle x,y,r : double area ( ) : double circumference(): double GraphicCircle outline, fill : Color draw (DrawWindow ) : void Superclass base class, Or parent class Subclass, Derived class, or Child class Subclassing Circle
  • 13. Subclassing Subclasses created by the keyword extends: Each GraphicCircle object is also a Circle! * public class GraphicCircle extends Circle { // automatically inherit all the variables and methods // of Circle, so only need to put in the ‘new stuff’ Color outline, fill; public void draw(DrawWindow dw) { dw.drawCircle(x,y,r,outline,fill); } }
  • 14. Final Classes Declaring class with final modifier prevents it being extended or subclassed. Allows compiler to optimize the invoking of methods of the class final class Cirlce{ ………… } *
  • 15. Subclasses & Constructors Default constructor automatically calls constructor of the base class: * GraphicCircle drawableCircle = new GraphicCircle(); default constructor for Circle class is called
  • 16. Subclasses & Constructors Defined constructor can invoke base class constructor with super: * public GraphicCircle(double x, double y, double r, Color outline, Color fill) { super(x, y, r); this.outline = outline; this fill = fill }
  • 17. Shadowed Variables Subclasses defining variables with the same name as those in the superclass, shadow them: *
  • 18. Shadowed Variables - Example * public class Circle { public float r = 100; } public class GraphicCircle extends Circle { public float r = 10; // New variable, resolution in dots per inch } public class CircleTest { public static void main(String[] args){ GraphicCircle gc = new GraphicCircle(); Circle c = gc; System.out.println(“ GraphicCircleRadius= “ + gc.r); // 10 System.out.println (“ Circle Radius = “ + c.r); // 100 } }
  • 19. Overriding Methods Derived/sub classes defining methods with same name, return type and arguments as those in the parent/super class, override their parents methods: *
  • 20. SummaryInheritance promotes reusability by supporting the creation of new classes from existing classes. Various forms of inheritance can be realised in Java. Child class constructor can be directed to invoke selected constructor from parent using super keyword. Variables and Methods from parent classes can be overridden by redefining them in derived classes. New Keywords: extends, super, final *