Abstract classes in JAVA


Abstract

We can identify Abstract method or Abstract class by declaration of keyword abstract

Abstract class cannot be instantiated

Object cannot be created for abstract class

Abstract class can be extended

Abstract class

Abstract class cannot be instantiated and has a abstract keyword.

Concrete Class is that what be basically define with class keyword followed by class name and can be extended to a abstract class.

Abstract class can be extended.

Why Abstract class cannot be instantiated?

Imagine that you have a project for a Pet animal shop. Here you have a animal class a dog class and a cat class. Now you can create object for Dog and Cat class which means it has a behavior. For example dog it should bark sound and cat should give Meaw sound. But what does animal give. Does this make sense. No so in order to overcome this problem we go for this abstract class where the compiler will not allow us to create object.

Abstract Method

Abstract method is a method that is declared with abstract keyword and does not have implementation

Abstract method should not have a body and it should end with a semicolon

Abstract methods can be overridden. Most of the cases it will be Over Ridden

Ex : abstract void disp();

Abstract With main method

abstract class AbsMainInAbs

{

public static void main(String args[])

{

System.out.println(“Thats Wow”);

}

}

Abstract class with Inheritance to concrete class and Abstract method

class AbsBasicTry1 extends AbsMethod

{

void disp()

{//contents here

}

public void gearType()

{

System.out.println(“GearType method and AbsBasicTry1 called”);

}

void run()

{

System.out.println(“void run called”);

}

}

class AbsBasicTry

{

public static void main(String args[])

{

AbsBasicTry1 t=new AbsBasicTry1();

t.disp();

t.run();

t.gearType();

}

}

abstract class AbsMethod

{

void disp()

{

System.out.println(“this is better”);

}

abstract void run();

abstract void gearType();

}

About pacesettergraam

A good and realistic person

Posted on December 29, 2012, in coding, Java and tagged , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: