• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a doubt about reference types

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's right. Not a bad explanation of subclassing.
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you try?
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .....
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic