Thursday, October 1, 2015

Abstract class in Java-2

Abstract class in Java-2


Understanding the real scenario of abstract class

In this example, Shape is the abstract class, its implementation is provided by the Rectangle and Circle classes. Mostly, we don't know about the implementation class (i.e. hidden to the end user) and object
of the implementation class is provided by the factory method.

A factory method is the method that returns the instance of the class. We will learn about the factory
method later.

In this example, if you create the instance of Rectangle class, draw method of Rectangle class will be invoked.

     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 Test{ 
    public static void main(String args[]){ 
    Shape s=new Circle(); 
    //In real scenario, Object is provided through factory method 
    s.draw(); 
    } 
    } 

Output:drawing circle

Abstract class having constructor, data member, methods etc.

Note: An abstract class can have data member, abstract method, method body, constructor and
even main() method.


    //example of abstract class that have method body 
     abstract class Bike{ 
       abstract void run(); 
       void changeGear(){System.out.println("gear changed");} 
     } 
     
     class Honda extends Bike{ 
     void run(){System.out.println("running safely..");} 
     
     public static void main(String args[]){ 
      Bike obj = new Honda(); 
      obj.run(); 
      obj.changeGear(); 
     } 
    } 

Output:running safely..
       gear changed

    //example of abstract class having constructor, field and method 
    abstract class Bike 
    { 
     int limit=30; 
     Bike(){System.out.println("constructor is invoked");} 
     void getDetails(){System.out.println("it has two wheels");} 
     abstract void run(); 
    } 
     
    class Honda extends Bike{ 
     void run(){System.out.println("running safely..");} 
     
     public static void main(String args[]){ 
      Bike obj = new Honda(); 
      obj.run(); 
      obj.getDetails(); 
      System.out.println(obj.limit); 
     } 
    } 

Output:constructor is invoked
running safely..
it has two wheels
30


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

    class Bike{ 
    abstract void run(); 
    } 

Output:compile time error


Rule: If you are extending any abstract class that have abstract method, you must either provide the
implementation of the method or make this class abstract.

Another real scenario of abstract class

The abstract class can also be used to provide some implementation of the interface. In such case, the end user may not be forced to override all the methods of the interface.

Note: If you are beginner to java, learn interface first and skip this example.
    interface A{ 
    void a(); 
    void b(); 
    void c(); 
    void d(); 
    } 
     
    abstract class B implements A{ 
    public void c(){System.out.println("I am C");} 
    } 
     
    class M extends B{ 
    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");} 
    } 
     
    class Test{ 
    public static void main(String args[]){ 
    A a=new M(); 
    a.a(); 
    a.b(); 
    a.c(); 
    a.d(); 
    }} 

Output:I am a
       I am b
       I am c
       I am d

No comments:

Post a Comment