JAVA 4
JAVA 4
A. True
B. False
Inheritance
• Is-A Relationship
• IS-A is a way of saying: This object is a type of that object. Let us see
how the extends keyword is used to achieve inheritance.
Inheritance
• Now, based on the above example, in Object-Oriented terms, which
the following are true −
1. Animal is the subclass of Mammal class.
2. Animal is the superclass of Reptile class.
3. Mammal and Reptile are subclasses
of Animal class.
4. Dog is the subclass of both Mammal
and Animal classes.
Inheritance
• Now, if we consider the IS-A relationship, we can say −
• Mammal IS-A Animal
• Reptile IS-A Animal
• Dog IS-A Mammal
• Hence: Dog IS-A Animal as well
Inheritance
• Level of inheritance:
1) single
2) Multi level
3) Hierarchical
4) Multiple
5) Hybrid
Inheritance (single)
• When a sub class inherit the property from only one base class, then
it is known as Single Inheritance.
Inheritance (Multi level)
• When a sub class inherit the property fro the class that itself inherit
from another class, then it is known as Multi Level inheritance.
Inheritance (Hierarchical)
• When several sub class inherit the property from single base class,
then it is known as Hierarchical Inheritance.
Inheritance (Multiple)
• When a sub class inherit the property from several base class, then it
is known as Multiple Inheritance.
Inheritance (hybrid)
• In Hybrid Inheritance several inheritance forms are combined.
Polymorphism
• The word polymorphism is combination of two words poly and
morphism. Poly means many and Morphism means form.
• In OOP polymorphism is the ability of object of different types to
respond to functions of the same name. (single method having
multiple functions under the same name. A single-action gets
executed in different ways.)
• The most common use of polymorphism in OOP occurs when a parent
class reference is used to refer to a child class object.
• The user doesn’t have to know the exact type of the object in
advance. The behavior of the object can be implemented at run time.
Polymorphism
• Overloading
• Overriding
Polymorphism (Overriding)
• The benefit of overriding is:
• Ability to define a behavior that's specific to the subclass type, which
means a subclass can implement a parent class method based on its
requirement.
• In object-oriented terms:
• Overriding means to override the functionality of an existing method.
• Constructors cannot be overridden.
Polymorphism (Overriding)
Polymorphism (Rules for method
Overriding)
• The argument list should be exactly the same as that of the overridden
method.
• The return type should be the same or a subtype of the return type declared
in the original overridden method in the superclass.
• The access level cannot be more restrictive than the overridden method's
access level. For example: If the superclass method is declared public then the
overriding method in the sub class cannot be either private or protected.
• Instance methods can be overridden only if they are inherited by the subclass.
• A method declared final cannot be overridden.
• A method declared static cannot be overridden but can be re-declared.
Exercise
• Write a Java program to create a class called Animal with a method called
makeSound(). Create 2 subclasses called Cat and Dog that overrides the
makeSound() method to bark.
• Write a Java program to create a class called Shape with a method called
getArea(). Create 2 subclass called Rectangle and Square that overrides the
getArea() method to calculate the area of (user inputs).
• Write a Java program that creates a class hierarchy for employees of a
company. The base class should be Employee, with subclasses Manager,
Developer, and Programmer. Each subclass should have properties such as
name, address, salary, and job title. Implement methods for calculating
bonuses, generating performance reports, and unique method managing
dev, managing pro, managing manger.