Wednesday, August 16, 2017

Abstract Class in Java

A class that is declared using “abstract” keyword is known as abstract class.

Abstract is a process of hiding the data implementation details and showing only functionality to the user.Abstraction lets you focus on what the object does instead of how it does it.

Main things of abstract class

  • Abstraction class may or may not contain abstraction method.(There can be non abstract method) Point : An abstract method is method that declare that is declared without an implementation (without braces, and followed by a semicolon), like this:      Ex : abstract void moveTo(double deltaX, double deltaY);
  • But, If a class has at least one abstract method then class must be abstract
  • If a class is declared abstract, it cannot be instantiated (Abstract classes may not be instantiated) {You are not allowed to create object of Abstract class}
  • To use abstract class you have to inherit it from another class. Provide implementation to all the abstract method in it.
  • If you inherit abstract class, You have to provide implementation to all the abstract method in it.

Declare abstract class

Specifying abstract keyword before the class during declaration, make it abstract. Have look below code:
abstract class AbstractDemo{ }

Declare abstract method

Specifying abstract keyword before the method during declaration, make it abstract. Have look below code
abstract void moveTo();//no body

Why we need to abstraction class

In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic object. These object all have certain states(for ex -: position, orientation, line color, fill color) and behaviors(for ex -: moveTo, rotate, resize, draw) in common. Some of these states and behaviors are the same for all graphic objects (for ex : fill color,position, and moveTo). Others require different implementation(for ex: resize or drow). All GraphicObject must be able to draw or resize themselves; they just differ in how they do it. This is a perfect situation for abstract superclass. You can take advantage of the similarities and declare all the graphic object to inherit from the same abstract parent object(for ex : GraphicObject) as shown in the following figure.


First, you declare an abstract class, GraphicObject, to provide member variables and method that are wholly shared by all subclasses, such as the current position and the move to method.GraphicObject also declared abstract methods for abstract methods, such as drow or resize, that need to be a implemented by all subclasses but must  be implemented in different ways. The GraphicObject class can look something like this:

abstract class GraphicObject {

   void moveTo(int x, int y) {
       //Inside  this method we have to do change position of the graphic object
// according to x,y .This changes same to every graphic object.
//Then we can implement here.
   }
   abstract void draw(); //But every graphic object drawing case is unique.
// Not common. Then we have to create that case
// inside each class . Then create these method as abstract
    abstract void resize();
}


Usage of abstract method in sub classes

Each non abstract subclasses of GraphicObject, such as Circle and Rectangle, must provide implementations for the draw and resize method.

class Circle extends GraphicObject {
   void draw() {
       //Add to some implementation here
   }
   void resize() {
       //Add to some implementation here   
   }
}
class Rectangle extends GraphicObject {
   void draw() {
       //Add to some implementation here
   }
   void resize() {
       //Add to some implementation here
   }
}

Inside the main method you can call all methods like this

Public static void main(String args[]){
GraphicObject c = new Circle();
c.drow();
c.resize();
c.moveTo(4,5);
}

Ways to achieve abstraction in java

There are two ways to achieve abstraction in java
  • Abstract class(0 to 100%)
  • Interface(100%)

Abstraction class having construction, data member, method etc

abstract class GraphicObject {
  
   GraphicObject (){
       System.out.println("GraphicObject  is created");
   }
   void moveTo(int y, int x) {
          System.out.println("Change position according to "+ x+ " and " + y);
   }
   abstract void draw();
}

class Circle extends GraphicObject {
   void draw() {
       System.out.println("Draw the Circle");
   }
}
class TestAbstract
{  
   
   public static void main(String args[]){
      
       GraphicObject  grObj = new Circle ();
       grObj.draw();
       grObj.moveTo(4,6);
   }
}

Output:
GraphicObject  is created
Draw the Circle
Change position according to 6 and 4


Remember two rules:

  • If the class is having few abstract methods and few concrete methods: declare it as abstract class.
  • If the class is having only abstract methods: declare it as interface.


No comments:

Post a Comment