Week 6 Abstraction
Week 6 Abstraction
Department of CSE
Course Title: Object Oriented Programming
Course Code: CSE 061 1203
Week - 06
Class – 01
1. What is Abstraction?
1|Page
2. Ways to achieve Abstraction
2|Page
Example of abstract class
abstract class A {}
In this example, Bike is an abstract class that contains only one abstract method run.
Its implementation is provided by the Honda class.
running safely
3|Page
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. class Rectangle extends Shape{
4. void draw(){System.out.println("drawing rectangle");}
5. }
6. class Circle1 extends Shape{
7. void draw(){System.out.println("drawing circle");} }
8. class TestAbstraction1{
9. public static void main(String args[]){
10.Shape s=new Circle1();//In a real scenario, object is provided through metho
d, e.g., getShape() method
11.s.draw();
12.} }
drawing circle
4|Page
Another example of Abstract class in java
File: TestBank.java
An abstract class can have a data member, abstract method, method body (non-
abstract method), constructor, and even main() method.
5|Page
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. class Honda extends Bike{
8. void run(){System.out.println("running safely..");}
9. }
10. class TestAbstraction2{
11. public static void main(String args[]){
12. Bike obj = new Honda();
13. obj.run();
14. obj.changeGear();
15. }
16.}
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(); }
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.
6|Page
Another real scenario of abstract class
1. interface A{
2. void a();
3. void b();
4. void c();
5. void d(); }
6. abstract class B implements A{
7. public void c(){System.out.println("I am c");} }
8. class M extends B{
9. public void a(){System.out.println("I am a");}
10.public void b(){System.out.println("I am b");}
11.public void d(){System.out.println("I am d");}
12.}
13.class Test5{
14.public static void main(String args[]){
15.A a=new M();
16.a.a();
17.a.b();
18.a.c();
19.a.d();
20.}}
Output:
I am a
I am b
I am c
I am d
7|Page
Class – 02
4. Advantages of Abstraction
8|Page
5. Disadvantages of Abstraction
Abstraction can make it more difficult to understand how the system works.
It can lead to increased complexity, especially if not used properly.
This may limit the flexibility of the implementation.
Abstraction can add unnecessary complexity to code if not used
appropriately, leading to increased development time and effort.
Abstraction can make it harder to debug and understand code, particularly
for those unfamiliar with the abstraction layers and implementation details.
Overuse of abstraction can result in decreased performance due to the
additional layers of code and indirection.
9|Page
6. What is Interface?
10 | P a g e
8. How to declare an interface?
Syntax:
1. interface <interface_name>{
2. // declare constant fields
3. // declare methods that abstract
4. // by default.
5. }
Internal addition by the compiler
In other words, Interface fields are public, static and final by default, and the methods
are public and abstract.
11 | P a g e
9. 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.
1. interface printable{
2. void print(); }
3. class A6 implements printable{
4. public void print(){System.out.println("Hello");}
5. public static void main(String args[]){
6. A6 obj = new A6();
7. obj.print();
8. } }
Output:
Hello
12 | P a g e
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
Output:
drawing circle
13 | P a g e
Java Interface Example: Bank
Let's see another example of java interface which provides the implementation of
Bank interface.
File: TestInterface2.java
1. interface Bank{
2. float rateOfInterest();
3. }
4. class SBI implements Bank{
5. public float rateOfInterest(){return 9.15f;}
6. }
7. class PNB implements Bank{
8. public float rateOfInterest(){return 9.7f;}
9. }
10.class TestInterface2{
11.public static void main(String[] args){
12.Bank b=new SBI();
13.System.out.println("ROI: "+b.rateOfInterest());
14.}}
Output:
ROI: 9.15
14 | P a g e
Class – 03
1. interface Printable{
2. void print(); }
3. interface Showable{
4. void show(); }
5. class A7 implements Printable,Showable{
6. public void print(){System.out.println("Hello");}
7. public void show(){System.out.println("Welcome");}
8. public static void main(String args[]){
9. A7 obj = new A7();
10.obj.print();
11.obj.show();
12. } }
Output:
Hello
Welcome
15 | P a g e
11. Multiple inheritance is not supported through class in java, but it is possible
by an interface, why?
1. interface Printable{
2. void print(); }
3. interface Showable{
4. void print(); }
5. class TestInterface3 implements Printable, Showable{
6. public void print(){System.out.println("Hello");}
7. public static void main(String args[]){
8. TestInterface3 obj = new TestInterface3();
9. obj.print();
10. } }
Output:
Hello
As you can see in the above example, Printable and Showable interface have same
methods but its implementation is provided by class TestTnterface1, so there is no
ambiguity.
16 | P a g e
12. What is Interface inheritance?
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.
11.public static void main(String args[]){
12.TestInterface4 obj = new TestInterface4();
13.obj.print();
14.obj.show();
15. }
16.}
Output:
Hello
Welcome
17 | P a g e
13. What is marker or tagged interface?
An interface can have another interface which is known as a nested interface. For
example:
1. interface printable{
2. void print();
3. interface MessagePrintable{
4. void msg();
5. }
6. }
18 | P a g e
14. What is the difference between abstract class and interface?
1) Abstract class can have abstract and non- Interface can have only abstract methods.
abstract methods.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and Interface has only static and final
non-static variables. variables.
4) Abstract class can provide the implementation of Interface can't provide the
interface. implementation of abstract class.
5) The abstract keyword is used to declare abstract The interface keyword is used to declare
class. interface.
6) An abstract class can extend another Java class and An interface can extend another Java
implement multiple Java interfaces. interface only.
7) An abstract class can be extended using keyword An interface can be implemented using
"extends". keyword "implements".
8) A Java abstract class can have class members like Members of a Java interface are public by
private, protected, etc. default.
9)Example: Example:
public abstract class Shape { public interface Drawable{
public abstract void draw ();} void draw();}
Let's see a simple example where we are using interface and abstract class both.
1. //Creating interface that has 4 methods
2. interface A{
3. void a();//by default, public and abstract
4. void b();
5. void c();
6. void d();
7. }
8. //Creating abstract class that provides the implementation of one method 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 implementa
tion 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();
20 | P a g e
23.a.b();
24.a.c();
25.a.d();
26.}}
Output:
I am a
I am b
I am c
I am d
21 | P a g e