Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
1  
It means precisely what it states. Anything specific about your confusion? –  delnan Jan 15 '12 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 Jan 15 '12 at 9:39

4 Answers 4

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.

share|improve this answer

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.

share|improve this answer
    
Other than type Exception can you provide a another example? I kinda still don't get it. –  user962206 Jan 15 '12 at 10:32

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

share|improve this answer
1  
which means that Boxes, Beagle is an instance of Dog? –  user962206 Jan 15 '12 at 9:45
    
Boxer myBoxer = new Boxer(); myBoxer is an Instance of Boxer as well as an instance of a Dog object. –  Monty0018 Jan 15 '12 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 Jan 15 '12 at 10:01
    
But this thing is not allowed Boxer Dog = new Dog() am right? I am just clearing out the concepts. –  user962206 Jan 15 '12 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 Jan 15 '12 at 10:17

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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