Abstraction in Java - Passionate Geekz

Breaking

Where you can unleash your inner geek.

Saturday, 25 January 2020

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 
  2. Interface 

1).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

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

Example 1 

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

Example 2

abstract class Shape
{
abstract void draw();
}
class Rectangle extends Shape
{
void draw()
{
System.out.println(“drawing rectangle”);
}
}
class Circle extends Shape
{
void draw()
{
System.out.println(“drawing circle”);
}
}
class abstract2nd
{
public static void main(String args[])
{
Circle c=new Circle();
c.draw();
Rectangle r=new Rectangle();
r.draw();
}
}

Example 3

abstract class Bank
{
abstract int getRateOfInterest();
}
class SBI extends Bank
{
int getRateOfInterest()
{
return 7;
}
}
class PNB extends Bank
{
int getRateOfInterest()
{
return 8;
}
}
class Testank
{
public static void main(String[] args)
{
SBI bs=new SBI();
System.out.println(“Rate of interest is:” +bs.getRateOfInterest()+”%”);
PNB bp=new PNB();
System.out.println(“Rate of interest is:” +bp.getRateOfInterest()+”%”);
}
}

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.

Example 4

abstract class Bike1
{
Bike1()
{
System.out.println(“bike is created”);
}
abstract void run();
void changeGear()
{
System.out.println(“gear changed”);
}
}
class Honda extends Bike1
{
void run()
{
System.out.println(“running safely”);
}
}
public class abstractconstructor
{
public static void main(String[] args)
{
Honda obj=new Honda();
obj.run();
obj.changeGear();
}
}

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

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

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.

2).Interface

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.

Example

interface A2
{
void a();
void b();
void c();
void d();
}

abstract class B2 implements A2
{
public void c()
{
System.out.println(“I am c”);
}
}
class M2 extends B2
{
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”);
}
}

public class abstractinterface
{
public static void main(String[] args)
{
M2 mm=new M2();
mm.a();
mm.b();
mm.c();
mm.d();
}
}

No comments:

Post a Comment