Abstract Class in java

Abstraction is the way of hiding implementation details and showing only required one from others. Like while we are sending messages from mobile phone, then we are not worrying about how messages go to transfer one end to another. Only we are focusing to send message.

Like while you drive your car we don’t how internally it works and we are not considering that part. Same way abstraction lets the programmer to hide internal implementation from the rest of the world.

How to Achieve Abstraction

Abstraction can be achieved by either of two ways.

  1. Abstract Class (0-100%)
  2. Interface (100%)

Abstract Class

By Abstract class we can achieve 0-100% abstraction. An Abstract is used to provide common behaviors to their child classes. An Abstract class has following properties.

  1. The abstract keyword is used to created abstract class
  2. Abstract class can have abstract or non-abstract methods.
  3. No instance can be created for abstract class.
  4. Non abstract method must not have body means only declaration not definition.

Abstract Method

It is a kind of method which implementation does not provided by base class. This is responsibility of its child class to implement all the abstract methods while extending abstract class.

Properties of Abstract Method

  1. The abstract method cannot be used in normal class. It is only used with abstract class.
  2. Child class needs to implement all abstract methods if not compiler raise exception for it.

Example of Abstract Class

abstract class A{
 abstract void method1();
} 
class B extends A{
  public void method1(){
    System.out.println("Method 1");
  }
  public static void main(String args[]){
    B b = new B();
	b.method1();
  }
}

Output

Method1

Why we need Abstract Class?

Through an abstract class we can create a template or specification for its child classes. Abstract class will provide the basics and common methods and each child class will provide their own implementations.

For below scenario, we have car as an abstract class which has following abstract methods.

  1.  start()
  2.  stop();

Abstract class

These two methods will be implemented by Audi,BMW and Nano child classes as per their own. As per available specification later one or more implementation may also be provided.

Loose Coupling

Loosely Couple is an approach where each software components are connected each other in an independent manner. We can achieve loose coupling  through abstract class. For achieving loose coupling we need to follow factory design pattern. Through factory design pattern we are not letting to create object of implemented classes object by invoking their constructor rather we have another class for creating its object. So that later we can easily provide different implementations later in future without maintenance problem. In the above example the code is tightly coupled as we are creating object directly by invoking their constructor.

In software development, we are creating services which can be consumed by various clients. If we let our clients the implemented classes, then whenever we are changing the structure of service, then each of client need to change their code to consume our services. That’s the tight coupling of code which can be sorted by factory design pattern.

Real Example of Loosing Couple based Abstraction

In this example we have Number abstract class and IntegerNumber and ComplexNumber classes are implemented classes.

Number

This is an abstract class. It has two abstract methods.

  1. add() :- To add two numbers.
  2. display() :- To display number

package com.startwithjava.oops;

public abstract class Number {
public abstract Number add(Number n);
public abstract void display();
}

ComplexNumber

This class extended Number class and provided its own implementation of over methods. Each complex number has real and imaginary part.


package com.startwithjava.oops;

public class ComplexNumber extends Number {
private int real;
private int img;
public ComplexNumber(){
super();
}
public ComplexNumber(int real,int img){
this.real=real;
this.img=img;
}
@Override
public Number add(Number n) {
ComplexNumber c1 = (ComplexNumber)n;
ComplexNumber complex = new ComplexNumber();
complex.real = c1.real+this.real;
complex.img = c1.img+this.img;
return complex;
}

@Override
public void display() {
System.out.println(this.real+"+"+this.img+"i");
}

}

IntegerNumber

It also extended Number class and provided its own implementation.


package com.startwithjava.oops;

public class IntegerNumber extends Number{
private int num;
public IntegerNumber(){
super();
}
public IntegerNumber(int num){
this.num = num;
}
@Override
public Number add(Number n) {
IntegerNumber num1 = (IntegerNumber)n;
IntegerNumber num = new IntegerNumber(num1.num+this.num);
return num;
}

@Override
public void display() {
System.out.println(num);
}
}

NumberFactory


package com.startwithjava.oops;

import java.util.Scanner;

public class NumberFactory {
//Factory method for creating object
public static Number getNumber(int choice){
Number number = null;
Scanner sc =null;
switch(choice){
case 1:
sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
number = new IntegerNumber(n);
break;
case 2:
sc = new Scanner(System.in);
System.out.println("Enter a real part");
int r = sc.nextInt();
System.out.println("Enter a img part");
int img = sc.nextInt();
number = new ComplexNumber(r,img);
break;
}
return number;
}
}

LooseCouplingEx


package com.startwithjava.oops;

public class LooseCouplingEx {

public static void main(String[] args) {
//Creating IntegerNumber Object
Number integer1 = NumberFactory.getNumber(1);
Number integer2 = NumberFactory.getNumber(1);

//Adding integers
Number integer = integer1.add(integer2);
integer.display();

//Creating ComplexNumber Object
Number complex1 = NumberFactory.getNumber(2);
Number complex2 = NumberFactory.getNumber(2);

//Adding Complex number
Number complex = complex1.add(complex2);
complex.display();
}

}

Output


Enter a number
12
Enter a number
10
22
Enter a real part

1
Enter a img part
2
Enter a real part
4
Enter a img part
5
5+7i

Leave a Reply