Unit 4 Part2
Unit 4 Part2
• A sub class has an ‘is a’ relationship with its superclass. This means that
a sub class is a special kind of its super class.
• When we talk in terms of objects, a sub class object can also be treated
as a super class object.
• Object of subclass can access all properties of superclass. But the
reverse of the same is not true
• And is same for reference object, we can assign the reference of a sub
class object to a super class variable type.
• However, the reverse is not true. A reference of a super class object may
not be assigned to a sub class variable. However, when we try to assign
the reference of a Person object to a Student variable, we get a
compilation error.
Polymorphism contin…
Introduction:
• Polymorphism is considered one of the important features of Object-
Oriented Programming.
• The word “poly” means many and “morphs” means forms, So it means
many forms.
• Hence 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.
• Real-life Illustration of Polymorphism in Java: A person at the same time
can have different characteristics. Like a man at the same time is a
father, a husband, and an employee.
Types of Java Polymorphism
• In Java Polymorphism is mainly divided into two types:
– Compile-time Polymorphism
– Runtime Polymorphism
Polymorphism contin…
Compile time polymorphism:
• It is also known as static polymorphism.
• This type of polymorphism is achieved by function overloading or
operator overloading.
• Note: But Java doesn’t support the Operator Overloading.
Method overloading:
• When there are multiple functions with the same name but different
parameters then these functions are said to be overloaded.
• Functions can be overloaded by changes in the number of arguments
or/and a change in the type of arguments.
Polymorphism contin…
Rumtime polymorphism(Dynamic Method Dispatch )
• Runtime polymorphism or Dynamic Method Dispatch is a process in
which a call to an overridden method is resolved at runtime rather than
compile-time.
• This type of polymorphism is achieved by Method Overriding.
• Method overriding occurs when a derived class has a definition for one
of the member functions of the base class. That base function is said to
be overridden.
• In this process, an overridden method is called through the reference
variable of a superclass.
• Means base class object can refer (address) to derived class object
Example
Polymorphism contin…
• Example of rum time polymorphism
Polymorphism contin…
Advantages of Polymorphism in Java
• Increases code reusability by allowing objects of different classes to be
treated as objects of a common class.
• Improves readability and maintainability of code by reducing the
amount of code that needs to be written and maintained.
• Supports dynamic binding, enabling the correct method to be called at
runtime, based on the actual class of the object.
• Enables objects to be treated as a single type, making it easier to write
generic code that can handle objects of different types.
Disadvantages of Polymorphism in Java
• Can make it more difficult to understand the behavior of an object,
especially if the code is complex.
• This may lead to performance issues, as polymorphic behavior may
require additional computations at runtime.
Final keyword in java:
• The final keyword in java is used to restrict the further access to the
relative entity.
• The java final keyword can be used with following :
– Variable
– Method
– Class
• Hence final is a non-access modifier applicable to a variable, method
and classes with some special properties
• What is the use of final keyword???
Final variable:
• If you make any variable as final, you cannot change the value of final
variable(It will be constant).
• Here when you call run() method
It will give compile time error at line
where you are trying to change the value
of final variable to 400.
• Static variables can be final
• The final variable that is not initialized at the time of declaration is
known as blank final variable.
• A static final variable that is not initialized at the time of declaration is
known as static blank final variable.
Rules of final variable:
• Final variable must initialize to specific value.
• Blank final variable can be initialized only in constructor.
• static blank final variable can be initialized only in static block.
Final Method:
Java final method:
• In Java, the final method cannot be overridden by the child class. For
example see the following example
• This is useful for methods that are part of a base class and should not be
modified by subclasses.
Final class:
Java final class
• If you make any class as final, you cannot extend it.
• When you try to extend subclass by superclass which is final, compile
time error will raise as follows
• This is useful for classes that are intended to be used as is and should
not be modified or extended.
Abstract classes and methods:
• The major use of abstract classes and methods is to achieve abstraction in
Java.
• Abstraction is an important concept of object-oriented programming that
allows us to hide unnecessary details and only show the needed
information.
• This allows us to manage complexity by omitting or hiding details with a
simpler, higher-level idea.
• A practical example of abstraction can be motorbike brakes. We know what
brake does. When we apply the brake, the motorbike will stop. However,
the working of the brake is kept hidden from us.
• The major advantage of hiding the working of the brake is that now the
manufacturer can implement brake differently for different motorbikes,
however, what brake does will be the same.
Ways to achieve Abstraction
• There are two ways to achieve abstraction in java
– Abstract class (with Abstract method)
– Interface
Abstract classes and methods contin…
Abstract Method:
• A method that doesn't have its body is known as an abstract method.
• We use the same abstract keyword to create abstract methods. For
example,
Important points:
• An abstract class can have both the regular methods and abstract
methods.
• If a class has abstract method, then that class must be abstract.
• The abstract class in Java cannot be instantiated (we cannot create
objects of abstract classes).
• But we can create reference of abstract claases.
• Other classes extend abstract classes.
• If abstract class have abstract method, its child classes must give
definition for such methods by method overriding
Abstract classes and methods contin…
Implementing Abstract Methods
• If the abstract class includes any abstract method, then all the child
classes inherited from the abstract superclass must provide the
implementation of the abstract method. For example,
Abstract classes and methods contin…
Key Points to Remember
• We use the abstract keyword to create abstract classes and methods.
• An abstract method doesn't have any implementation (method body).
• A class containing abstract methods should also be abstract.
• We cannot create objects of an abstract class. But we can create
reference of abstract class
• To implement features of an abstract class, we inherit subclasses from it
and create objects of the subclass.
• A subclass must override all abstract methods of an abstract class.
• However, if the subclass is declared abstract, it's not mandatory to
override abstract methods.
• We can access the static attributes and methods of an abstract class
using the reference of the abstract class. For example,
Animal.staticMethod();
……Where Animal is name of abstract class
Abstract classes and methods contin…
• Keep in mind these key point about abstract class and method:
Interface:
• An interface is a fully abstract class. It includes a group of only abstract
methods, with no body
• It is used to achieve abstraction and multiple inheritances in Java
Declaration:
• To declare an interface, use the interface
keyword as below.
Interface implementation:
• A class that implements an interface must implement all the methods
declared in the interface. To implement the interface use the implements
keyword.
• Such class must implements there methods as a public methods
Interface contin…
Simple java program for interface
• Class ‘Student’ have implemented abstract method ‘StudentData()’ from
interface ‘College’
Interface contin…
Simple java program for interface
• Classes ‘SBI’ and ‘PNB’ have implemented abstract method
‘rateOfInterest()’ from interface ‘Bank’
interface contin…
Multiple inheritance using interface:
• If a class implements multiple interfaces, or an interface extends
multiple interfaces, it is known as multiple inheritance.
Example:
Important points in Interface:
• We can’t create an instance of the interface but we can make the reference
of it that refers to the Object of its implementing class.
• A class can implement more than one interface.
• An interface can extend to another interface or interfaces
• A class that implements the interface must implement all the methods in the
interface.
• All the methods are public and abstract. And all the fields are public, static,
and final.
• It is used to achieve multiple inheritances.
• It is used to achieve loose coupling.
• Inside the Interface not possible to declare instance variables because by
default variables are public static final.
• Inside the Interface, constructors are not allowed.
• Inside the interface main method is not allowed.
• Inside the interface, static, final, and private methods declaration are not
possible.
Interface contin….
• Since Java 8, we can have method body in interface. But we need to
make it default method.
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 creates and returns the exact copy (clone) of
CloneNotSupportedException 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 causes the current thread to wait for the
InterruptedException specified milliseconds, until another thread
notifies (invokes notify() or notifyAll()
method).
ObjectClass contin…
public final void wait(long timeout,int causes the current thread to wait for
nanos)throws InterruptedException the specified milliseconds and
nanoseconds, until another thread
notifies (invokes notify() or notifyAll()
method).
public final void wait()throws causes the current thread to wait,
InterruptedException until another thread notifies (invokes
notify() or notifyAll() method).
End of chapter……………………..
Thank you