Uploaded by niludukare

group 'C'

advertisement
INHERITANCE & POLYMORPHISM
•
•
•
Presented By : Prerna Nagori
Nilvanti
INTRODUCTION TO INHERITANCE
• In Object-Oriented Programming (OOP), Inheritance is a key concept that allows a
class to inherit properties and behaviors from another class.
• The primary purpose of inheritance is to promote code reusability.
• A class can be derived from another class. The derived class is called as sub class and the class from where
it is derived is called as super class
1.Superclass (Parent Class):
1. The class whose properties and behaviors are inherited by other classes.
2. Contains common attributes and methods that are shared among its subclasses.
2.Subclass (Child Class):
1. The class that inherits from another class (superclass).
2. Inherits attributes and methods from the superclass and can have its own unique
attributes and methods.
INHERITANCE SYNTAX
public class name extends superclass
{
}
public class Lawyer extends Employee
{
...
}
• By using extend keyword we can inherit in java.
TYPES OF INHERITANCE
• 1.Single-level inheritance
• 2.Multi-level Inheritance
• 3. Multiple inheritance
• 4. Hierarchical Inheritance
• 5. Hybrid Inheritance
SINGLE INHERITANCE
• 1.single-level inheritance Only one class is derived from the parent class.
• In this type of inheritance, the properties are derived from a single parent class
and not more than that.
// SUPERCLASS
CLASS PARENT {
VOID BIKE()
{
SYSTEM.OUT.PRINTLN (“ PARENT HAVE 2 BIKES ");
}
}
// SUBCLASS INHERITING FROM PARENT
CLASS CHILD EXTENDS PARENT {
VOID PHONE()
{
SYSTEM.OUT.PRINTLN (“ CHILD HAVE IT’S OWN PHONE.");
}
}
• In this example, parent is the superclass with a method bike.
• The child class is a subclass of parent and inherits the bike method. Additionally, it has its own
method phone.
• The child class benefits from code reusability by inheriting the common behavior of bike from the
parent class.
2. MULTI-LEVEL INHERITANCE
•
Multi-level inheritance includes the involvement of at least two or more than two classes.
•
One class inherits the features from a parent class and the newly created sub-class becomes the base class
for another new class.
3. MULTIPLE INHERITANCE
•
Multiple inheritances is a type of inheritance where a subclass can inherit features from more than one parent class.
•
Multiple inheritances should not be confused with multi-level inheritance, in multiple inheritances the newly derived
class can have more than one superclass.
•
In java, multiple inheritances can be achieved through interfaces.
4. HYBRID INHERITANCE
•
Hybrid inheritance is a combination of more than two types of inheritances single and multiple. It can be achieved
through interfaces only as multiple inheritance is not supported by Java.
•
It is basically the combination of simple, multiple, hierarchical inheritances.
5. HIERARCHICAL INHERITANCE
•
The type of inheritance where many subclasses inherit from one single class is known as Hierarchical Inheritance.
•
Hierarchical Inheritance a combination of more than one type of inheritance
INTRODUCTION TO POLYMORPHISM
• Polymorphism is considered one of the important features of Object-Oriented Programming.
• Polymorphism allows us to perform a single action in different ways. In other words,
polymorphism allows you to define one interface and have multiple implementations.
TYPES OF POLYMORPHISM
•
There are two types of polymorphism :
1. Compile-time Polymorphism
2. Runtime Polymorphism
1. COMPILE-TIME POLYMORPHISM
• Compile-Time Polymorphism during the compilation stage.
• Here, the overloading method resolution takes place in the compilation stage.
• There are two occurrences of Compile-Time Polymorphism
• A . Method overloding :
• Method Overloading is the process in which the class has two or more methods
with the same name. Nevertheless, the implementation of a specific method
occurs according to the number of parameters in the method call.
Operator Overriding
Operator Overriding a procedure where We can define an
operator in both parent and child classes with the same signature,
but with different operational capability.
Java does not allow Operator Overriding to avoid ambiguities.
2. RUNTIME POLYMORPHISM
• B. Run-time polymorphism :
• In this process, an overridden method is called through the reference variable of a superclass.
• The determination of the method to be called is based on the object being referred to by the
reference variable
THANK YOU !
Download