0% found this document useful (0 votes)
37 views

Java 6 Absraction, Interface

Uploaded by

amaleshmsc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Java 6 Absraction, Interface

Uploaded by

amaleshmsc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Abstract class in Java

A class which is declared with the abstract keyword is known as


an abstract class in Java. It can have abstract and non-abstract
methods (method with the body).

Abstraction in Java
Abstraction is a process of hiding the implementation details
and showing only functionality to the user.

Another way, it shows only essential things to the user and hides
the internal details, for example, sending SMS where you type the
text and send the message. You don't know the internal
processing about the message delivery.

Abstraction lets you focus on what the object does instead of how
it does it.

Ways to achieve Abstraction

There are two ways to achieve abstraction in java

1. Abstract class (0 to 100%)


2. Interface (100%)

Abstract class in Java


A class which is declared as abstract is known as an abstract
class. It can have abstract and non-abstract methods. It needs to
be extended and its method implemented. It cannot be
instantiated.

Points to Remember
o An abstract class must be declared with an abstract keyword.
o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not to
change the body of the method.

Example of abstract class

1. abstract class A{}

Abstract Method in Java


A method which is declared as abstract and does not have
implementation is known as an abstract method.

Example of abstract method

1. abstract void printStatus();//no method body and abstract

Example of Abstract class that has an abstract


method
In this example, Bike is an abstract class that contains only one
abstract method run. Its implementation is provided by the Honda
class.

abstract class Bike{


abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
running safely

Understanding the real scenario of Abstract class


In this example, Shape is the abstract class, and its
implementation is provided by the Rectangle and Circle classes.

Mostly, we don't know about the implementation class (which is


hidden to the end user), and an object of the implementation
class is provided by the factory method.

A factory method is a method that returns the instance of the


class. We will learn about the factory method later.

In this example, if you create the instance of Rectangle class,


draw() method of Rectangle class will be invoked.

File: TestAbstraction1.java

1. abstract class Shape{


2. abstract void draw();
3. }
4. //In real scenario, implementation is provided by others i.e. unknown by e
nd user
5. class Rectangle extends Shape{
6. void draw(){System.out.println("drawing rectangle");}
7. }
8. class Circle1 extends Shape{
9. void draw(){System.out.println("drawing circle");}
10. }
11. //In real scenario, method is called by programmer or user
12. class TestAbstraction1{
13. public static void main(String args[]){
14. Shape s=new Circle1();//In a real scenario, object is provided through
method, e.g., getShape() method
15. s.draw();
16. }
17. }

drawing circle

Another example of Abstract class in java


File: TestBank.java

1. abstract class Bank{


2. abstract int getRateOfInterest();
3. }
4. class SBI extends Bank{
5. int getRateOfInterest(){return 7;}
6. }
7. class PNB extends Bank{
8. int getRateOfInterest(){return 8;}
9. }

10. class TestBank{


11. public static void main(String args[]){
12. Bank b;
13. b=new SBI();
14. System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");

15. b=new PNB();


16. System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");

17. }}
Rate of Interest is: 7 %
Rate of Interest is: 8 %

Abstract class having constructor, data member and


methods
An abstract class can have a data member, abstract method,
method body (non-abstract method), constructor, and even
main() method.
File: TestAbstraction2.java

1. //Example of an abstract class that has abstract and non-abstract methods


2. abstract class Bike{
3. Bike(){System.out.println("bike is created");}
4. abstract void run();
5. void changeGear(){System.out.println("gear changed");}
6. }
7. //Creating a Child class which inherits Abstract class
8. class Honda extends Bike{
9. void run(){System.out.println("running safely..");}
10. }
11. //Creating a Test class which calls abstract and non-abstract methods
12. class TestAbstraction2{
13. public static void main(String args[]){
14. Bike obj = new Honda();
15. obj.run();
16. obj.changeGear();
17. }
18. }
bike is created
running safely..
gear changed

Rule: If there is an abstract method in a class, that class must be abstract.


1. class Bike12{
2. abstract void run();
3. }
compile time error

Rule: If you are extending an abstract class that has an abstract method, you must
either provide the implementation of the method or make this class abstract.

Another real scenario of abstract class


The abstract class can also be used to provide some
implementation of the interface. In such case, the end user may
not be forced to override all the methods of the interface.

Note: If you are beginner to java, learn interface first and skip this example.
1. interface A{
2. void a();
3. void b();
4. void c();
5. void d();
6. }
7.
8. abstract class B implements A{
9. public void c(){System.out.println("I am c");}
10. }
11.
12. class M extends B{
13. public void a(){System.out.println("I am a");}
14. public void b(){System.out.println("I am b");}
15. public void d(){System.out.println("I am d");}
16. }
17.
18. class Test5{
19. public static void main(String args[]){
20. A a=new M();
21. a.a();
22. a.b();
23. a.c();
24. a.d();
25. }}
26.
Output:I am a
I am b
I am c
I am d

Interface in Java
An interface in Java is a blueprint of a class. It has static constants and
abstract methods.

The interface in Java is a mechanism to achieve abstraction.


There can be only abstract methods in the Java interface, not
method body. It is used to achieve abstraction and
multiple inheritance in Java.

In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body.

Java Interface also represents the IS-A relationship.

It cannot be instantiated just like the abstract class.

Why use Java interface?


There are mainly three reasons to use interface. They are given
below.

