This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why abstract classes in java do not have objects, any strong reasons ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why abstract classes in java do not have objects, any strong reasons ?" Watch "Why abstract classes in java do not have objects, any strong reasons ?" New topic
Author

Why abstract classes in java do not have objects, any strong reasons ?

Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
why abstract class cannot be instantiated, please explain with some e.g ?


Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Vinod,

in an abstract class you can define an interface (method declarations) and optionally some behavior that's already known. But you want to say explicitly with the abstract concept that the class in question is not complete and therefore no objects can be created directly from it. Therefore child classes have to fill in the missing parts and then you can create objects from these child classes.

Marco
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Vinod Vijay wrote:why abstract class cannot be instantiated, please explain with some e.g ?
An abstract class is one that cannot be instantiated. That is what it means.You might have methods like takePassenger() in Car and Bus, sellTicket() in Bus, and collectGoods() in Truck. Those methods would be different in all cases.
You might have a method like buyFuel() in Vehicle, which is abstract because a Car might use petrol/gasoline, a Truck might use Diesel and (in some places) a Bus might use electricity.
You might have methods like turnRight() turnLeft() go() and stop() implemented in Vehicle because they are the same in all three subclasses.

You would never want an object of the Vehicle class, only Car Bus and Truck. But all three of those objects are also objects of Vehicle.
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
in an abstract class you can define an interface
as said by Marco Ehrentreich

what do you mean by defining an interface inside an abstract class.
As far as i know, an abstract class may have abstract and non-abstract methods.
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
I think Marco probably meant that an abstract class with only method declarations and no behaviour is similar to an interface.
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
Thank you so much Campbell Ritchie for your easy live example.
but please could you give me a real life example of Inheritance also.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Earlier, I wrote:. . .You might have methods like takePassenger() in Car and Bus, sellTicket() in Bus, and collectGoods() in Truck. Those methods would be different in all cases.
You might have a method like buyFuel() in Vehicle, which is abstract because a Car might use petrol/gasoline, a Truck might use Diesel and (in some places) a Bus might use electricity.
You might have methods like turnRight() turnLeft() go() and stop() implemented in Vehicle because they are the same in all three subclasses.

You would never want an object of the Vehicle class, only Car Bus and Truck. But all three of those objects are also objects of Vehicle.
That's an example of inheritance
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
I'm sorry...Campbell Ritchie ...i'm asking for Interfaces with any suitable real-life example....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Oh, you want interfaces? I thought the earlier request about inheritance sounded peculiar Try here.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Sorry, I couldn't find the time to reply earlier!
I think Marco probably meant that an abstract class with only method declarations and no behaviour is similar to an interface.

That's right. Interface in this context was just meant as an abstract concept or contract which doesn't necessarily have to corrspond directly to a Java interface. Of course a Java interface can correspond to the concept "interface" too.

Marco
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
hi Campbell Ritchie and others....thanks for your precious time...
i'm sorry......i didn't understand with your explanation. Could you please give me any other real-life example(like vehicle abstract class).

Thanks again.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Please have a look at this tiny example. In particular at the abstract shape class. Here the implementations for the simple setColor() and getColor() methods are already known and defined in the abstract base class "Shape". In contrast the method area() depends on the concrete child class of Shape and therefore it's left as an abstract method because there's no reasonable default implementation for "Shape". That means any child class CAN override setColor() or getColor() if this is needed. But at least each concrete child class HAS TO implement the area() method because this is only abstract.

Here is a short summary of advices when it makes sense to use an interface and for which problems an abstract class might be better.

Marco
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why abstract classes in java do not have objects, any strong reasons ?
 
Similar Threads
why HttpServlet is abstract?
HttpServlet class
Why awt is called Abstract
New Java Language Features
about Class AbstractCollection