It is a
partially implemented class used for developing some of the operations of an
object which are common for all next level sub classes. A class that is
declared as abstract using abstract modifier is called abstract class. It
contains both abstract methods, concrete methods including variables, blocks and
constructors. It is always created as super class next to interface in object’s
inheritance hierarchy for implementing common operations from interface. A method
that is declared as abstract and does not have implementation is known as
abstract methods. Abstract class is basically used to have defined and declared
methods both.
Suppose if we have some method to define but currently we don’t
have its solution, so simply we can declare that method. And in the future we
can define that class by extending it.
Note – We cannot create object
of abstract class.
Example –
abstract class
Mobile{
public abstract void
call();
public abstract void
playMovie();
public abstract void
hasGame();
public abstract void
hasVideochat();
}
abstract class
Samsung extends Mobile{
@Override
public void
call() {
System.out.println("Samsung implemented calling facility");
}
@Override
public void
playMovie() {
System.out.println("Samsung implemented movie facility");
}
public abstract void
hasGame();
public abstract void
hasVideochat();
}
abstract class
Mi extends Samsung{
public void
hasGame()
{
System.out.println("Mi implemented gaming facility");
}
}
class
Apple extends Mi{
@Override
public void
hasVideochat() {
System.out.println("Apple implemented videochat");
}
}
class
Tesr1 {
public static void
main(String[] args) {
Apple a = new
Apple();
a.call();
a.hasGame();
a.hasVideochat();
a.playMovie();
}
}
Output –
Samsung implemented calling facility
Mi implemented gaming facility
Apple implemented videochat
Samsung implemented movie
facility
Example –
abstract class
Mobile{
public abstract void
showConfig();
}
class
Mi extends Mobile{
@Override
public void
showConfig() {
System.out.println("3GB Ram, 32GB Memory, 16MP camera");
}
}
class
Samsung extends Mobile{
@Override
public void
showConfig() {
System.out.println("2GB Ram, 16GB Memory, 13MP camera");
}
}
class
Tesr1 {
public static void
main(String[] args) {
Mi obj
= new Mi();
Samsung obj1 = new Samsung();
show(obj1);
}
public static void
show(Object obj)
{
((Mobile) obj).showConfig();
}
}
Output –
2GB Ram, 16GB Memory, 13MP
camera
Note – In the above example we are passing object to access the
configuration of a particular phone, as all phone extends class called Mobile.
So whatever object we will pass we can see the configuration of that particular
phone.
No comments:
Post a Comment