aspose file tools *
The moose likes Beginning Java and the fly likes Abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract class" Watch "Abstract class" New topic
Author

Abstract class

V Bunny
Greenhorn

Joined: Dec 31, 2007
Posts: 20
Hi all,

Why an Abstract class can not be instantiated?for what reasons.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9322
    
  17

Because an Abstract class is incomplete. See this example-


Now statement (1) will generate an error in Java. But if it had not, then what would happen at statement (2)? The call will try to call a method that has no body. Then what will happen? This is why you cannot instantiate an abstract class.
However you can create a reference variable which has the abstract class as its type-

To instantiate this object you will have to create an instance of a concrete sub-class of the A class. That is you will have to create an object of a class that extends the A class and provides body to the display method like this-

Now the new code for the main method can be something like this-

This will print Hello! on the Console.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
V Bunny
Greenhorn

Joined: Dec 31, 2007
Posts: 20
AnkitJi Garg
can you tell me what is the use of abstract class.
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 444
    
    1

Hi,

The purpose of an abstract class is to group similar functionality together and leave the different ones to child classes which can then implement them as desired. Most books that teach Inheritancs and OO concepts would give the Vehicle example, with the Vehicle being the abstract base class that implement either partially or completely the characteristics common to all vehicles and leaving the rest to child classes such as for example, Car, Bike, Bus...... Hope this helps?

Cheers,
Raj.
[ August 12, 2008: Message edited by: Raj Kamal ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19799
    
  20

Originally posted by AnkitJi Garg:
Now statement (1) will generate an error in Java. But if it had not, then what would happen at statement (2)? The call will try to call a method that has no body. Then what will happen?

An AbstractMethodError would be thrown.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gaurav Chauhan
Greenhorn

Joined: Apr 14, 2008
Posts: 27
Abstract classes :

-> combine similar methods/properties in one class
-> implement methods according to scenario
-> helps during inheritance


Don't tell me there is nothing beyond sky, There are footprints on moon.
John M Morrison
Greenhorn

Joined: Jul 25, 2005
Posts: 21
Abstract classes provide an intermediate facility between regular (concrete) classes and (totally abstract) interfaces.

If you are creating a related family of classes, you can avoid maintaining duplicate code by pushing implmentations as far up the class tree as you can to their common origins. You can leave methods abstract for which you want polymorphism but for which you do not yet have enough specificity for a meaningful implementation. An abstract class is a "partially implemented" interface.

Take note that any class with an abstract method MUST be abstract, but you may declare a class abstract even if none of its methods is abstract.

In no case, may you instantiate an abstract class.
V Bunny
Greenhorn

Joined: Dec 31, 2007
Posts: 20
Hi all,

If there is a abstract class then why we need interface.The abstract class itself satisfied our needs know.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9322
    
  17

But you cannot inherit two abstract classes but you can inherit two interfaces.
For example there's a class Manager. The employee is an Employee as well as a Father or a Husband...Then the Manager class cannot look like this

class Manager extends Employee, Husband

but it can do this

class Manager implements Employee, Husband.
Shahnawaz Shakil
Ranch Hand

Joined: Aug 04, 2008
Posts: 57
Java doesn't support multiple inheritance because its a big nightmare in some scenario. Here the only option left is to implement multiple interfaces. Abstract classes will not be helpful here.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19799
    
  20

Ankit,

Please read your private messages regarding an important announcement.

Thank you,

Rob
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract class