Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a doubt about reference types

 
K Srinivasan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right. Not a bad explanation of subclassing.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can compare these concepts with real world scenarios which will be very easily understandable and meaningful...
 
K Srinivasan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you try?
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21362
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic