Friday, November 13, 2015

Abstraction in java with Example


Abstraction in java:

Abstraction is a process of hiding the implementation details and showing only functionality to the user.It shows only important things to the user and hides the internal details.


For Example, Consider the ATM Machine it shows some options like withdraw money,check the balance etc..but we don't know the internal implementation. So it hides the internal details. You don't know the internal process how the ATM working


ways to achieve Abstraction in java:

There are two ways to achieve abstraction in java
a)abstract class
b) interface

Abstract class in java:

A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.

Example abstract method:

abstract void display();// no body and abstract

Example of abstract class that has abstract method:
           
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();  
}  
}  

Output:
running safely..



Another example of abstract class in java:

abstract class Bank{    
abstract int getRateOfInterest();    
}    
class SBI extends Bank{    
int getRateOfInterest(){
return 7;}    
}    
class PNB extends Bank{    
int getRateOfInterest(){
return 7;
}    
}    
class TestBank{    
public static void main(String args[]){    
Bank b=new SBI();//if object is PNB, method of PNB will be invoked    
int interest=b.getRateOfInterest();    
System.out.println("Rate of Interest is: "+interest+" %");    
}
}    

OUTPUT:

Rate of Interest is: 7 %

Abstraction using Interface in java:

In Java Interface is an another way of providing abstraction

interface A{  
void a();  
void b();  
void c();  
void d();  
}  
abstract class B implements A{  
public void c(){
System.out.println("I am C");}  
}  
class M extends B{  
public void a(){
System.out.println("I am a");}  
public void b(){
System.out.println("I am b");}  
public void d(){
System.out.println("I am d");
}  
}  
class Test{  
public static void main(String args[]){  
A a=new M();  
a.a();  
a.b();  
a.c();  
a.d();  
}
}  

Output:

I am a
I am b
I am c
I am d

Abstraction : Things to Remember
  1.  In Java you can not create instance of abstract class using new operator, its compiler error. Though abstract class can have constructor.
  2. abstract is a keyword in Java, which can be used with both class and method.  Abstract class can contain both abstract and concrete method. Abstract method doesn't have body, just declaration.
  3. A class automatically becomes abstract class when any of its method declared as abstract
  4. abstract method doesn't have method body.
  5. If a class extends an abstract class or interface it has to provide implementation to all its abstract method to be a concrete class. alternatively this class can also be abstract.


No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...