INTERFACES
Abstract class interface
Abstract public (here all the public methods of abstract class
are already included in interface so no need to write it again)
Extends implements
The right one is interface and the left one is abstract class.
In abstract class the class has abstract methods and later that
class is been extended (or) inherited and then the abstract
classes are made concrete by defining them in subclass again.
Abstract classes are mainly used for polymorphism by
inheriting all features of the abstract class except the private
ones.
Inheritance: One class borrows features of another (subclass
↔ superclass).
Abstract class:
Contains one or more abstract methods.
Used for both inheritance and polymorphism.
Can also provide concrete methods.
Interface:
Only for polymorphism.
Nothing to borrow (no method bodies).
An interface in Java is like an abstract class with all
abstract methods.
It is used only for achieving polymorphism.
Unlike abstract classes, an interface does not provide any
concrete features to subclasses.
If you just want polymorphism and no inheritance of code,
you use interfaces.
In interface the class is not borrowed it is actually implemented.
1. Interface = abstract class with all methods abstract.
2. Used only to achieve polymorphism.
3. Methods are public abstract by default.
4. Variables inside interface = public static final.
5. A class can implement multiple interfaces. One
abstract class can be extended only one concrete class
but one interface can be implemented to many concrete
classes
6. Interface reference can hold object of implementing class
→ runtime polymorphism.
Here also we can create reference for interface but not object
Test1 t = new test1() => cannot create object
And we can call the methods of implemented class also
In the next example we have inherited from smartphone from
phone and implemented the features of camera and music
player.
Phone is a concrete class.
It has two methods: call() and sms().
Any Smartphone (which is a Phone) will inherit these.
interface Camera → Defines methods click() and record().
interface MusicPlayer → Defines methods play(), pause(),
stop().
These are abstract contracts → Any class that says I am a
Camera or I am a MusicPlayer must provide implementations.
phone p =s this is a super class variable and s is a subclass
object
[Link]() possible but [Link]() not possible
A store sells products to many customers. Among them, some
are member customers.
Whenever the store starts a sale/discount, it must inform
all its registered members.
A member customer agrees to receive calls (callbacks)
from the store.
This means the customer must implement a Member
interface, which declares a callback() method.
The store will maintain a list of member customers,
allow new members to register, and notify them using
the callback() method whenever a sale starts.
You need to implement:
1. An interface Member with the callback() method.
2. A Store class that manages member registrations and
invokes the callback method when a sale starts.
3. A Customer class that implements the Member interface.
4. A main application to test the scenario.
Member[] members = new Member[100]; =>here Member is a
interface and we cannot create a objeect for interface only
reference variable
new Member[100] creates space for 100 references
(pointers) to Member objects.
By default, all 100 slots are initialized to null.
Later, when you register a Customer (which implements
Member), you actually assign a concrete object into one of
those slots.
NOTE:
(1)Interfaces cannot be private.
(2)We can have identifier inside interface and they are static
and final by default.
(3)By default methods inside interface should not have any
body
But when the method is static then it can have body
(4)
Here we accessed the static variable inside the interface and
also the static method inside the interface without actually
creating a object.
(5)
An interface can extend from another interface
(6)Here instead of public static we can use default also
NOTE:
We can have private methods inside interface also.
This default method is called has helper method.
Here, meth5() is default, so any class implementing Test
automatically gets this method.
meth5() calls meth3().
So, when an implementing class calls meth5(), it will indirectly
execute the private logic in meth3().
This is useful when you want to reuse common code inside
the interface without forcing every implementing class to
repeat it.
NEED FOR DEFAULT METHOD => It allows an interface to
provide a concrete implementation (so classes that
implement it don’t have to override it unless they want).
MULTIPLE INHERITANCE (vs) INTERFACE:
Java does support multiple inheritance
1. Multiple Inheritance in C++
C++ allows multiple inheritance → a class can directly
inherit from multiple parent classes.
Example from the image:
o SmartPhone inherits from Phone, Camera, and
MusicPlayer.
o Amphibian inherits from both Terrestrial and Aquatic.
So in C++:
o Smartphone is-a Phone, is-a Camera, is-a
MusicPlayer.
o Amphibian is-a Terrestrial, is-a Aquatic.
Problem:
This leads to confusion and ambiguity
. Java’s Perspective
Java does not support multiple inheritance of
classes → one class can only extend one superclass.
Instead, Java uses interfaces to represent additional
capabilities.
Example:
o Smartphone is fundamentally a Phone (class).
o Smartphone implements Camera (interface) and
implements MusicPlayer (interface).
o Amphibian is fundamentally a Terrestrial (class).
o Amphibian implements Aquatic (interface).
Key difference in thinking:
C++: “Smartphone is Phone, Camera, MusicPlayer.”
Java: “Smartphone is Phone, but it has features of Camera
and MusicPlayer.”
3. Real-Life Analogy
Car & Music Player
o A Car (class) can have a MusicPlayer interface on
its dashboard.
o But Car is not a MusicPlayer; it just has the
capability.
o Example: Suzuki is a Car (not a MusicPlayer), but it
has music player functionality.
Similarly:
o A Smartphone is a Phone → but it can act as a
Camera & MusicPlayer.