SlideShare a Scribd company logo
1 of 15
INHERITANCE &
METHOD
OVERRIDING
ADVANCED JAVA
PROGRAMMING
NADAR SARASWATHI COLLEGE OF ARTS AND
SCIENCE,THENI.
B.NIVEGEETHA(I-MSC(CS))
INHERITANCE
 Inheritance is an important pillar of OOP(Object Oriented
Programming). It is the mechanism in java by which one class is
allow to inherit the features(fields and methods) of another class.
Important terminology:
 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). The
subclass can add its own fields and methods in addition to the
super class fields and methods.
 Reusability: Inheritance supports the concept of “reusability”,
i.e. when we want to create a new class and there is already a
class that includes some of the code that we want, we can derive
our new class from the existing class. By doing this, we are
reusing the fields and methods of the existing class.
SINGLE INHERITANCE
 Types of Inheritance in Java
 Below are the different types of inheritance which is supported
by Java.
 Single Inheritance : In single inheritance, subclasses inherit
the features of one super class. In image below, the class A
serves as a base class for the derived class B.
MULTILEVEL
INHERITANCE
 Multilevel Inheritance : In Multilevel Inheritance, a derived
class will be inheriting a base class and as well as the derived
class also act as the base class to other class. In below image, the
class A serves as a base class for the derived class B, which in
turn serves as a base class for the derived class C. In Java, a class
cannot directly access the grandparent’s members.
HIERARCHICAL
INHERITANCE
 Hierarchical Inheritance : In Hierarchical Inheritance, one
class serves as a super class (base class) for more than one sub
class. In below image, the class A serves as a base class for the
derived class B,C and D.
MULTIPLE INHERITANCE
 Multiple Inheritance (Through Interfaces) : In Multiple
inheritance ,one class can have more than one super class and
inherit features from all parent classes. Please note that Java
does not support multiple inheritance with classes. In java, we
can achieve multiple inheritance only through Interfaces. In
image below, Class C is derived from interface A and B.
HYBRID INHERITANCE
 Hybrid Inheritance(Through Interfaces) : It is a mix of two or
more of the above types of inheritance. Since java doesn’t
support multiple inheritance with classes, the hybrid inheritance
is also not possible with classes. In java, we can achieve hybrid
inheritance only through Interfaces.
IMPORTANT FACTS
 Default super class: Except Object class, which has no super class,
every class has one and only one direct super class (single
inheritance). In the absence of any other explicit super class, every
class is implicitly a subclass of Object class.
 Super class can only be one: A super class can have any number of
subclasses. But a subclass can have only one super class. This is
because Java does not support multiple inheritance with classes.
Although with interfaces, multiple inheritance is supported by java.
 Inheriting Constructors: A subclass inherits all the members
(fields, methods, and nested classes) from its super class.
Constructors are not members, so they are not inherited by
subclasses, but the constructor of the super class can be invoked
from the subclass.
 Private member inheritance: A subclass does not inherit the
