*
The moose likes Beginning Java and the fly likes a doubt about reference types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a doubt about reference types" Watch "a doubt about reference types" New topic
Author

a doubt about reference types

K Srinivasan
Ranch Hand

Joined: Mar 08, 2010
Posts: 34
hi..
i was thinking ..
why is it that if Class B extends class A,
we can create a reference of type A and point it to an object of class B.
Is it because since A is the super class , so it implies that B will have atleast all the instance variables and methods that A has. ? Hence we can make reference of A point to object of type B. ?

Thanks..
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Yes, that's right. Not a bad explanation of subclassing.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

You can compare these concepts with real world scenarios which will be very easily understandable and meaningful...


SCJP 6 [SCJP - Old is Gold]
K Srinivasan
Ranch Hand

Joined: Mar 08, 2010
Posts: 34
but then ,
what if some methods in the base class are private , then they won't be available with the extended class.
How does it work in this scenario ?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Why don't you try?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

M Srinivasan wrote:but then ,
what if some methods in the base class are private , then they won't be available with the extended class.
How does it work in this scenario ?


Why not you try making two classes and try it out by yourself it will be more helpful to you..if you cannot understand then tell here
K Srinivasan
Ranch Hand

Joined: Mar 08, 2010
Posts: 34
thanks , i tried it out..
now , why is that a sub class reference can not point to a super class object, although super class will have the most of the methods that the sub class has .....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

M Srinivasan wrote:but then ,
what if some methods in the base class are private , then they won't be available with the extended class.
How does it work in this scenario ? :rolleyes:


Does it really matter? Private methods are not accessable from outside the class, so it isn't defined as a required part of the class (its more implementation detail). Inheritance is about the subclass being able to behave as the base class -- it does not mean that it should have full access to the base class.

M Srinivasan wrote:thanks , i tried it out..
now , why is that a sub class reference can not point to a super class object, although super class will have the most of the methods that the sub class has .....


I think you need to take a step back, and examine OO concepts first. Trying to understand why references can refer to particular types of objects, and why, is a lot easier once you understand the theory first.

Henry
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

M Srinivasan wrote:thanks , i tried it out..
now , why is that a sub class reference can not point to a super class object, although super class will have the most of the methods that the sub class has .....


just to inform you.
if we are able to point subclass reference to a superclass object then private method and static method of the superclass will become not reachable and this will give compile time error.
just to stop such kind of disaster to happen then compile is made in a such a way that it will stop you to do this because there are situations when you are not having any private and static method in your superclass.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a doubt about reference types