Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

CS 8392 

OBJECT ORIENTED
PROGRAMMING

  VA S A N T H A K U M A R V, A P / C S E
ABSTRACT 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%)
ABSTRACT
CLASS
RULES 
ABSTRACT 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;
}    
}   
ABSTRACT CLASS WITH
CONSTRUCTOR 
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;
}   }   
ABSTRACT CLASS WITH
NON-ABSTRACT 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.

I N T E R FA C E • 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.

I N T E R FA C E
I N T E R FA C E   - E X A M P L E    

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 AVA B Y I N T E R F A C E    
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 FA 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 FA U LT M E T H O D I N I N T E R FA 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 TAT I C M E T H O D I N I N T E R FA 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
A B S T R A C T C L A S S A N D I N T E R FA C E    
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 variables. Interface has only static and final 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 multiple An interface can extend another Java interface only.
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, protected, etc. Members of a Java interface are public by default.

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

You might also like