SlideShare a Scribd company logo
UNIT-2-CHAPTER-3
INHERITANCE
Inheritance basics
 Java supports inheritance by allowing one
class to incorporate another class into its
declaration
 Done using a keyword “extend”
 Subclass adds to (extends ) the superclass
 A class that is inherited is called a superclass
 The class that does the inheriting is called
subclass
 Hence subclass is a specialized version of
superclass
 It inherits all of the variables and methods
Chap3 inheritance
Chap3 inheritance
Chap3 inheritance
 the triangle class includes all TwoDObject and
adds the field style and a method area() and
method showStyle().
 The triangle ‘s style is stored in style.
 this can be any string that describes the
triangle such as “filled” “outlined”,
“transparent”, “isosceles” or “rounded”.
 The area() method computes and returns the
area of the triangle, showStyle() displays the
triangle style.
 The general form of a class declaration that inherits a
superclass is shown here:
class subclass_name extends superclass_name{
//body of the class
}
 Can mention one superclass for any subclass that you
create.
 Java does not support the inheritance of multiple
super classes into a single subclass
 Allows to create a hierarchy of inheritance in which a
subclass becomes a super class of another subclass.
 No class is superclass of itself.
Advantage of inheritance
 Once you have created a superclass that
defines the attributes common set of objects, it
can be used to create any number of more
specific subclass
Member access and inheritance
 A subclass includes all of the members of its
superclass it cannot access those members of the
superclass that have been declared private
class TwoDshape{
private double width;
private double height;
…..
double area(){
return width*height /2;
}
}
 Program will not compile because the
reference to width and height inside the area()
method causes an access violation.
 Since width and height members are private in
nature, they are accessible only by other
members of their own class.
 Private members are not accessible outside
the class, even subclasses.
 Java uses accessor methods to provide
access to the private members of the class.
Chap3 inheritance
Chap3 inheritance
Constructors and inheritance
 The constructor for the superclass constructs
the superclass portion of the object,
constructor for the subclass constructs the
subclass part.
 The superclass has no knowledge of access to
any element in a subclass., hence their
construction must be separate.
 The superclass portion of the object is
constructed automatically using a default
constructor.
Chap3 inheritance
Chap3 inheritance
Chap3 inheritance
Chap3 inheritance
 Here triangle’s constructor initializes the
members of the TwoD class that it inherits
along with its own style field.
 When both the superclass and the subclass
defines as constructor, the process is more
complicated because both superclass and
subclass constructor must be executed.
Using super to call Superclass
constructors
 A subclass can call a constructor defined by its
superclass by the use of the following form of
super:
 Super(parameter_list);
 Here parameter_list specifies any parameter
needed by the constructor in the superclass
 super() must be the first statement executed
inside a subclass constructor
 Eg: defines a constructor that initializes width
and height varaible in the TwoDShape
program.
Chap3 inheritance
Chap3 inheritance
Chap3 inheritance
 Triangle() calls super() with parameters w and h.
 This causes the TwoDshape() constructor to be
called, which initialize width and height using
these values.
 Triangle no longer initializes these values itself.
 TwoDshape can add functionality about which
existing subclasses have no knowledge, thus
preventing existing code from breaking.
 Any form of constructor defined by the superclass
can be called by super()
 The constructor executed will be one that matches
Chap3 inheritance
Chap3 inheritance
Chap3 inheritance
Chap3 inheritance
 When a subclass calls super(), it is calling the
constructor of its immediate superclass.
 Super() always refers to the superclass
immediately above the calling class.
 It is true even in a multilevel hierarchy
 super() must always be the first statement
executed inside a constructor.
Using super to access superclass
members
 Using super we can refer to the members of
the superclass.
 General form is as follows:
 super.member
 Here the member can be either a method or
an instance variable.
 Used when the members of the subclass hide
members by the same in the superclass.
Chap3 inheritance
Chap3 inheritance
Creating a multilevel hierarchy
 Assume that there are three classes A,B and C
where C is a subclass of B, which in turn is
subclass of A.
 Here class C will inherit all of the traits found in
