Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class

 
V Bunny
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Why an Abstract class can not be instantiated?for what reasons.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
V Bunny
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AnkitJi Garg
can you tell me what is the use of abstract class.
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gaurav Chauhan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract classes :

-> combine similar methods/properties in one class
-> implement methods according to scenario
-> helps during inheritance
 
John M Morrison
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

If there is a abstract class then why we need interface.The abstract class itself satisfied our needs know.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit,

Please read your private messages regarding an important announcement.

Thank you,

Rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic