SlideShare a Scribd company logo
1 of 45
Inheritance
Prepared by
Dr.K.Vijayalakshmi & Mrs.N.Nithya
Department of Computer science and Engineering
Ramco Institute of Technology
Rajapalayam
CS8392- UNIT II INHERITANCE AND INTERFACES 9
• Inheritance – Super classes- sub classes –Protected
members – constructors in sub classes- the Object
class – abstract classes and methods- final methods
and classes – Interfaces – defining an interface,
implementing interface, differences between classes
and interfaces and extending interfaces - Object
cloning -inner classes, Array Lists - Strings
Contents
• Inheritance
• Super class and sub class
• Extends and protected keyword
• Types of inheritance
• Single inheritance
• Method overloading and method overriding
• Super keyword and its use
• Subclass constructor
• Object class
Concept of Inheritance
Source: NPTEL video – Programming in Java
Inheritance
• Inheritance can be defined as the procedure or mechanism of
acquiring all the properties and behaviour of one class to
another.
• Process of deriving a new class from the existing class is called
inheritance. New class is called sub class and Existing class is
called super class.
• It is the act of mechanism through which the object of one
class can acquire(inherit) the methods and data members of
another class.
Super class & Sub class
• Super Class:
• The class whose features are inherited is known as super
class(or a base class or a parent class).
• Sub Class:
• The class that inherits the other class is known as sub
class(or a derived class, extended class, or child class).
• A sub class is a specialized version of the super class.
• The subclass can add its own fields and methods in
addition to the superclass fields and methods.
Example:
Vehicle
Car Truck
Example:
• 2Dpoint and 3Dpoint
2Dpoint – superclass and 3Dpoint – subclass
• Person and Student
Person – superclass and Student – subclass
• Person and Employee
Person – superclass and Student – subclass
Example of Inheritance
• Superclass Fields : x, y
• Subclass Fields : x & y (Inherited), z
2DPoint
3DPoint
Reusability
• Inheritance supports the concept of “reusability”, i.e.
• When we want to create a new class, which is more specific
to a class that is already existing, we can derive our new
class from the existing class.
• By doing this, we are reusing the fields and methods of the
existing class.
Syntax - extends keyword
class SubClassName extends SuperClassName
{
//fields and methods
}
The meaning of extends is to increase the functionality.
Eg. Class student extends person
{
}
protected keyword
• A class’s private members are accessible only within the class
itself.
• A superclass’s protected members can be accessed
• by members of that superclass,
• by members of its subclasses and
• by members of other classes in the same package
Types of Inheritance
• Single inheritance - One sub
class is derived from one
super class
• multilevel inheritance - A
sub class is derived from
another sub class which is
derived from a super class.
• hierarchical inheritance-
Two or more sub classes
have the same super class
• Person – Student
• Animal – Dog
Student – Exam – Result
Person – Student
Person – Employee
https://nptel.ac.in/courses/106105191/13
Person
Student
Student
Exam
Result
Person
Student Employee
Purpose of inheritance
• Inheritance is the process by which objects of one class
acquire the properties and methods of another class.
• It provides the idea of reusability.
• We can add additional features to an existing class without
modifying it by deriving a new class from it- Extendibility
• Save development time
Single inheritance
• Deriving a single sub class from a single super class is called
single inheritance. It is derived using the keyword ‘extends’
class A
{ }
class B extends A
{ }
• class A is the super class from which the class B is derived.
• Class B inherits all the public and protected members of class
A but class B cannot access private members of class A.
Example for Single Inheritance
Circle
Sphere
• Example Programs
Output
Explanation
• Superclass Circle has a method display()
• Subclass Sphere has a method with the same name display()
• When calling display() with the subclass object, subclass’s display()
method is invoked.
Method Overloading
• Methods in a class having the same method name with
different number and type of arguments is said to be
method overloading.
• Method overloading is done at compile time.
• Number of parameter can be different.
• Types of parameter can be different.
Method Overriding
• The methods in the super class and its sub classes have the
same method name with same type and the same number
of arguments (same signature), this is referred as method
overriding.
• Method in sub classes overrides the method in super class.
• Eg. display() method in class Circle & class Sphere.
Class Poll
Class Poll
In which java oops feature one object can acquire all
the properties and behaviours of the parent object?
• Encapsulation
• Inheritance
• Polymorphism
• None of the above
Class Poll
What is subclass in java?
A subclass is a class that extends another class
A subclass is a class declared inside a class
Both above.
None of the above.
Class Poll
If class B is subclassed from class A then which is the
correct syntax
• class B:A{}
• class B extends A{}
• class B extends class A{}
• class B implements A{}
Super Keyword
1. To access the data members of immediate super
class when both super and sub class have member
with same name.
2. To access the method of immediate super class
when both super and sub class have methods with
same name.
3. To explicitly call the no-argument(default) and
parameterized constructor of super class
1. To access the data members of immediate
super class – both super and sub class should
have same member variable name
class Animal
{
String color="white";
}
class Dog extends Animal
{
String color="black";
void printColor()
{
System.out.println(color); //prints color of Dog class
System.out.println(super.color); //prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
} }
• In the above example, Animal and Dog both classes have a common
property color. If we print color property, it will print the color of
current class by default. To access the parent property, we need to
use super keyword as super.color
2. To access methods of immediate super
class – both super class and subclass should
have same method name and method
signature (Overridden method).
/* Base class Person */
class Person
{
void message()
{
System.out.println("This is person class");
}
}
/* Subclass Student */
class Student extends Person
{
void message()
{
System.out.println("This is student class");
}
// Note that display() is only in Student
class
void display()
{
message();
super.message();
}
}
/* Driver program to test */
class Test
{
public static void main(String args[])
{
Student s = new Student();
// calling display() of Student
s.display();
}
}
3. To explicitly call the no-argument(default)
and parameterized constructor of super class
Constructors in sub classes
subclass constructor
• Constructor which is defined in the subclass is called as
subclass constructor.
• Used to call the superclass constructor and construct the
instance variables of both superclass and subclass.
• Subclass constructor uses the keyword super() to invoke the
superclass constructor
• On object creation of sub class, first super class constructor
and then sub class constructor will be called.
Constructors in sub classes
super() can call both
• default as well as
• parameterized constructors
depending upon the situation.
Points to Remember
• Call to super() must be first statement in subclass constructor.
• If a constructor does not explicitly invoke a superclass constructor, the
Java compiler automatically inserts a call to the no-argument(default)
constructor of the superclass.
Class Poll
Order of execution of constructors in Java Inheritance is
• Base to derived class
• Derived to base class
• Random order
• none
Class Poll
Inheritance relationship in Java language is
• Association
• Is-A
• Has-A
• None
Class Poll
Advantage of inheritance in java programming is/are
• Code Re-usability
• Class Extendibility
• Save development time
• All
The object class
• Object class is present in java.lang package.
• Every class in Java is directly or indirectly derived from
the Object class.
• If a Class does not extend any other class, then it is direct
child class of Object and if extends other class then it is an
indirectly derived. Therefore the Object class methods are
available to all Java classes.
• Hence Object class acts as a root of inheritance hierarchy in
any Java Program.
Method Description
public final Class getClass() returns the Class class object of this object. The Class class can further be used to
get the metadata of this class.
public int hashCode() returns the hashcode number for this object.
public boolean equals(Object obj) compares the given object to this object.
protected Object clone() throws
CloneNotSupportedException
creates and returns the exact copy (clone) of this object.
public String toString() returns the string representation of this object.
public final void notify() wakes up single thread, waiting on this object's monitor.
public final void notifyAll() wakes up all the threads, waiting on this object's monitor.
public final void wait(long timeout)throws
InterruptedException
causes the current thread to wait for the specified milliseconds, until another
thread notifies (invokes notify() or notifyAll() method).
public final void wait(long timeout,int
nanos)throws InterruptedException
causes the current thread to wait for the specified milliseconds and nanoseconds,
until another thread notifies (invokes notify() or notifyAll() method).
public final void wait()throws
InterruptedException
causes the current thread to wait, until another thread notifies (invokes notify() or
notifyAll() method).
protected void finalize()throws Throwable is invoked by the garbage collector before object is being garbage collected.
How does these methods work?
• https://www.geeksforgeeks.org/object-class-in-java/
• Object class documentation Java SE 7
• https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html
• Object class Tutorial
• https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html
Method overloading Method overriding
• Methods in a class having the same
method name with different number
and type of arguments (different
signature) is said to be method
overloading.
• Method overloading is done at
compile time. Thus Compile Time
Polymorphism is achieved.
• Number of parameter can be
different.
• Types of parameter can be different.
• The methods in the super class and
its sub classes have the same method
name with same type and the same
number of arguments (same
signature), this is referred as method
overriding.
• Method overriding is one of the way
by which java achieve Run Time
Polymorphism.
• Number of parameters are same.
• Types of parameter are same.