o It is used to achieve abstraction.


o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.
The relationship between classes and interfaces
As shown in the figure given below, a class extends another class,
an interface extends another interface, but a class implements
an interface.

How to declare an interface?


An interface is declared by using the interface keyword. It
provides total abstraction; means all the methods in an interface
are declared with the empty body, and all the fields are public,
static and final by default. A class that implements an interface
must implement all the methods declared in the interface.

Syntax:
1. interface <interface_name>{
2.
3. // declare constant fields
4. // declare methods that abstract
5. // by default.
6. }
7. interface printable{
8. void print();
9. }
10. class A6 implements printable{
11. public void print(){System.out.println("Hello");}
12.
13. public static void main(String args[]){
14. A6 obj = new A6();
15. obj.print();
16. }
17. }
Out put: Hello

Java Interface Example: Drawable


In this example, the Drawable interface has only one method. Its
implementation is provided by Rectangle and Circle classes. In a
real scenario, an interface is defined by someone else, but its
implementation is provided by different implementation
providers. Moreover, it is used by someone else. The
implementation part is hidden by the user who uses the interface.
File: TestInterface1.java

1. //Interface declaration: by first user


interface Drawable{
void draw();
}
//Implementation: by second user
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class Circle implements Drawable{
public void draw(){System.out.println("drawing circle");}
}
//Using interface: by third user
class TestInterface1{
public static void main(String args[]){
Drawable d=new Circle();//In real scenario, object is provided by method e.g. g
etDrawable()
d.draw();
}}

Output:
drawing circle

Multiple inheritance in Java by interface


If a class implements multiple interfaces, or an interface extends
multiple interfaces, it is known as multiple inheritance.

interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
A7 obj = new A7();
obj.print();
obj.show();
}
}

Output:Hello
Welcome

Java Interface Example


In this example, the Printable interface has only one method, and
its implementation is provided in the A6 class.

1. interface printable{
2. void print();
3. }
4. class A6 implements printable{
5. public void print(){System.out.println("Hello");}
6.
7. public static void main(String args[]){
8. A6 obj = new A6();
9. obj.print();
10. }
11. }
Output:
Hello

Q) Multiple inheritance is not supported through class in java,


but it is possible by an interface, why?
As we have explained in the inheritance chapter, multiple
inheritance is not supported in the case of class because of
ambiguity. However, it is supported in case of an interface
because there is no ambiguity. It is because its implementation is
provided by the implementation class. For example:

1. interface Printable{
2. void print();
3. }
4. interface Showable{
5. void print();
6. }
7.
8. class TestInterface3 implements Printable, Showable{
9. public void print(){System.out.println("Hello");}
10. public static void main(String args[]){
11. TestInterface3 obj = new TestInterface3();
12. obj.print();
13. }
14. }

Output:
Hello

Interface inheritance
A class implements an interface, but one interface extends
another interface.

1. interface Printable{
2. void print();
3. }
4. interface Showable extends Printable{
5. void show();
6. }
7. class TestInterface4 implements Showable{
8. public void print(){System.out.println("Hello");}
9. public void show(){System.out.println("Welcome");}

10. public static void main(String args[]){


11. TestInterface4 obj = new TestInterface4();
12. obj.print();
13. obj.show();
14. }
15. }

Output:
Hello

Welcome

Difference between abstract class and


interface
Abstract class and interface both are used to achieve abstraction
where we can declare the abstract methods. Abstract class and
interface both can't be instantiated.

But there are many differences between abstract class and


interface that are given below.

Simply, abstract class achieves partial abstraction (0 to 100%)


whereas interface achieves fully abstraction (100%).
Abstract class Interface

1) Abstract class can have abstract Interface can have only


and non-abstract methods. abstract methods. Since Java 8, it can
have default and static methods also.

2) Abstract class doesn't support Interface supports multiple


multiple inheritance. inheritance.

3) Abstract class can have final, Interface has only static and final
non-final, static and non-static variables.
variables.

4) Abstract class can provide the Interface can't provide the


implementation of interface. implementation of abstract class.

5) The abstract keyword is used to The interface keyword is used to


declare abstract class. declare interface.

6) An abstract class can extend An interface can extend another Java


another Java class and implement interface only.
multiple Java interfaces.

7) An abstract class can be An interface can be implemented using


extended using keyword "extends". keyword "implements".

8) A Java abstract class can have Members of a Java interface are public by
class members like private, protected, default.
etc.

9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }

Example of abstract class and interface in


Java
1. //Creating interface that has 4 methods
2. interface A{
3. void a();//bydefault, public and abstract
4. void b();
5. void c();
6. void d();
7. }

8. //Creating abstract class that provides the implementation of one meth


od of A interface
9. abstract class B implements A{
10. public void c(){System.out.println("I am C");}
11. }

12. //Creating subclass of abstract class, now we need to provide the


implementation of rest of the methods
13. class M extends B{
14. public void a(){System.out.println("I am a");}
15. public void b(){System.out.println("I am b");}
16. public void d(){System.out.println("I am d");}
17. }

18. //Creating a test class that calls the methods of A interface


19. class Test5{
20. public static void main(String args[]){
21. A a=new M();
22. a.a();
23. a.b();
24. a.c();
25. a.d();
26. }}
Test it Now

Output:
I am a
I am b
I am c
I am d

You might also like