subclasses.
Chap3 inheritance
Chap3 inheritance
Chap3 inheritance
When are constructors called?
 Constructors are called in the order of
derivation from superclass to subclass.
 Super() must be the first statement executed in
subclass constructor.
 If super() is not used then the default
constructor will be executed.
Chap3 inheritance
Chap3 inheritance
Superclass references and
subclass objects
 Java is strongly typed language
 type compatibility is strictly enforced
 A reference variable for one class type cannot
normally refer to an object of another class type.
class X{
int a;
x(int i)
{
a= i ;
}
}
class Y{
int a;
Y(int i){
a=i;
}
}
class incompatibletypes{
X objx1=new X(10);
X objx2;
Y y=new Y(15);
objx2=x; // OK, both of same type
objx2=y; //error , not of same type
}
 Class x and y are physically same, it is not
possible to assign an X reference to Y object
they have different types.
 An object can only refer a object of its type.
 A reference variable of superclass can be
assigned a reference to an object of any
subclass derived from that superclass.
 A superclass object can refer to a subclass
object.
class X{
int a;
X (int i)
{
a=i;
}
}
class Y extends X{
int b;
Y(int i, int j){
super(j);
b= i;
}
class SupSub{
public static void main(String
args[]){
X x1=new X(10);
X x2;
Y y=new Y(5,6);
x2=x1;
System.out.println(“x2.a:”+x2.a);
x2=y;
System.out.println(“x2.a:”+x2.a);
X2.a=19; //OK
X2.b=27; //error, x does not have a
and b member
}
}
Method overriding
 When a method in a subclass has the same
return type and signature as a method in its
superclass, then the method in the subclass is
said to be override the method in superclass.
 When an overridden method is called from
within a subclass , it will always refer to the
version of that method defined by the
subclass.
 The version defined by the superclass will be
hidden.
Chap3 inheritance
 If the superclass version of an overridden
method has to be accessed then, you can do
so by using super().
 Method overriding occurs only when the
signatures of the two methods are identical.
 If they are not then, the two methods are
simply overloaded.
Chap3 inheritance
Overridden methods support
polyorphsim
 Dynamic method dispatch
 Mechanism by which call to an overridden
method is resolved at run time rather than
compile time
 Dynamic method dispatch is important
because this how java implements run-time
polymorphism.
 Is superclass reference variable can refer to a
subclass objects.
 Java determines which version of that method
to execute based upon the type of object being
referred to at a time the call occurs.
 When different types of objects are referred to,
different versions of overridden method will be
called.
 It is the type of the objects being referred to that
determines which version of an overridden
method will be executed.
 If a superclass contains a method that is
overridden by a subclass, then when different
types of objects are referred to through a
superclass reference variable , different versions
of the method are executed.
Chap3 inheritance
Chap3 inheritance
Chap3 inheritance
Why overridden methods?
 It allows to support polymorphism in JAVA
 Polymorphism allows a general class to specify
methods that will be common to all of its
derivatives, while allowing subclasses to define
the specific implementation of some or all of those
methods.
 It helps to implement “one interface, multiple
methods”
 By combining inheritance with the overridden
methods, a superclass can define the general
form of methods that will be used by all of its
subclasses.
Using Abstract Classes
 A class that determines the nature of methods
that subclasses must implement but does not
itself provide any implementation of one or
more of these methods, such a class is called
abstract class
 Defined by specifying the abstract type
modifier.
 It contains no body and is there not
implemented by the superclass
 The subclass must override it
 abstract type name(parameter_list);
 No body is present, the abstract modifier can
be used only on normal methods.
 Cannot be applied to static methods or to
constructors.
 A class that contains one or more abstract
method should be declared as abstract class
with the abstract modifier.
 Since abstract class does not define a
complete implementation, there can be no
objects of an abstract class.
 Attempting to create an object of an abstract
class, will lead to a compile time error.
 When a subclass inherits an abstract class, it
should implement all of the abstract method in
superclass
 If it does not, then the subclass must be
specified as abstract.
 Hence abstract is inherited until complete
implementation of the methods are achieved.
Chap3 inheritance
Using final
 To prevent a method from overridden, specify
that method with a keyword “final” at the start
of its declaration
 Methods declared as final cannot be
overridden
 example
Chap3 inheritance
Final prevents inheritance
 By having a class declared as final, inheritance can be
prevented
 By declaring a class as final, implicitly it declares al
the methods as final too.
 Using abstract and final together is illegal because,
abstract depends on the subclass for its
implementations and final avoids inheritance.
final class A{
//….
}
class B extends A{ //error cant have subclass of A
//….
}
Using final with data members
 final can be applied on data members of the
class.
 If we have members of class as final, the value
cannot be changed throughout the lifetime of
the program..
 Initial values can be given to the variable
 Used to create a named constant.
Chap3 inheritance
Object class
 Java defines a class called Object that is an
implicit superclass of all other classes
 All other classes are subclasses of Object
 This means that a reference variable of type
Object can refer to an object of any other
class.
 Object defines the following methods, which
are available for all objects
Method purpose
Object clone() Creates a new object that is same as the object
being cloned.
boolean equals(Object
obj)
Determines whether one object is equal to
another.
void finalize() Called before unused object to be recycled
int hashCode() Returns the hash code associated with the
invoking object
void notify() Resumes execution of a thread waiting on the
invoking object
void wait() Waits on another thread of execution
String toString() Returns a string that describes the object

More Related Content

PPTX
Inner class
Guna Sekaran
 
PPTX
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
pcnmtutorials
 
PPTX
Super keyword in java
Hitesh Kumar
 
PPTX
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 
PPTX
Inheritance
Tech_MX
 
PPTX
TYPES OF ERRORS.pptx
muskanaggarwal84101
 
PPTX
Single inheritance
Äkshäý M S
 
Inner class
Guna Sekaran
 
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
pcnmtutorials
 
Super keyword in java
Hitesh Kumar
 
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 
Inheritance
Tech_MX
 
TYPES OF ERRORS.pptx
muskanaggarwal84101
 
Single inheritance
Äkshäý M S
 

What's hot (20)

PPTX
C# Types of classes
Prem Kumar Badri
 
PPT
4. Classes and Methods
Nilesh Dalvi
 
PPT
Packages in java
Jancypriya M
 
PPTX
Bootstrap 3
McSoftsis
 
PPT
Object Oriented Programming with Java
backdoor
 
PPTX
Inheritance in Object Oriented Programming
Ashita Agrawal
 
PPT
Java inheritance
Arati Gadgil
 
PPTX
Encapsulation
Githushan Gengaparam
 
PPTX
Java class,object,method introduction
Sohanur63
 
DOCX
Encapsulation in C++
Hitesh Kumar
 
PDF
Java Presentation For Syntax
PravinYalameli
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPTX
C# Access modifiers
Prem Kumar Badri
 
PDF
jQuery - Chapter 3 - Effects
WebStackAcademy
 
PPT
Inheritance in java
Lovely Professional University
 
PDF
Wrapper classes
Ravi_Kant_Sahu
 
PPTX
Наследование и полиморфизм
Ural Federal University named after First President of Russia B.N. Yeltsin
 
PPTX
Java interfaces
jehan1987
 
PPTX
JSP- JAVA SERVER PAGES
Yoga Raja
 
PDF
Abstract classes and Methods in java
Harish Gyanani
 
C# Types of classes
Prem Kumar Badri
 
4. Classes and Methods
Nilesh Dalvi
 
Packages in java
Jancypriya M
 
Bootstrap 3
McSoftsis
 
Object Oriented Programming with Java
backdoor
 
Inheritance in Object Oriented Programming
Ashita Agrawal
 
Java inheritance
Arati Gadgil
 
Encapsulation
Githushan Gengaparam
 
Java class,object,method introduction
Sohanur63
 
Encapsulation in C++
Hitesh Kumar
 
Java Presentation For Syntax
PravinYalameli
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
C# Access modifiers
Prem Kumar Badri
 
jQuery - Chapter 3 - Effects
WebStackAcademy
 
Inheritance in java
Lovely Professional University
 
Wrapper classes
Ravi_Kant_Sahu
 
Наследование и полиморфизм
Ural Federal University named after First President of Russia B.N. Yeltsin
 
Java interfaces
jehan1987
 
JSP- JAVA SERVER PAGES
Yoga Raja
 
Abstract classes and Methods in java
Harish Gyanani
 
Ad

Similar to Chap3 inheritance (20)

PPT
9781439035665 ppt ch10
Terry Yoast
 
PPTX
Chapter 9 java
Ahmad sohail Kakar
 
PPTX
Ppt on this and super keyword
tanu_jaswal
 
PPT
Chap11
Terry Yoast
 
DOCX
Class notes(week 6) on inheritance and multiple inheritance
Kuntal Bhowmick
 
PDF
java_inheritance.pdf
JayMistry91473
 
PDF
Class notes(week 6) on inheritance and multiple inheritance
Kuntal Bhowmick
 
PPT
Inheritance & Polymorphism - 2
PRN USM
 
DOCX
Core java by amit
Thakur Amit Tomer
 
PPTX
Java basics
Shivanshu Purwar
 
DOCX
Sdtl assignment 03
Shrikant Kokate
 
PPTX
Inheritance and Polymorphism Java
M. Raihan
 
DOC
116824015 java-j2 ee
homeworkping9
 
PPTX
inheritance in Java with sample program.pptx
V.V.Vanniaperumal College for Women
 
PPTX
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
PPTX
Ch5 inheritance
HarshithaAllu
 
PPTX
Inheritance ppt
Nivegeetha
 
PDF
Java - Inheritance Concepts
Victer Paul
 
PDF
Presentation 3.pdf
JatinGupta645530
 
9781439035665 ppt ch10
Terry Yoast
 
Chapter 9 java
Ahmad sohail Kakar
 
Ppt on this and super keyword
tanu_jaswal
 
Chap11
Terry Yoast
 
Class notes(week 6) on inheritance and multiple inheritance
Kuntal Bhowmick
 
java_inheritance.pdf
JayMistry91473
 
Class notes(week 6) on inheritance and multiple inheritance
Kuntal Bhowmick
 
Inheritance & Polymorphism - 2
PRN USM
 
Core java by amit
Thakur Amit Tomer
 
Java basics
Shivanshu Purwar
 
Sdtl assignment 03
Shrikant Kokate
 
Inheritance and Polymorphism Java
M. Raihan
 
116824015 java-j2 ee
homeworkping9
 
inheritance in Java with sample program.pptx
V.V.Vanniaperumal College for Women
 
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Ch5 inheritance
HarshithaAllu
 
Inheritance ppt
Nivegeetha
 
Java - Inheritance Concepts
Victer Paul
 
Presentation 3.pdf
JatinGupta645530
 
Ad

More from raksharao (20)

PPTX
Unit 1-logic
raksharao
 
PPTX
Unit 1 rules of inference
raksharao
 
PPTX
Unit 1 quantifiers
raksharao
 
PPTX
Unit 1 introduction to proofs
raksharao
 
PPTX
Unit 7 verification & validation
raksharao
 
PPTX
Unit 6 input modeling problems
raksharao
 
PPTX
Unit 6 input modeling
raksharao
 
PPTX
Unit 5 general principles, simulation software
raksharao
 
PPTX
Unit 5 general principles, simulation software problems
raksharao
 
PPTX
Unit 4 queuing models
raksharao
 
PPTX
Unit 4 queuing models problems
raksharao
 
PPTX
Unit 3 random number generation, random-variate generation
raksharao
 
PPTX
Unit 1 introduction contd
raksharao
 
PPTX
Unit 1 introduction
raksharao
 
PDF
Module1 part2
raksharao
 
PDF
Module1 Mobile Computing Architecture
raksharao
 
PPTX
java-Unit4 chap2- awt controls and layout managers of applet
raksharao
 
PPTX
java Unit4 chapter1 applets
raksharao
 
PPTX
Chap3 multi threaded programming
raksharao
 
PPTX
Java-Unit 3- Chap2 exception handling
raksharao
 
Unit 1-logic
raksharao
 
Unit 1 rules of inference
raksharao
 
Unit 1 quantifiers
raksharao
 
Unit 1 introduction to proofs
raksharao
 
Unit 7 verification & validation
raksharao
 
Unit 6 input modeling problems
raksharao
 
Unit 6 input modeling
raksharao
 
Unit 5 general principles, simulation software
raksharao
 
Unit 5 general principles, simulation software problems
raksharao
 
Unit 4 queuing models
raksharao
 
Unit 4 queuing models problems
raksharao
 
Unit 3 random number generation, random-variate generation
raksharao
 
Unit 1 introduction contd
raksharao
 
Unit 1 introduction
raksharao
 
Module1 part2
raksharao
 
Module1 Mobile Computing Architecture
raksharao
 
java-Unit4 chap2- awt controls and layout managers of applet
raksharao
 
java Unit4 chapter1 applets
raksharao
 
Chap3 multi threaded programming
raksharao
 
Java-Unit 3- Chap2 exception handling
raksharao
 

Recently uploaded (20)

PDF
Queuing formulas to evaluate throughputs and servers
gptshubham
 
PDF
A Framework for Securing Personal Data Shared by Users on the Digital Platforms
ijcncjournal019
 
PDF
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
PPT
Ppt for engineering students application on field effect
lakshmi.ec
 
PPTX
Azure-DevOps-Training presentation downloadable
NamanGoyal428595
 
PDF
Principles of Food Science and Nutritions
Dr. Yogesh Kumar Kosariya
 
PDF
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
demidovs1
 
PPTX
Practice Questions on recent development part 1.pptx
JaspalSingh402
 
PDF
Introduction to Data Science: data science process
ShivarkarSandip
 
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
ghousebhasha2007
 
PPTX
Chapter----five---Resource Recovery.pptx
078bce110prashant
 
PDF
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
PPTX
anatomy of limbus and anterior chamber .pptx
ZePowe
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PDF
6th International Conference on Artificial Intelligence and Machine Learning ...
gerogepatton
 
PPTX
AgentX UiPath Community Webinar series - Delhi
RohitRadhakrishnan8
 
PPTX
Module_II_Data_Science_Project_Management.pptx
anshitanarain
 
PPTX
EE3303-EM-I 25.7.25 electrical machines.pptx
Nagen87
 
Queuing formulas to evaluate throughputs and servers
gptshubham
 
A Framework for Securing Personal Data Shared by Users on the Digital Platforms
ijcncjournal019
 
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
Ppt for engineering students application on field effect
lakshmi.ec
 
Azure-DevOps-Training presentation downloadable
NamanGoyal428595
 
Principles of Food Science and Nutritions
Dr. Yogesh Kumar Kosariya
 
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
demidovs1
 
Practice Questions on recent development part 1.pptx
JaspalSingh402
 
Introduction to Data Science: data science process
ShivarkarSandip
 
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
ghousebhasha2007
 
Chapter----five---Resource Recovery.pptx
078bce110prashant
 
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
anatomy of limbus and anterior chamber .pptx
ZePowe
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
6th International Conference on Artificial Intelligence and Machine Learning ...
gerogepatton
 
AgentX UiPath Community Webinar series - Delhi
RohitRadhakrishnan8
 
Module_II_Data_Science_Project_Management.pptx
anshitanarain
 
EE3303-EM-I 25.7.25 electrical machines.pptx
Nagen87
 

Chap3 inheritance

