Social Icons

twitterfacebookgoogle pluslinkedinemail

Wednesday 14 August 2013

how to use abstract class in java

abstarct class means , we cannot create the instance of abstract class.
whenever we create the abstract class in java we cannot use the property of that class through the instance of that class.
so, how can we access the property of abstract class. we can access the property and method of abstract class using inheritance.
we can create the child class and inherit the abstract class and through the child class we can access the property of abstract class.




abstract class shape()
{
abstract draw()
}

class circle extends shape
{
int radius;
circle(int r)
{
radius = r;
}
draw()
{
int area; 
final float pie = 3.14;
area = pie * radius * radius;
}
}
class demo
{
public static void main(String args[])
{
circle c1 = new circle();
}
}|

here,we have created one abstract class and its own empty constructor .
now, we have create the child class and circle and inherit the abstract class here, child class own constructor
circle and the draw constructor of abstract class we have been modify the body draw class so its overriding
and at last we are creating one object of circle class and using the property of abstract class

also, we can create the abstract method. 

No comments :

Post a Comment

 

Free Advertisement

Free Advertisement

Free Advertisement

Free Advertisement