Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

Inheritance, Interface and Polymorphism

Inheritance allows one class to inherit properties from another class. There are different forms of inheritance including single, multiple, hierarchical, and multilevel inheritance. A derived or subclass inherits from a base or superclass. Abstract classes can contain abstract methods that subclasses must implement. Interfaces allow subclasses to implement common methods without inheritance. Polymorphism allows subclasses to override methods from the superclass.

Uploaded by

Andrina Praveen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Inheritance, Interface and Polymorphism

Inheritance allows one class to inherit properties from another class. There are different forms of inheritance including single, multiple, hierarchical, and multilevel inheritance. A derived or subclass inherits from a base or superclass. Abstract classes can contain abstract methods that subclasses must implement. Interfaces allow subclasses to implement common methods without inheritance. Polymorphism allows subclasses to override methods from the superclass.

Uploaded by

Andrina Praveen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

INHERITANCE,

INTERFACE AND
POLYMORPHISM
Chapter-12
Lesson Outcome

✔ Introduction
✔ Need of Inheritance
✔ Different Forms of Inheritance
✔ Derived/Sub and Base/Super Classes
✔ Inheritance and Constructors
✔ Abstract Classes
✔ Interfaces
✔ Polymorphism in Java
Introduction
• Inheritance 🡪 Capability of one class to derive properties from another class.
Automobiles

Vehicles