  • 2. Inheritance basics  Java supports inheritance by allowing one class to incorporate another class into its declaration  Done using a keyword “extend”  Subclass adds to (extends ) the superclass  A class that is inherited is called a superclass  The class that does the inheriting is called subclass  Hence subclass is a specialized version of superclass  It inherits all of the variables and methods
  • 6.  the triangle class includes all TwoDObject and adds the field style and a method area() and method showStyle().  The triangle ‘s style is stored in style.  this can be any string that describes the triangle such as “filled” “outlined”, “transparent”, “isosceles” or “rounded”.  The area() method computes and returns the area of the triangle, showStyle() displays the triangle style.
  • 7.  The general form of a class declaration that inherits a superclass is shown here: class subclass_name extends superclass_name{ //body of the class }  Can mention one superclass for any subclass that you create.  Java does not support the inheritance of multiple super classes into a single subclass  Allows to create a hierarchy of inheritance in which a subclass becomes a super class of another subclass.  No class is superclass of itself.
  • 8. Advantage of inheritance  Once you have created a superclass that defines the attributes common set of objects, it can be used to create any number of more specific subclass
  • 9. Member access and inheritance  A subclass includes all of the members of its superclass it cannot access those members of the superclass that have been declared private class TwoDshape{ private double width; private double height; ….. double area(){ return width*height /2; } }
  • 10.  Program will not compile because the reference to width and height inside the area() method causes an access violation.  Since width and height members are private in nature, they are accessible only by other members of their own class.  Private members are not accessible outside the class, even subclasses.  Java uses accessor methods to provide access to the private members of the class.
  • 13. Constructors and inheritance  The constructor for the superclass constructs the superclass portion of the object, constructor for the subclass constructs the subclass part.  The superclass has no knowledge of access to any element in a subclass., hence their construction must be separate.  The superclass portion of the object is constructed automatically using a default constructor.
  • 18.  Here triangle’s constructor initializes the members of the TwoD class that it inherits along with its own style field.  When both the superclass and the subclass defines as constructor, the process is more complicated because both superclass and subclass constructor must be executed.
  • 19. Using super to call Superclass constructors  A subclass can call a constructor defined by its superclass by the use of the following form of super:  Super(parameter_list);  Here parameter_list specifies any parameter needed by the constructor in the superclass  super() must be the first statement executed inside a subclass constructor  Eg: defines a constructor that initializes width and height varaible in the TwoDShape program.
  • 23.  Triangle() calls super() with parameters w and h.  This causes the TwoDshape() constructor to be called, which initialize width and height using these values.  Triangle no longer initializes these values itself.  TwoDshape can add functionality about which existing subclasses have no knowledge, thus preventing existing code from breaking.  Any form of constructor defined by the superclass can be called by super()  The constructor executed will be one that matches
  • 28.  When a subclass calls super(), it is calling the constructor of its immediate superclass.  Super() always refers to the superclass immediately above the calling class.  It is true even in a multilevel hierarchy  super() must always be the first statement executed inside a constructor.
  • 29. Using super to access superclass members  Using super we can refer to the members of the superclass.  General form is as follows:  super.member  Here the member can be either a method or an instance variable.  Used when the members of the subclass hide members by the same in the superclass.
  • 32. Creating a multilevel hierarchy  Assume that there are three classes A,B and C where C is a subclass of B, which in turn is subclass of A.  Here class C will inherit all of the traits found in subclasses.
  • 36. When are constructors called?  Constructors are called in the order of derivation from superclass to subclass.  Super() must be the first statement executed in subclass constructor.  If super() is not used then the default constructor will be executed.
  • 39. Superclass references and subclass objects  Java is strongly typed language  type compatibility is strictly enforced  A reference variable for one class type cannot normally refer to an object of another class type. class X{ int a; x(int i) { a= i ; } }
  • 40. class Y{ int a; Y(int i){ a=i; } } class incompatibletypes{ X objx1=new X(10); X objx2; Y y=new Y(15); objx2=x; // OK, both of same type objx2=y; //error , not of same type }
  • 41.  Class x and y are physically same, it is not possible to assign an X reference to Y object they have different types.  An object can only refer a object of its type.  A reference variable of superclass can be assigned a reference to an object of any subclass derived from that superclass.  A superclass object can refer to a subclass object.
  • 42. class X{ int a; X (int i) { a=i; } } class Y extends X{ int b; Y(int i, int j){ super(j); b= i; } class SupSub{ public static void main(String args[]){ X x1=new X(10); X x2; Y y=new Y(5,6); x2=x1; System.out.println(“x2.a:”+x2.a); x2=y; System.out.println(“x2.a:”+x2.a); X2.a=19; //OK X2.b=27; //error, x does not have a and b member } }
  • 43. Method overriding  When a method in a subclass has the same return type and signature as a method in its superclass, then the method in the subclass is said to be override the method in superclass.  When an overridden method is called from within a subclass , it will always refer to the version of that method defined by the subclass.  The version defined by the superclass will be hidden.
  • 45.  If the superclass version of an overridden method has to be accessed then, you can do so by using super().  Method overriding occurs only when the signatures of the two methods are identical.  If they are not then, the two methods are simply overloaded.
  • 47. Overridden methods support polyorphsim  Dynamic method dispatch  Mechanism by which call to an overridden method is resolved at run time rather than compile time  Dynamic method dispatch is important because this how java implements run-time polymorphism.  Is superclass reference variable can refer to a subclass objects.  Java determines which version of that method to execute based upon the type of object being referred to at a time the call occurs.
  • 48.  When different types of objects are referred to, different versions of overridden method will be called.  It is the type of the objects being referred to that determines which version of an overridden method will be executed.  If a superclass contains a method that is overridden by a subclass, then when different types of objects are referred to through a superclass reference variable , different versions of the method are executed.
  • 52. Why overridden methods?  It allows to support polymorphism in JAVA  Polymorphism allows a general class to specify methods that will be common to all of its derivatives, while allowing subclasses to define the specific implementation of some or all of those methods.  It helps to implement “one interface, multiple methods”  By combining inheritance with the overridden methods, a superclass can define the general form of methods that will be used by all of its subclasses.
  • 53. Using Abstract Classes  A class that determines the nature of methods that subclasses must implement but does not itself provide any implementation of one or more of these methods, such a class is called abstract class  Defined by specifying the abstract type modifier.  It contains no body and is there not implemented by the superclass  The subclass must override it  abstract type name(parameter_list);
  • 54.  No body is present, the abstract modifier can be used only on normal methods.  Cannot be applied to static methods or to constructors.  A class that contains one or more abstract method should be declared as abstract class with the abstract modifier.  Since abstract class does not define a complete implementation, there can be no objects of an abstract class.
  • 55.  Attempting to create an object of an abstract class, will lead to a compile time error.  When a subclass inherits an abstract class, it should implement all of the abstract method in superclass  If it does not, then the subclass must be specified as abstract.  Hence abstract is inherited until complete implementation of the methods are achieved.
  • 57. Using final  To prevent a method from overridden, specify that method with a keyword “final” at the start of its declaration  Methods declared as final cannot be overridden  example
  • 59. Final prevents inheritance  By having a class declared as final, inheritance can be prevented  By declaring a class as final, implicitly it declares al the methods as final too.  Using abstract and final together is illegal because, abstract depends on the subclass for its implementations and final avoids inheritance. final class A{ //…. } class B extends A{ //error cant have subclass of A //…. }
  • 60. Using final with data members  final can be applied on data members of the class.  If we have members of class as final, the value cannot be changed throughout the lifetime of the program..  Initial values can be given to the variable  Used to create a named constant.
  • 62. Object class  Java defines a class called Object that is an implicit superclass of all other classes  All other classes are subclasses of Object  This means that a reference variable of type Object can refer to an object of any other class.  Object defines the following methods, which are available for all objects
  • 63. Method purpose Object clone() Creates a new object that is same as the object being cloned. boolean equals(Object obj) Determines whether one object is equal to another. void finalize() Called before unused object to be recycled int hashCode() Returns the hash code associated with the invoking object void notify() Resumes execution of a thread waiting on the invoking object void wait() Waits on another thread of execution String toString() Returns a string that describes the object