Search This Blog

17 June, 2011

Abstract method and Abstract class part-2

Abstract method and Abstract class Part-1


Abstract method:- A method which does not have a body part needs to be declared as an abstract method.
A method can't be declared as an abstract method if it has a body part.




Abstract class:- A class which has a abstract method needs to be declared as abstract. A class can also be declared as abstract even if it doen't have a abstract method.


If a class is declared as an abstract class then it cannot be instantiated. A pointer can be created from an abstract class.


If a class is extending a class which is abstract then within the derived class you need to override all the abstract methods of the base class or the derived class needs to be declared as an abstract class.


consider the following examples:-
1)
class aaa
{
public void sam();                                               
public void tom()        
{
System.out.println("Hello");
}
}
//incorrect  
Note:- Incorrect because sam method has not been declared as abstract.
--------------------------------------------------------------------------
2)
class aaa     
{
abstract public void sam();
public void tom()
{
System.out.println("Hello");
}
}
//Incorrect
Note:- incorrect because aaa class has not been declared as abstract.
----------------------------------------------------------------------------
3)
abstract class aaa
{
abstract public void sam();
public void tom()
{
System.out.println("Hi");
}
}
class mmm
{
pubic static void main()
{
aaa a;
a=new aaa();
}
}
//incorrect, will not get compiled. 
Note:- Incorrect, because an object of the class aaa is being created whereas aaa is an abstract class and cannot be instantiated. 
----------------------------------------------------------------------------------
4)
abstract class aaa
{
abstract public void sam();
public void tom()
{
System.out.println();
}
}
class kkk extends aaa
{
public void boy()
{
System.out.println("bye");
}
}
//Incorrect
Note:- Incorrec, because kkk has not been declared as abstract. 
           Remember that if a class is extending an abstract class then within the derived class you need to override all the abstract methods or declare the derived class as abstract.
----------------------------------------------------------------------------------
5) Finally a correct code:-


abstract class aaa
{
abstract public void sam();
public void tom()
{
System.out.println("Hello");
}
}
abstract class kkk extends aaa
{
public void boy()
{
System.out.println("Bye");
}
}
----------------------------------------------------------------------------------
6) Another correct code


abstract class aaa
{
abstract public void sam();
public void tom()
{
System.out.println("Hello");
}
}
class kkk extends aaa
{
public void sam()
{
System.out.println("God is great");
}
public void boy()
{
System.out.println("Bye");
}
}
class mmm
{
public static void main(String mm[])
{
aaa a;
a=new kkk();
a.sam();
a.tom();
}
}
----------------------------------------------------------------------------------
A class can be declared as an abstract even if it doesn't have an abstract method.


abstract class aaa
{
public void tiger()
{
System.out.println("Hello");
}
}
//Correct code because the aaa class can't be instantiated.
----------------------------------------------------------------------------------

No comments:

Post a Comment

Meetme@