More Related Content

What's hot

Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introductionSohanur63
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streamsbabak danyal
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Paumil Patel
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In JavaManish Sahu
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
Friend function
Friend functionFriend function
Friend functionzindadili
 
Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionPritom Chaki
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPTPooja Jaiswal
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 

What's hot (20)

Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Inheritance
InheritanceInheritance
Inheritance
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Friend function
Friend functionFriend function
Friend function
 
Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaion
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Inner class
Inner classInner class
Inner class
 
Java Beans
Java BeansJava Beans
Java Beans
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Java packages
Java packagesJava packages
Java packages
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 

Similar to Java Inheritance - sub class constructors - Method overriding

Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritanceKalai Selvi
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritanceteach4uin
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritanceteach4uin
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1PRN USM
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritancemcollison
 
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 examplesSunil Kumar Gunasekaran
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance ConceptsVicter Paul
 
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.pptAshwathGupta
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptxakila m
 
البرمجة الهدفية بلغة جافا - الوراثة
البرمجة الهدفية بلغة جافا - الوراثةالبرمجة الهدفية بلغة جافا - الوراثة
البرمجة الهدفية بلغة جافا - الوراثةMahmoud Alfarra
 

Similar to Java Inheritance - sub class constructors - Method overriding (20)

Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Inheritance
InheritanceInheritance
Inheritance
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
Inheritance
Inheritance Inheritance
Inheritance
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
 
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
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
 
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
البرمجة الهدفية بلغة جافا - الوراثة
البرمجة الهدفية بلغة جافا - الوراثةالبرمجة الهدفية بلغة جافا - الوراثة
البرمجة الهدفية بلغة جافا - الوراثة
 

