You are on page 1of 15

CS 8392

OBJECT ORIENTED
PROGRAMMING

VASANTHA KUMAR V, A P/ CS E
ABSTR AC T CLASS
Abstraction is a process of hiding the implementation details and showing only functionality to
the user.
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).
It needs to be extended and its method implemented. It cannot be instantiated

Ways to achieve Abstraction


There are two ways to achieve abstraction in java
• Abstract class (0 to 100%)
• Interface (100%)
ABSTR ACT
CLASS
RULES
ABSTR ACT CLASS - EXAMPLE

abstract class Bank class TestBank


{ {
abstract int getRateOfInterest(); public static void main(String args[])
} {
class SBI extends Bank Bank b = new SBI();
{
int getRateOfInterest() System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
{ Bank b=new PNB();
return 7;
} System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
} }
class PNB extends Bank }
{
int getRateOfInterest()
{
return 8;
}
}
ABSTR ACT CLASS WITH
CO N STR U CTO R
abstract class Bank class TestBank
{ {
abstract int getRateOfInterest(); public static void main(String args[])
Banking() {
{ Bank b = new SBI();
System.out.println("Banking");
} } System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
class SBI extends Bank Bank b=new PNB();
{
int getRateOfInterest() System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
{ }
return 7; }
} }
class PNB extends Bank
{
int getRateOfInterest()
{
return 8;
} }
ABSTR ACT CLASS WITH NON-
ABSTR ACT METHODS
abstract class Bank class TestBank
{ {
abstract int getRateOfInterest(); public static void main(String args[])
public void display() {
{ Bank b = new SBI();
System.out.println("Method"); b.display();
} }
class SBI extends Bank System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
{ Bank b=new PNB();
int getRateOfInterest()
{ System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
return 7;
} } }
class PNB extends Bank }
{
int getRateOfInterest()
{
return 8;
• 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.

INTERFACE • It is used to achieve abstraction and


multiple inheritance in Java.
Syntax:
interface <interface_name>
{
}
The Java compiler adds public and abstract keywords before the interface method. Moreover, it
adds public, static and final keywords before data members.

INTERFACE
INTERFACE - EXAMPLE

interface Bank
{ class TestInterface2
float rateOfInterest(); {
} public static void main(String[] args)
class SBI implements Bank {
{ Bank b=new SBI();
public float rateOfInterest() System.out.println("ROI: "+b.rateOfInterest());
{ }
return 9.15f;} }
}
class PNB implements Bank
{
public float rateOfInterest()
{
return 9.7f;}
}
M U LT I P L E I N H E R I TA N C E I N J AV A B Y
INTERFACE
interface Printable class A7 implements Printable,Showable
{ {
void print(); public void print(){System.out.println("Hello");
} }
interface Showable public void show(){System.out.println("Welcome");
{ }
void show(); public static void main(String args[])
} {
A7 obj = new A7();
obj.print();
obj.show();
}
}
I N T E R F A C E I N H E R I TA N C E

interface Printable class TestInterface4 implements Showable


{ {
void print(); public void print(){System.out.println("Hello");
} }
interface Showable extends Printable{ public void show(){System.out.println("Welcome");
void show(); }
}
public static void main(String args[])
{
TestInterface4 obj = new TestInterface4();
obj.print();
obj.show();
}
}
D E F A U LT M E T H O D I N I N T E R F A C E

interface Drawable class TestInterfaceDefault


{ {
void draw(); public static void main(String args[])
default void msg() {
{ Drawable d=new Rectangle();
System.out.println("default method"); d.draw();
} d.msg();
} }
class Rectangle implements Drawable }
{
public void draw()
{
System.out.println("drawing rectangle");
}
}
S T AT I C M E T H O D I N I N T E R F A C E

interface Drawable class TestInterfaceStatic


{ {
void draw(); public static void main(String args[])
static int cube(int x) {
{ Drawable d=new Rectangle();
return x*x*x; d.draw();
} System.out.println(Drawable.cube(3));
} }
class Rectangle implements Drawable }
{
public void draw()
{
System.out.println("drawing rectangle");
}
}
DIFFERENCE BETWEEN
ABSTR ACT CLASS AND INTERFACE
Abstract class Interface
1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can
have default and static methods also.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static Interface has only static and final variables.
variables.
4) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
6) An abstract class can extend another Java class and implement An interface can extend another Java interface only.
multiple Java interfaces.
7) An abstract class can be extended using keyword "extends". An interface can be implemented using keyword "implements".
8) A Java abstract class can have class members like private, Members of a Java interface are public by default.
protected, etc.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw();} void draw();
THANK YOU

You might also like