-1

I have read once in a book about exception where "an object of type Exception can be an instance of any subclassof Exception". I kinda get it, but there's still confusion can anyone clarify me the meaning of this?

2
  • 1
    It means precisely what it states. Anything specific about your confusion?
    – user7043
    Commented Jan 15, 2012 at 9:22
  • can anyone provide me another example that is related to that, that's the confusion. it doesn't seem so clear to me, maybe another example of something related to it would do
    – user962206
    Commented Jan 15, 2012 at 9:39

4 Answers 4

6

Author means to say that base class pointers can point to any object of derived classes.

A derived class will have the public/protected members of the base class. So, the casting is implicit and not harmful.

Example: We have a class vehicle.

Class car is derived from it, and so is truck.

So, if you create an object of car or truck type, the object can obviously point to its base class vehicle. This crux of it is the is-a relationship. A car is-a vehicle.

2

The point the author is getting at is when you have a variable that's declared to be of type Exception, the actual object assigned to that variable is not necessarily an instance of Exception itself - it may also be any of the subclasses of Exception.

1
  • Other than type Exception can you provide a another example? I kinda still don't get it.
    – user962206
    Commented Jan 15, 2012 at 10:32
0

Exception subclasses consist of objects that extend Exception, ie NullPointerException, ClassNotFoundException, OutOfRangeException. Only subclasses of Exception will ever be an Exception object.

From wikipedia;

In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects.

Boxer, Beagle, Collie = Subclasses of Dog

5
  • 1
    which means that Boxes, Beagle is an instance of Dog?
    – user962206
    Commented Jan 15, 2012 at 9:45
  • Boxer myBoxer = new Boxer(); myBoxer is an Instance of Boxer as well as an instance of a Dog object.
    – Monty0018
    Commented Jan 15, 2012 at 9:56
  • will this do? Dog Boxer = new Boxer() but the Boxer will only have the methods of Dog am I right? to access the methods of Boxer I'll type cast it right?
    – user962206
    Commented Jan 15, 2012 at 10:01
  • But this thing is not allowed Boxer Dog = new Dog() am right? I am just clearing out the concepts.
    – user962206
    Commented Jan 15, 2012 at 10:01
  • I made a little snippet at IDEone here: ideone.com/Nvm2C It isn't allowed implicitly. But you can always do so explicitly like the casting operators in C++. cplusplus.com/doc/tutorial/typecasting
    – c0da
    Commented Jan 15, 2012 at 10:17
0

The point of polymorphism is that objects of a certain class can be treated in the same way as objects of their parent class, although their behavior may be different.

The prime example in Java is the Object class. All other classes in Java are subclasses of Object, and so can be used wherever Object can. A List is an ordered collection of Objects--thus (at least prior to Java 1.5) a List could hold 3 Foo objects, 14 Bar objects, and single Bat object. You don't necessarily know up front what the actual class of the objects in the List are, except that they are Objects.

Another good example in Java is the Number class. An Integer is an Number, so anywhere you can use a Number, you can use an Integer, or any of the other subclasses of Number (Long, Float, Double, Short, BigDecimal, AtomicInteger, etc). All of these subclasses of Number are a little different--Floats represent real numbers, whereas Integers represent integers--but all of them are Numbers and so can be described and accessed in terms of them being numbers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.