Tuesday, 5 April 2011

what is abstraction

Abstraction

Defination:

The process of providing necessary information (method proto-type ) by removing / hiding unnecessary information (method body) is called Abstraction.

Hiding implementation

Removing implementation

In first case (hiding implementation) abstraction provides reusability to the subclass developer.

As a superclass developer we define a method with logic and inform to user (subclass developer) only prototype, for using this method implementation is unnecessary for subclass developer.

EX

System.out.println();

Here we don’t bother about println() method implementation

All classes by default contain concrete methods hence they are implementing abstraction at first level.

in second case (removing implementation) abstraction provides forcibility to the subclass developer.

we should explicitly implement abstraction at second level

it means in super class we define a method without body, the method should be overridden and implemented in subclass based on subclass business requirement.

below steps shows us, implementing abstraction at second level for sub classes to implement sum logic.

1. Create abstract class by using abstract keyword.

Syntax

abstract class

Ex

public abstract MyAbstract{

2. Declare abstract method in that class , remove body of the method({}) And declare it as abstract method, by using abstract keyword(modifier)

Syntax

abstract(parameterlist);

EX

abstract void m1();

3. Define subclass from this abstract class and override abstract method in the subclass with that class business requirement

Rules in creating abstract class

1 it should contain abstract modifier in its prototype and should not have body else it leads to compile time error.

2 if a class contain abstract method it should be declared as abstract else it leads to compile time error.

Rules in implementing abstraction

Rule 1

abstract method must contain abstract keyword else it leads to compile time error missing method body or declare abstract.

Ex

void add();// CE

Abstract void add();

Rule 2

if a class contain abstract method it should be declared as abstract class using abstract keyword else it leads to compile time error.

Ex

Class Example{

Abstract void add ();//CE

}

Abstract class Example {

Abstraction void m1();

}

Rule 3

If a class is declared as abstract it can’t be instantiated directly means object can’t be create by using its constructor violation leads to CE

Ex

abstract class Example {

abstraction void m1();

p s v m (String[] args){

Example e= new Example();//CE: Example is abstract can’t be instantiated

}

Rule 4

Sub class must override all abstract methods of its super calss else it should be declared as abstract violation leads to compile time error

EX

class Sample extends Example{}//CE sample is not a abstract and doesnot override abstract method m1 in Example.

To solve the above CE we can give below 2 solutions

1) Declare sample class as abstract

2) Declare body for m1() in sample which is abstract method in Example

No comments:

Post a Comment