Sunday 19 April 2015

Abstract class in java

Abstract Class :-

Firstly i will explain you that what is an abstract class in java -

An abstract class a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
In other words,A class that is declared with abstract keyword, is known as abstract class in java. It can have abstract(method without body) and non-abstract methods (method with body).

Abstraction is a process of hiding the implementation details and showing only functionality to the user.We can achieve abstraction in two ways in java 
1. Using abstract class(0-100%)
2. Using interface(100%)

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.

Declaration :-



abstract class class_name { }

Abstract Method :-


An abstract class can include methods that contain no implementation. These are called abstract methods. The abstract method declaration must then end with a semicolon rather than a block.Abstract method can never be final and static.


Declaration :-


abstract return_type function_name ();


Lets take some examples -


// A Simple demonstration of abstract. 
abstract class A { 
abstract void callme(); 
// concrete methods are still allowed in abstract classes 
void callmetoo() { 
System.out.println("This is a concrete method."); 

}

class B extends A {  
void callme() {
  System.out.println("B's implementation of callme.");
  } 
 }

class AbstractDemo { 
 public static void main(String args[]) { 
 B b = new B(); 
 b.callme();
  b.callmetoo();  

} 

Output will be :

B's implementation of callme.
This is a concrete method.

Abstract classes cannot be used to instantiate objects, they can be used to create object references, because Java's approach to run-time polymorphism is implemented through the use of superclass references. Thus, it must be possible to create a reference to an abstract class so that it can be used to point to a subclass object. You will see this feature in the next example. 


A Different One :


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();  
}  
}  

  1. Output will be : 
    
       running safely..
      

Important Thing About Abstract Class -:

We can not create the object of an abstract class but we can create the reference variable of an abstract class then the reference variable can be used to refer to an object of any class that is derived from abstract class .We understand this with an example 


// Using abstract methods and classes. 
abstract class Figure { 
double dim1; 
double dim2; 
Figure(double a, double b) { 
dim1 = a; 
dim2 = b; 

// area is now an abstract method 
abstract double area(); 
}
class Rectangle extends Figure { 
Rectangle(double a, double b) { 
super(a, b); 

// override area for rectangle 
double area() { 
System.out.println("Inside Area for Rectangle."); 
return dim1 * dim2; 

}
class Triangle extends Figure { 
Triangle(double a, double b) { 
super(a, b); 

// override area for right triangle 
double area() { 
System.out.println("Inside Area for Triangle."); 
return dim1 * dim2 / 2; 

}
class AbstractAreas { 
public static void main(String args[]) { 
// Figure f = new Figure(10, 10); // illegal now 
Rectangle r = new Rectangle(9, 5); 
Triangle t = new Triangle(10, 8); 
Figure figref; // this is OK, no object is created 
figref = r; 
System.out.println("Area is " + figref.area()); 
figref = t; 
System.out.println("Area is " + figref.area()); 

}

Here we see that we can not create the object of  type Figure but we can create a reference variable of type Figure.Here we created a reference variable of type Figure named figref and figref is used to refer to the objects of  Class Rectangle and Triangle .

 Points of abstract class :
  1. Abstract class contains abstract methods.
  2. Program can't instantiate an abstract class.
  3. Abstract classes contain mixture of non-abstract and abstract methods.
  4. If any class contains abstract methods then it must implements all the abstract methods of the abstract class.

 When we use Abstract class and Methods :-


Abstract methods are usually declared where two or more subclasses are expected to do a similar thing in different ways through different implementations. These subclasses extend the same Abstract class and provide different implementations for the abstract methods.

No comments:

Post a Comment