Friday, 16 January 2015

Abstract classes in java


concrete method:-

It is a method with body.When all the objects want to perform same task.We should write a concrete method in the class.

Abstract method:-

A method with out body is called abstract method.When the same method should be implemented differently for different objects.Then we should write it as an abstract method.

Abstract class

A class that contains abstract methods is called abstract class.Both the abstract class and abstract methods should be declared using the keyword 'abstract'.

Can be applied to classes and methods.
purpose : For deferring implementation to sub-classes.
         Opposite of final, final can't be sub-classed, abstract must be sub-classed
         A class should be declared abstract,
       1.     if it has any abstract methods.
       2.     if it doesn't provide implementation to any of the abstract methods it inherited
3.     if it doesn't provide implementation to any of the methods in an interface that it  says implementing

Abstraction
Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Note:- We cannot create object for Abstract class

Note:

1.An abstract class is a class with 0 (or) more abstract methods.
2.An abstract class may have instance variables and concreate methods in addition to abstract methods.
3. We can not create an object to an abstract class or Abstract classes are never instantiated.
4. We can create a reference of abstract class.
5. All the abstract methods of abstract class should be implemented in its sub classes.
6. If any abstract method is not implemented, then the sub class should also be declared as 'abstract'.
7. Abstract class reference can be used to refer to all objects of its   sub classes.
8. A class can not be declared as both 'abstract' and 'final'.
ex:- final abstract class A  // invalid
9. Abstract classes can have Constructors, Member variables and Normal methods.
10. When you extend Abstract class with abstract method, you must define the abstract method in the child class, or make the child class abstract.

Ways to achieve Abstaction
There are two ways to achieve abstraction in java
1.    Abstract class (0 to 100%)
2.    Interface (100%)

Note : We create an object from the class that implements the target interface

1.    abstract class Bike
2.    {  
3.     abstract void run();  
4.    }  
5.    class Maruti extends Bike{  
6.    void run()
7.    {
8.     System.out.println("running safely..");
9.    }  
10.    public static void main(String args[]){  
11.    Bike b = new Maruti();  
12.    b.run();  
13.    }  
14.    }  
Output:running safely..

A superclass reference variable can access a sub-class object.here as the subclass method is implemented, it gets executed

factory method

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

1.    abstract class Shape{  
2.    abstract void draw();  
3.    }  
4.    class Circle extends Shape
5.    {  
6.     void draw()
7.      {
8.       System.out.println("drawing circle");
9.      }  
10.    }  
11.    class Rectangle extends Shape
12.    {  
13.     void draw()
14.     {
15.        System.out.println("drawing rectangle");
16.      }  
17.    }   
18.    class Test
19.    {  
20.    public static void main(String args[])
21.    {  
22.    Shape s=new Circle();  
23.    //In real scenario, Object is provided through factory method  
24.    s.draw();  
25.    }  
26.    }  
Output:drawing circle

abstract class Bike
{  
 abstract void run();  
 void changeGear()   //concrete method
{
System.out.println("gear changed");
}  
}  
class Honda extends Bike{  
void run()
{
System.out.println("running safely..");
 }  
public static void main(String args[]){  
Bike b = new Honda();  
b.run();  
b.changeGear();  
}  
}  
Output:running safely..
  gear changed






abstract class Bike  
{  
int limit=300;  
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 b = new Honda();  
b.run();  
b.getDetails();  
System.out.println(b.limit);  
}  
}  
Output:constructor is invoked
running safely..
it has two wheels
300

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

class Bike{  
abstract void run();  
}  
Output:compile time error


interface A{  
void m();  
void n();  
void o();  
void p();  
}  
abstract class B implements A{  
public void o()
{
System.out.println("I am O");}  
}  
class M extends B{  
public void m(){System.out.println("I am m");}  
public void n(){System.out.println("I am n");}  
public void p(){System.out.println("I am p");}  
}  
class Test{  
public static void main(String args[]){  
A a=new M();  
a.m();  
a.n();  
a.o();  
a.p();  
}
}  
Output:I am m
  I am n
  I am o
  I am p



When to use Abstract Methods & Abstract Class?

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.

Abstract classes are used to define generic types of behaviors at the top of an object-oriented programming class hierarchy, and use its
subclasses to provide implementation details of the abstract class.

No comments:

Post a Comment