*
The moose likes Programmer Certification (SCJP/OCPJP) 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Abstract Class" Watch "Abstract Class" New topic
Author

Abstract Class

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
for eg : abstract class Car{}
It says that the above class cannot br instantiated
What do we exactly mean by the above words? does it means that it cannot be subclassed?
Which other classes can be instantiated??
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
in the contrary, an abstract class "must" be subclassed. The reason why people make classes abstract is that they want the subclasses (of the abstract class) to implement their behavior.
Let's say we have a class Vehicle with a method drive() in it. You cannot tell how to drive a vehicle. That's why you have to let the subclasses of Vehicle (Car,Ship,Aircraft,...) implement that behavior, and in terms of implementation it is done like this:

As you can see, abstract classes are a way of specifying the interface of a class but letting the subclasses implementing their behavior. This way you can have, for instance, an array of Vehicle and invoke the drive method on its elements without even having to care about what type of Vehicle is in the Array. This is called Polymorphism.

As you can see, we can reference Cars, Ships, ... with a Vehicle reference because a Car is_a Vehicle and a Ship is_a Vehicle too.
Moreover, we don't instantiate a Vehicle directly, but we instantiate its subclasses (Car and Ship). You cannot call new Vehicle() but you can call new Car() and new Ship() because the latter two are concrete classes.

HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform

[This message has been edited by Valentin Crettaz (edited December 10, 2001).]


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
No.."Cannot be instantiated" means you cannot create an object of this class.
Abstract classes have partial implementation or no implementation of methods in them.. It does not make sense to instantiate them. You have to create a subclass of the abstract class & implement the methods.
Originally posted by sonir shah:
for eg : abstract class Car{}
It says that the above class cannot br instantiated
What do we exactly mean by the above words? does it means that it cannot be subclassed?
Which other classes can be instantiated??

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Abstract Class