Car
Derived class inheriting the properties including the methods of the old class known as base
class
Advantage: Code reusability
Once a base class is written and debugged it can be used in various situations without having
to redefine it or rewrite it. Already tested and saved previous code can be reused
Without redefining the old class we can add new properties to derived class and inherited class
member function's can be redefined
Need of Inheritance
• Capability to express the inheritance relationship which ensures closeness with the real –
world models
• Reusability[Allows addition of additional features to an existing class without modifying
it.,
• Eg: Student🡪 GraduateStudent(Derived Class) with new features added that differentiate
the between students and graduate students
• Transitive nature of inheritance
• Person

Student

Graduate Student
Different forms of Inheritance
• New class that is been derived is called derived class/sub-class/target class🡪
inherits from the base class
• Existing class🡪 base class or super class/parent class
• Single Inheritance
• When a subclass inherits only one base class it is known a s single inheritance
X Base Class

Sub Class
Y
Different forms of Inheritance
• Multiple Inheritance🡪When a subclass inherits from multiple base classes it is known as
multiple inheritance
Base classes

X Y Inheritance Graph/Derivation
Derivation from multiple base classes

Z Sub Class
Multiple Inheritance
Different forms of Inheritance
• Hierarchical Inheritance
• When many subclass inherit from a single base class it is known as hierarchical
inheritance
Different forms of Inheritance
• Multilevel Inheritance
• The transitive nature of inheritance is reflected by this form of inheritance
• When a subclass inherits from a class that itself inherits from another class, it is
known as multilevel inheritance

Inheritance hierarchy/Derivation
Relationship between base and derived class
Different forms of Inheritance
• Hybrid inheritance
• When a subclass inherits from multiple base class and all of its base classes inherit
from a single base class
Derived/Sub and Base/Super Classes
• Derived class/Sub class has to identify from which it is derived(base/super class)
• Java way of identifying the base class is to include the base class name in the
derived class definition
• A subclass is defined by using the keyword extends along with the super/base
class name in addition to its own details
• Syntax of defining a derived/sub class
• class<sub class-name> extends < super-class name> {
• :// Members of sub class
•}
Concept of Inheritance
// Java program to illustrate
// Java program to illustrate
// protected modifier
// protected modifier
package p1; package p2;
import p1.*; // importing all classes in package p1

// Class A
// Class B is subclass of A
public class A class B extends A
{ {
public static void main(String args[])
protected void display()
{
{
B obj = new B();
System.out.println(“Old is Gold"); obj.display();
} }
Access Control of Inherited members
• Although a subclass or derived class inherits all the members of its super class yet it can access only those
variables/methods for it has access permissions
• Access of inherited members depends upon their access modifiers
• i.e whether they are private or public or protected or something else
• Access rules of inherited members:
• Private: members are accessible only inside their own class where they have declared
• Public: members are accessible in all the classes whether a subclass or class in the same package or class in
another class along with their own class
• Protected: member s are accessible inside their own class as well as in all subclasses of their class regardless
of whether subclasses exist in the same package or any other package
• default: members with no access specifier
• members are accessible only inside classes that are in same package as that of their own class. These
members are not accessible outside the package; not even though subclass that are in some other package
• Private protected: members are accessible only from subclasses(whether in same aockage or any other
package)
Accessibility of Access Specifiers
Access Control of Inherited members-Example
Access Control of Inherited members-Example
Subtleties of Inheritance in Java
• Every class declared in Java is direct or indirect subclass of a class Object defined
in java.lang package.,
• you create a class with no extends keyword still it will be a subclass., and its
superclass is class Object provided by Java in java.lang package
• Java allows only single inheritance which means that a class has atmost one
immediate superclass
• Every class in Java has one and only one immediate/direct superclass, but might
have several subclasses
• This inheritance is transitive, therefore every class inherits state and behaviour
from all classes which are higher in the class hierarchy
Note:
• ``
Overriding Methods and Hiding Member
Variables
• Sometimes a subclass can use the same names for variables and methods as in the
superclass
• This leads to following behavior
• Methods are overridden
• Variables are hidden
• If a method or variable is used without explicitly specifying its parent class’s name ,only
the subclass member would be used or invoked.
Overriding Methods and Hiding Member
Variables
Referring to Overridden method
• We can still refer to the methods of superclass by using the keyword super
• E.g.,super.overriddenMethod()🡪 refers to the method as it is defined in the superclass
• Class Two extends One{
• :
• public void otherMethod(){
• System.out.println(“I am going to use variable value and invoke method display().”);
• value=25;//this will store 25 in value of class Two
• super.display();//now display of class One shall get invoked
• } I am going to use variable value and invoke method display().”);
• } I am member of class One.
Referring to Hidden Variable
• Superclass’s hidden variables are also can be referred by using keyword super., eg.,
super.hiddenVariable refers to variable hidden Variable of the superclass
• Eg., value=25 can be referred as super.value=25
Use of final
• The final variables are constants the cannot be changed
• The final methods cannot be overridden by subclasses
• A final class cannot be extended
Overriding(Points to Remember)

• A subclass cannot override methods that are declared final in the


superclass
• A subclass must override methods that are declared abstract in the
superclass or the subclass itself must be abstract
Inheritance and Constructors
• Constructors of Super class are not inherited by subclasses
• But it can be called explicitly by using the keyword super
• Eg: if SuperClass has a constructor SuperClass(int initNumber) it can be called by
Super(i)
• Such an explicit call of the constructor of the superclass must be the first
statement in the subclass constructor.
• If a constructor does not explicitly invoke a superclass constructor ,the default
constructor of the superclass is automatically invoked before any statements
within the constructor are executed.
Inheritance and Constructors
Abstract Classes
• From the examples discussed above, super-classes contains elements and properties common to
all of the subclasses
• A concrete superclass is the one whose objects can be declared and created
• Sometimes we need to define a superclass having general characteristics and behavior of its
subclasses but no object of such class should be defined as the class depict a concept only

public abstract class Shape


Abstract Methods
• Methods with no method statements
• Subclasses must provide the method statements for the inherited abstract methods
public abstract class Shape{
String name;
double area;
public void display(){} //not an abstract method as abstract keyword and (;) is missing
public abstract void display();// abstract method
Abstract methods of superclass would require overriding in each subclass i.e we should write the
definition in the subclass
If not overrided,then the applied method may be in appropriate
Abstract Methods
Abstract Classes and Abstract methods-Example
• abstract class MotorBike {
• abstract void brake();}
• class SportsBike extends MotorBike {
• // implementation of abstract method
• public void brake() {
• System.out.println("SportsBike Brake");}}
Output:
• class MountainBike extends MotorBike {
• / implementation of abstract method MountainBike Brake
• public void brake() { SportsBike Brake
• System.out.println("MountainBike Brake");}}
• class Main {
• public static void main(String[] args) {
• MountainBike m1 = new MountainBike();
• m1.brake();
• SportsBike s1 = new SportsBike();
• s1.brake();}}
Abstract Classes and Abstract methods-Example
• //abstract parent class
• abstract class Animal{
• //abstract method
• public abstract void sound();
• }
• //Dog class extends Animal class
• class Dog extends Animal{
Output:
Woof
• public void sound(){
• System.out.println("Woof");
• }
• public static void main(String args[]){
• Animal obj = new Dog();
• obj.sound();
• }
• }

You might also like