Welcome back!
in Today's class at my institute
starting of the class
I asked an one question
which is
"why abstract and when to use them in realtime?"
Then my trainer answered for this
with clean explaination
so let's see what my trainer answered for that
Answer from trainer - Why abstraction
he started with one question
"which databases can we connect with the java"
every trainee's answer comes with the different answer
me and my trainees are almost covered all the databases we know
"PostgreSql, MySql , MongoDb, Oracle Db"
then the trainer answered with
Yes, we can connect with java with these database
but the for these database can differ from there behaviour right?
but java is sets one common protocol for databse conectivity
you have to difinetly write some essential methods
which is not written by java
but you have to definetly write these methods (override)
to connect with java for these databases
for these reasons abstraction is useful
from this i have got clear idea about why abstraction
Let's begin - Interface in java
An interface in Java is a blueprint that defines a set of methods a class must implement without providing full implementation details.
It helps achieve abstraction by focusing on what a class should do rather than how it does it.
Interfaces also support multiple inheritance in Java.
- A class must implement all abstract methods of an interface.
- All variables in an interface are public, static, and final by default.
- Interfaces can have default, static, and private methods (Java 8+ and 9+).
Example:
Defines constants and abstract methods, which are implemented by a class.
public interface Company{
int number=10;
public void takeLeave(){
}
public void getSalary(){
}
}
output:
so here proved that
interface abstract methods
sholdn't have method body
now let's remove method body
public interface Company{
int number=10;
public void takeLeave();
public void getSalary();
}
now compiles without any error
ok, now try to access these methods and override them
in abstraction class
we simple used extends keyword to acces methods and override them
but in the interface
going to use the implements keywords
both are doing the same thing in high level
let's use it
public class Employee implements Company{
public void takeLeave(){
System.out.println("8 days");
}
public void getSalary(){
System.out.println("20,000");
}
public static void main(String[] args){
Employee emp=new Employee();
emp.takeLeave();
}
}
output:
8 days
now the output is showing with the method execution
ok now can check that the variable is defaultly with the final and static keyword
public interface Company{
int number=10;
public void takeLeave();
public void getSalary();
}
public class Employee implements Company{
public static void main(String[] args){
Employee emp=new Employee();
emp.number=20;
System.out.println(number);
}
}
here we are tried to change that variable in interface
output
but the output is clearly says that
we can't assign a value to static final variable
hence here proved that the
interface defaultly give a variable to a
final and static keyword
now the one question comeup
it's like abstract class
in abstract we can give all the things explicitly
is the interface and abstract both are same ?
ok
let's clear that
in abstract class we can't create object
but by using inherit
we can create a object for child class
like that
we can create object for interface
but by using implements keyword
in child the objects can act as object for the interface too
but while creating object
one thing automatically generated
you
guessed it
right?
yes it's constructor
constructor creation is possible in subclass of interface
but in abstract class it's not possible
hope u got clear
Takeaways
- Abstraction is used to define a common contract that different classes must follow.
- Real-world examples include database connectivity, where different databases such as PostgreSQL, MySQL, Oracle, and MongoDB provide their own implementations while following a common Java contract.
- An interface specifies what a class should do rather than how it should do it.
- Methods declared in an interface without a body are abstract methods and must be implemented by the class that implements the interface.
- A class uses the
implementskeyword to inherit the contract defined by an interface. - Interface variables are
public static finalby default, making them constants. - Since Java 8, interfaces can contain default and static methods.
- Since Java 9, interfaces can also contain private methods.
- Interfaces help achieve abstraction, loose coupling, and code flexibility.
- A class can implement multiple interfaces, which provides a form of multiple inheritance in Java.
-
Interfaces and abstract classes both support abstraction, but:
- An interface focuses on defining behavior (contract).
- An abstract class can define both behavior and partial implementation.
You cannot create an object directly from an interface, but you can create an object of a class that implements the interface and store it in an interface reference.
Simple Rule to Remember
Use an interface when you want to define a common contract that multiple classes must follow.
Use an abstract class when you want to share common code and behavior among related classes.
This is the core reason interfaces are heavily used in frameworks, database drivers, APIs, and enterprise Java applications.
hope u catchup a topic i covered in this blog
see you on next blog
thank you !
Reference:
interface in java:


Top comments (0)