Recently uploaded

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 

Recently uploaded (20)

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 

Java Inheritance - sub class constructors - Method overriding

  • 1. Inheritance Prepared by Dr.K.Vijayalakshmi & Mrs.N.Nithya Department of Computer science and Engineering Ramco Institute of Technology Rajapalayam
  • 2. CS8392- UNIT II INHERITANCE AND INTERFACES 9 • Inheritance – Super classes- sub classes –Protected members – constructors in sub classes- the Object class – abstract classes and methods- final methods and classes – Interfaces – defining an interface, implementing interface, differences between classes and interfaces and extending interfaces - Object cloning -inner classes, Array Lists - Strings
  • 3. Contents • Inheritance • Super class and sub class • Extends and protected keyword • Types of inheritance • Single inheritance • Method overloading and method overriding • Super keyword and its use • Subclass constructor • Object class
  • 4. Concept of Inheritance Source: NPTEL video – Programming in Java
  • 5. Inheritance • Inheritance can be defined as the procedure or mechanism of acquiring all the properties and behaviour of one class to another. • Process of deriving a new class from the existing class is called inheritance. New class is called sub class and Existing class is called super class. • It is the act of mechanism through which the object of one class can acquire(inherit) the methods and data members of another class.
  • 6. Super class & Sub class • Super Class: • The class whose features are inherited is known as super class(or a base class or a parent class). • Sub Class: • The class that inherits the other class is known as sub class(or a derived class, extended class, or child class). • A sub class is a specialized version of the super class. • The subclass can add its own fields and methods in addition to the superclass fields and methods.
  • 8. Example: • 2Dpoint and 3Dpoint 2Dpoint – superclass and 3Dpoint – subclass • Person and Student Person – superclass and Student – subclass • Person and Employee Person – superclass and Student – subclass
  • 9. Example of Inheritance • Superclass Fields : x, y • Subclass Fields : x & y (Inherited), z 2DPoint 3DPoint
  • 10. Reusability • Inheritance supports the concept of “reusability”, i.e. • When we want to create a new class, which is more specific to a class that is already existing, we can derive our new class from the existing class. • By doing this, we are reusing the fields and methods of the existing class.
  • 11. Syntax - extends keyword class SubClassName extends SuperClassName { //fields and methods } The meaning of extends is to increase the functionality. Eg. Class student extends person { }
  • 12. protected keyword • A class’s private members are accessible only within the class itself. • A superclass’s protected members can be accessed • by members of that superclass, • by members of its subclasses and • by members of other classes in the same package
  • 13. Types of Inheritance • Single inheritance - One sub class is derived from one super class • multilevel inheritance - A sub class is derived from another sub class which is derived from a super class. • hierarchical inheritance- Two or more sub classes have the same super class • Person – Student • Animal – Dog Student – Exam – Result Person – Student Person – Employee https://nptel.ac.in/courses/106105191/13 Person Student Student Exam Result Person Student Employee
  • 14. Purpose of inheritance • Inheritance is the process by which objects of one class acquire the properties and methods of another class. • It provides the idea of reusability. • We can add additional features to an existing class without modifying it by deriving a new class from it- Extendibility • Save development time
  • 15. Single inheritance • Deriving a single sub class from a single super class is called single inheritance. It is derived using the keyword ‘extends’ class A { } class B extends A { } • class A is the super class from which the class B is derived. • Class B inherits all the public and protected members of class A but class B cannot access private members of class A.
  • 16. Example for Single Inheritance Circle Sphere
  • 18. Output Explanation • Superclass Circle has a method display() • Subclass Sphere has a method with the same name display() • When calling display() with the subclass object, subclass’s display() method is invoked.
  • 19. Method Overloading • Methods in a class having the same method name with different number and type of arguments is said to be method overloading. • Method overloading is done at compile time. • Number of parameter can be different. • Types of parameter can be different.
  • 20. Method Overriding • The methods in the super class and its sub classes have the same method name with same type and the same number of arguments (same signature), this is referred as method overriding. • Method in sub classes overrides the method in super class. • Eg. display() method in class Circle & class Sphere.
  • 22. Class Poll In which java oops feature one object can acquire all the properties and behaviours of the parent object? • Encapsulation • Inheritance • Polymorphism • None of the above
  • 23. Class Poll What is subclass in java? A subclass is a class that extends another class A subclass is a class declared inside a class Both above. None of the above.
  • 24. Class Poll If class B is subclassed from class A then which is the correct syntax • class B:A{} • class B extends A{} • class B extends class A{} • class B implements A{}
  • 25. Super Keyword 1. To access the data members of immediate super class when both super and sub class have member with same name. 2. To access the method of immediate super class when both super and sub class have methods with same name. 3. To explicitly call the no-argument(default) and parameterized constructor of super class
  • 26. 1. To access the data members of immediate super class – both super and sub class should have same member variable name
  • 27. class Animal { String color="white"; } class Dog extends Animal { String color="black"; void printColor() { System.out.println(color); //prints color of Dog class System.out.println(super.color); //prints color of Animal class } } class TestSuper1{ public static void main(String args[]){ Dog d=new Dog(); d.printColor(); } }
  • 28. • In the above example, Animal and Dog both classes have a common property color. If we print color property, it will print the color of current class by default. To access the parent property, we need to use super keyword as super.color
  • 29. 2. To access methods of immediate super class – both super class and subclass should have same method name and method signature (Overridden method).
  • 30. /* Base class Person */ class Person { void message() { System.out.println("This is person class"); } } /* Subclass Student */ class Student extends Person { void message() { System.out.println("This is student class"); } // Note that display() is only in Student class void display() { message(); super.message(); } }
  • 31. /* Driver program to test */ class Test { public static void main(String args[]) { Student s = new Student(); // calling display() of Student s.display(); } }
  • 32. 3. To explicitly call the no-argument(default) and parameterized constructor of super class
  • 34. subclass constructor • Constructor which is defined in the subclass is called as subclass constructor. • Used to call the superclass constructor and construct the instance variables of both superclass and subclass. • Subclass constructor uses the keyword super() to invoke the superclass constructor • On object creation of sub class, first super class constructor and then sub class constructor will be called.
  • 35. Constructors in sub classes super() can call both • default as well as • parameterized constructors depending upon the situation.
  • 36.
  • 37.
  • 38. Points to Remember • Call to super() must be first statement in subclass constructor. • If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument(default) constructor of the superclass.
  • 39. Class Poll Order of execution of constructors in Java Inheritance is • Base to derived class • Derived to base class • Random order • none
  • 40. Class Poll Inheritance relationship in Java language is • Association • Is-A • Has-A • None
  • 41. Class Poll Advantage of inheritance in java programming is/are • Code Re-usability • Class Extendibility • Save development time • All
  • 42. The object class • Object class is present in java.lang package. • Every class in Java is directly or indirectly derived from the Object class. • If a Class does not extend any other class, then it is direct child class of Object and if extends other class then it is an indirectly derived. Therefore the Object class methods are available to all Java classes. • Hence Object class acts as a root of inheritance hierarchy in any Java Program.
  • 43. Method Description public final Class getClass() returns the Class class object of this object. The Class class can further be used to get the metadata of this class. public int hashCode() returns the hashcode number for this object. public boolean equals(Object obj) compares the given object to this object. protected Object clone() throws CloneNotSupportedException creates and returns the exact copy (clone) of this object. public String toString() returns the string representation of this object. public final void notify() wakes up single thread, waiting on this object's monitor. public final void notifyAll() wakes up all the threads, waiting on this object's monitor. public final void wait(long timeout)throws InterruptedException causes the current thread to wait for the specified milliseconds, until another thread notifies (invokes notify() or notifyAll() method). public final void wait(long timeout,int nanos)throws InterruptedException causes the current thread to wait for the specified milliseconds and nanoseconds, until another thread notifies (invokes notify() or notifyAll() method). public final void wait()throws InterruptedException causes the current thread to wait, until another thread notifies (invokes notify() or notifyAll() method). protected void finalize()throws Throwable is invoked by the garbage collector before object is being garbage collected.
  • 44. How does these methods work? • https://www.geeksforgeeks.org/object-class-in-java/ • Object class documentation Java SE 7 • https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html • Object class Tutorial • https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html
  • 45. Method overloading Method overriding • Methods in a class having the same method name with different number and type of arguments (different signature) is said to be method overloading. • Method overloading is done at compile time. Thus Compile Time Polymorphism is achieved. • Number of parameter can be different. • Types of parameter can be different. • The methods in the super class and its sub classes have the same method name with same type and the same number of arguments (same signature), this is referred as method overriding. • Method overriding is one of the way by which java achieve Run Time Polymorphism. • Number of parameters are same. • Types of parameter are same.