This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Base Class, Sub Class Confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Base Class, Sub Class Confusion" Watch "Base Class, Sub Class Confusion" New topic
Author

Base Class, Sub Class Confusion

Preety Narashimhan
Greenhorn

Joined: Aug 19, 2002
Posts: 28
Hi all,
I have a problem here regd the base and sub class,
Assumen the subclass extends the base class.
SubClass objSC = new SubClass();
BaseClass objBC = objSC;
Now here the object of the BaseClass refers to the SubClass?
So all methods of the SubClass can be referred to by objBC?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Now here the object of the BaseClass refers to the SubClass?

Yes.
So all methods of the SubClass can be referred to by objBC?

No. Through objBC you can only call the methods of BaseClass. However, if SubClass overrides a method of BaseClass it will be SubClass's version of the method that will be executed by the call.
-Barry
[ October 02, 2002: Message edited by: Barry Gaunt ]
[ October 02, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Preety Narashimhan
Greenhorn

Joined: Aug 19, 2002
Posts: 28
Tx Barry,
But i am unable to understand, when the parent object (now referring to the subclass) cannot access any other methods of the SubClass( by which i mean those methods which are not overridden by the SubClass)...after all it has teh refernece of the SubClass ?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The BaseClass class does not necessarily have all the methods that the SubClass class has. So if you are looking at an instance of the SubClass "through" the BaseClass' view how can you call a method which it does not have?
-Barry
[ October 03, 2002: Message edited by: Barry Gaunt ]
Preety Narashimhan
Greenhorn

Joined: Aug 19, 2002
Posts: 28
Agreed a base calss cannot access methods it does not ahve but a baseclass object referring to a subclass .. that shud b able to since it now no longer points to the base class but to the subclass rt?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Sorry but I can only suggest that you read up on the subject by downloading a book from here.
-Barry
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Preety Narashimhan:
Agreed a base calss cannot access methods it does not ahve but a baseclass object referring to a subclass .. that shud b able to since it now no longer points to the base class but to the subclass rt?

Java is a statically typed language, that is, all method calls are checked versus validity at compile time. But the compiler doesn't know what object the BaseClass reference is pointing to, therefore it can only allow you to call BaseClass' methods.
What you are describing is possible in dynamically typed languages like Smalltalk and Ruby. In those languages you get an error at runtime when you call a method on an object that doesn't understand the message.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://jrebel.com/download
 
subject: Base Class, Sub Class Confusion
 
Similar Threads
constructor confusion
Sing me a song of Java (upcast & downcast)
Decorating objects..
explicit cast
no argument constructor