private members of its parent class.
GENERAL SYNTAX OF
INHERITANCE
public class Class extends
ParentClass
{
//new variable or methods here
}
EXAMPLE
public class Shape {
int length;
int breadth;
}
public class Rectangle extends Shape
{
int area;
public void calculateArea()
{
area = length*breadth;
}
public static void main(String args[])
{
Rectangle r = new Rectangle();
//Assigning values to Shape class attributes
r.length = 10;
r.breadth = 20;
//Calculate the area
r.calcualteArea();
System.out.println("The Area of rectangle of length "" +r.length+"" and breadth
""+r.breadth+"" is ""+r.area+""");
}
METHOD OVERRIDING
 Declaring a method in sub class which is already present
in parent class is known as method overriding. Overriding is
done so that a child class can give its own implementation to a
method which is already provided by the parent class. In this
case the method in parent class is called overridden method and
the method in child class is called overriding method.
class Human
{
//Overridden method
public void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human
{
//Overriding method public void eat()
{
System.out.println("Boy is eating");
}
public static void main( String args[])
{
Boy obj = new Boy();
//This will call the child class version of eat()
obj.eat();
}
}
ADVANTAGE OF METHOD
OVERRIDING
 The main advantage of method overriding is that the class can
give its own specific implementation to a inherited
method without even modifying the parent class code.
 This is helpful when a class has several child classes, so if a child
class needs to use the parent class method, it can use it and the
other classes that want to have different implementation can use
overriding feature to make changes without touching the parent
class code.
RULES OF METHOD
OVERRIDING
 Argument list: The argument list of overriding method (method
of child class) must match the Overridden method(the method of
parent class). The data types of the arguments and their sequence
should exactly match.
 private, static and final methods cannot be overridden as they are
local to the class. However static methods can be re-declared in
the sub class, in this case the sub-class method would act
differently and will have nothing to do with the same static
method of parent class.
 Binding of overridden methods happen at runtime which is
known as dynamic binding
 If a class is extending an abstract class or implementing
an interface then it has to override all the abstract methods
unless the class itself is a abstract class.
END

More Related Content

What's hot

Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++Rabin BK
 
Seminar on java
Seminar on javaSeminar on java
Seminar on javashathika
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingAshita Agrawal
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Sanjit Shaw
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++Sujan Mia
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in javaHitesh Kumar
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Gajendra Singh Thakur
 
Inheritance
InheritanceInheritance
InheritanceTech_MX
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...Simplilearn
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaRahulAnanda1
 

What's hot (20)

Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
 
Inheritance
InheritanceInheritance
Inheritance
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 

Similar to Inheritance ppt

Similar to Inheritance ppt (20)

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Object Oriented Programming - 7.1. Inheritance
Object Oriented Programming - 7.1. InheritanceObject Oriented Programming - 7.1. Inheritance
Object Oriented Programming - 7.1. Inheritance
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java
JavaJava
Java
 
Inheritance
InheritanceInheritance
Inheritance
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING
 
Inheritance used in java
Inheritance used in javaInheritance used in java
Inheritance used in java
 
Ayan Das_25300121057.pptx
Ayan Das_25300121057.pptxAyan Das_25300121057.pptx
Ayan Das_25300121057.pptx
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 
Inheritance in Java.pdf
Inheritance in Java.pdfInheritance in Java.pdf
Inheritance in Java.pdf
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
 
Java(inheritance)
Java(inheritance)Java(inheritance)
Java(inheritance)
 
Inheritance
InheritanceInheritance
Inheritance
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Design
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritance
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 

Recently uploaded

Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Recently uploaded (20)

Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

Inheritance ppt

  • 1. INHERITANCE & METHOD OVERRIDING ADVANCED JAVA PROGRAMMING NADAR SARASWATHI COLLEGE OF ARTS AND SCIENCE,THENI. B.NIVEGEETHA(I-MSC(CS))
  • 2. INHERITANCE  Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in java by which one class is allow to inherit the features(fields and methods) of another class. Important terminology:  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). The subclass can add its own fields and methods in addition to the super class fields and methods.  Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
  • 3. SINGLE INHERITANCE  Types of Inheritance in Java  Below are the different types of inheritance which is supported by Java.  Single Inheritance : In single inheritance, subclasses inherit the features of one super class. In image below, the class A serves as a base class for the derived class B.
  • 4. MULTILEVEL INHERITANCE  Multilevel Inheritance : In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In below image, the class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. In Java, a class cannot directly access the grandparent’s members.
  • 5. HIERARCHICAL INHERITANCE  Hierarchical Inheritance : In Hierarchical Inheritance, one class serves as a super class (base class) for more than one sub class. In below image, the class A serves as a base class for the derived class B,C and D.
  • 6. MULTIPLE INHERITANCE  Multiple Inheritance (Through Interfaces) : In Multiple inheritance ,one class can have more than one super class and inherit features from all parent classes. Please note that Java does not support multiple inheritance with classes. In java, we can achieve multiple inheritance only through Interfaces. In image below, Class C is derived from interface A and B.
  • 7. HYBRID INHERITANCE  Hybrid Inheritance(Through Interfaces) : It is a mix of two or more of the above types of inheritance. Since java doesn’t support multiple inheritance with classes, the hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.
  • 8. IMPORTANT FACTS  Default super class: Except Object class, which has no super class, every class has one and only one direct super class (single inheritance). In the absence of any other explicit super class, every class is implicitly a subclass of Object class.  Super class can only be one: A super class can have any number of subclasses. But a subclass can have only one super class. This is because Java does not support multiple inheritance with classes. Although with interfaces, multiple inheritance is supported by java.  Inheriting Constructors: A subclass inherits all the members (fields, methods, and nested classes) from its super class. Constructors are not members, so they are not inherited by subclasses, but the constructor of the super class can be invoked from the subclass.  Private member inheritance: A subclass does not inherit the private members of its parent class.
  • 9. GENERAL SYNTAX OF INHERITANCE public class Class extends ParentClass { //new variable or methods here }
  • 10. EXAMPLE public class Shape { int length; int breadth; } public class Rectangle extends Shape { int area; public void calculateArea() { area = length*breadth; } public static void main(String args[]) { Rectangle r = new Rectangle(); //Assigning values to Shape class attributes r.length = 10; r.breadth = 20; //Calculate the area r.calcualteArea(); System.out.println("The Area of rectangle of length "" +r.length+"" and breadth ""+r.breadth+"" is ""+r.area+"""); }
  • 11. METHOD OVERRIDING  Declaring a method in sub class which is already present in parent class is known as method overriding. Overriding is done so that a child class can give its own implementation to a method which is already provided by the parent class. In this case the method in parent class is called overridden method and the method in child class is called overriding method.
  • 12. class Human { //Overridden method public void eat() { System.out.println("Human is eating"); } } class Boy extends Human { //Overriding method public void eat() { System.out.println("Boy is eating"); } public static void main( String args[]) { Boy obj = new Boy(); //This will call the child class version of eat() obj.eat(); } }
  • 13. ADVANTAGE OF METHOD OVERRIDING  The main advantage of method overriding is that the class can give its own specific implementation to a inherited method without even modifying the parent class code.  This is helpful when a class has several child classes, so if a child class needs to use the parent class method, it can use it and the other classes that want to have different implementation can use overriding feature to make changes without touching the parent class code.
  • 14. RULES OF METHOD OVERRIDING  Argument list: The argument list of overriding method (method of child class) must match the Overridden method(the method of parent class). The data types of the arguments and their sequence should exactly match.  private, static and final methods cannot be overridden as they are local to the class. However static methods can be re-declared in the sub class, in this case the sub-class method would act differently and will have nothing to do with the same static method of parent class.  Binding of overridden methods happen at runtime which is known as dynamic binding  If a class is extending an abstract class or implementing an interface then it has to override all the abstract methods unless the class itself is a abstract class.
  • 15. END