Abstract Class
Abstract Class
in Java
Dr Richa
Assistant Professor
Abstract Means
}
Example of Abstract class and Method
abstract class Base {
public Base() public class Main
{ {
System.out.println("super base class"); public static void main(String[] args)
{
}
public void method1() { Base b=new Derived();
System.out.println("Method1 super base class"); b.method1();
} b.method2();
}
abstract public void method2();
}
}
class Derived extends Base{
public void method2() { Output:
System.out.println("Method2 super base class"); super base class
Method1 super base class
}
Method2 super base class
}
How abstract class is useful
abstract class Hospital Class MyHospital extends Hospital
{
{ MyHospital()
{
abstract void emergency();
}
abstract void appointment(); void Emergency()
{
abstract void admit(); }
void appointment()
abstract void billing(); {
}
} ……
……
}
Explanation
• The abstract class only gives you the standard; the hospital class gives you
standard of an ideal hospital where emergency, appointment etc will be
there. Once you create your own hospital, say Fortis, where you follow all the
standard then only you will be able to create the instance of the hospital.
• We cannot create instance of abstract class: The body which gives the
standard they cannot show you the hospital, they only gives you guideline. If
fortis is opening there hospital then they will follow the standard and can
show you the hospital.
• But the abstract class can have the reference. If you have the reference then
you can create the object of concrete subclass. Since everyone will say that
fortis is hospital which has features as a standard hospital. That is why
reference of the hospital is allowed.
Another Example
Class MyKFC extends KFC
abstract class KFC {
MyKFC()
{
{
public KFC() ……
{ }
void billing()
} {
void makeFood() }
{ void offer()
{
…… }
} void
abstract void billing(); }
}
public class Main{
public static void main(String[] args) {
Test t=new Test();
}
}
Output: error: no interface expected here
error: Test is abstract; cannot be instantiated
Example Interface
interface Test {
void method1(); public class Main{
void method2(); public static void main(String[] args)
} {
class Test1 implements Test{ Test t=new Test1();
public void method1() { t.method1();
System.out.println("Method1 of class Test1"); t.method2();
} }
public void method2() {
}
Output:
System.out.println("Method2 of class Test1");
Method1 of class Test1
}
Method2 of class Test1
public void method3() {
System.out.println("Method3 of class Test1");
}
}
What would be the output?
interface Test {
void method1(); public class Main{
void method2(); public static void main(String[] args)
} {
class Test1 implements Test{ Test t=new Test1();
public void method1() { t.method1();
System.out.println("Method1 of class Test1"); t.method2();
} t.method3();
public void method2() {
}
}
System.out.println("Method2 of class Test1");
Output:
}
error: cannot find symbol
public void method3() { Example of Dynamic Method Dispatch or
System.out.println("Method3 of class Test1"); Runtime Polymorphism
}
}
To call Method3 Create reference of Test1
interface Test {
void method1(); public class Main{
void method2(); public static void main(String[] args)
} {
class Test1 implements Test{ Test1 t=new Test1();
public void method1() { t.method1();
System.out.println("Method1 of class Test1"); t.method2();
} t.method3();
public void method2() {
}
}
System.out.println("Method2 of class Test1");
Output:
}
Method1 of class Test1
public void method3() { Method2 of class Test1
System.out.println("Method3 of class Test1"); Method3 of class Test1
}
}
Example of Interface in Java Class
{
Phone
void call()
Class smartphone extends phone implement camera, musicPlayer {
{ }
void videocall() void sms()
{
{
}
}
}
void click() Interface Camera
{ {
} void click();
void record() void record();
{ }
} Interface MusicPlayer()
void play(){…} {
void pause(){…} void play();
Smartphone S=new Smartphone(); void pause();
void stop(){…}
Phone P=S; void stop();
}
Camera C=S; }
MusicPlayer M=S;
class SmartPhone extends Phone implements Camera, MusicPlayer{
public void record() {
System.out.println("video call");
}
public void click() {
System.out.println("video call");
}
class Phone { public void play() {
public void call() { System.out.println("play");
}
System.out.println("call");
public void stop() {
} System.out.println("stop");
public void msg() { }
System.out.println("Msg"); }
public class Main{
} public static void main(String[] args) {
} SmartPhone S=new SmartPhone();
interface Camera{ Phone P=S;
Camera C=S;
void click(); MusicPlayer M=S;
void record(); P.call();
} P.msg();
C.click();
interface MusicPlayer{ C.record();
void play(); M.play();
void stop(); M.stop();
}
} }
Interface
• Class phone is concrete class, and its method can be there. Interface
is camera and musicPlayer.
• Camera can be class also, in some situation can be interface also.
• Object is created for smartphone. Using object, I can call all methods
of smartphone.
• We can have a phone reference where object of smartphone can be copied.
So basically, I want to say that I can use the smartphone as phone also. That
means I can only call those methods that is of phone and not of smartphone.
• Similarly, I can have a camera reference which is copying the smartphone
object, and it can only call the methods of camera
• The same applies on the reference of the music Player.
• In the previous example while using the reference of phone, camera
and music player can be used by the object of smartphone but only
can be used as phone, camera and music player. It cannot access the
smartphone methods.
• Since the class smartphone is implementing two interface; one is
camera, and another is music player. And that is why we can create an
object of smartphone that can be referenced through phone, camera
and music player.
To Declare and use in Interface in Java
interface Car
{
public void start();
}
• Methods declared inside an interface are implicitly marked as public
and abstract, and variables declared inside an interface are implicitly
marked as public static final by the compiler.
• Why are member variables in an interface implicitly made public,
static, and final?
• To use an interface in Java, a class must implement it using
the implements keyword and override (provide its own
implementations) all the methods declared in the interface.
Example
interface Car {
public void start();
}
class Mechanic implements Car {
public void start() {
System.out.println("Mechanic class");
}
}
public class Main{
public static void main(String[] args) {
Mechanic m=new Mechanic();
m.start();
}
}
• What would happen if a class does not provide implementations of all
the methods declared in an interface it is implementing?
Why Interface
• Better polymorphism: With interfaces, you don’t have to make everything fit into one
family of classes, offering much more flexibility.
• Multiple inheritances: In Java, we cannot extend multiple classes because of the
famous Diamond problem (explained later in this article). To solve this, we can use a
Java interface to give child classes the freedom to extend whatever parent class they
wish (and implement our interface) rather than forcing them to extend a specific base
class to use our functionality.
• For abstraction: Interfaces help to achieve security, hide certain details, and only
show the relevant and necessary information. For example, when we start our car, we
just plug in the key and start the car without worrying about the nitty-gritty details of
what happens underneath when the car starts. This is a great example of abstraction!
We carry out our task, that is, starting the car, but we don’t need to take care of
anything else that happens in the background when starting the car.
• The method in the interface can not be private; since its to be
implemented by other classes. The methods are by default public.
Also, its by default abstract.
• If you want to give any variable in interface then it should be in
uppercase and these identifier are by default static and final, that
means it’s a constant.
• You can not give body to method of the class otherwise it will give
error. But if the method is static in the interface then it will work. So
the interface can have method with body that is static
Example
interface Test {
int X=10;
public void method1();
public static void method2()
{
System.out.println("In method 2");
}
}
public class Main{
public static void main(String[] args) {
System.out.println(Test.X);
Test.method2();
}
}
Output:
10
In method 2
Note: If you want to implement Test through some class. Creating the object of class will cause compilation error since
Guess the output
System.out.println(t2.X);
interface Test {
System.out.println(Test.X);
int X=10; t2.show();
void show();
} }
class Test2 implements Test }
{ Output:
public void show2() 10
10
{
In test show10
2 Multiple Inheritance Multiple Inheritance is not supported. Interface supports Multiple Inheritance.
5 Keyword Abstract class declared using abstract keyword. Interface is declared using interface keyword.
Abstract class can be inherited using extends Interface can only be implemented using
7 Inheritance
keyword. implements keyword.