• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Inheritance with extended classes

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, I'm having some problems understanding this code.


A. The code will print 10 and 40 if // 3 is commented.
B. The code will print 40 if // 2 and // 3 are commented.
C. The code will not compile because of // 1.
D. The code will compile if the line marked // 2 is commented out.
E. None of these.



Apparently the correct answer is B. Now, I would have thought that C could access i (so c.i was valid) because C is A anyway and even if B's i is private, surely C inherits A's i. But apparently if we want to call i from C we have to do and what I don't get is the reason for the cast.
ANy idea?
thanks
 
author
Posts: 23959
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
Please QuoteYourSources.

Henry
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! jason,"i" is a private variable in class B so you have not inherit it during sub-classing B and by doing like this-
((A) C).i you are actually casting in accordance for the heirarchy,compiler interpret it as--->((A)((B) C)).i,this is all about how java compiler is intelligent.remember in java a class can only have a single parent for ex-
Class HeirarchySub-classingIS-A Relationship
Class Asub-class of ObjectA is-a Object,A's only parent is object
Class Bsub-class of AB is a A but A is-a Object =>B is a Object,B's only parent is A
Class Csub-class of BC is a B but B is-a A =>C is a A & A is a Object => C is a object,C's only parent is B

the only parent of C is B,B is A,A is Object.But due to is-a relationship and polymorphism,that holds according to the class heirarchy,we can cast class C to A which is up in the heirarchy(acc. to C).and one more thing you have quote-

Jason Attin wrote:surely C inherits A's i

this is incorrect,c only inherits its super class B member(though it cannot inherit B's private member acc. to the inheritance rule).
so the only reason for casting is a multiple-level inheritance(Acc.to the class heirarchy).

Hope this will help!

kind regards,
Praveen.
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please QuoteYourSources.


Right, sorry it's Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Location 4738). Enthuware. Kindle Edition.


OK I understand now thanks.
A couple of other points though:
1)Usually if you go up the hierarchy, say you want to access from a subclass variables or methods of a superclass you don't need to cast because if A is superclass of B then B is A. But you need to cast if you go down the hierarchy, correct?
2)In a slightly different scenario, say we have again superclass A, subclass B extending A and subclass C extending B, and let's say that all the variables are all coderanch, then all the members in A are also inherited by B and by C, so C effectively has members in A. Correct?
 
Henry Wong
author
Posts: 23959
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

Jason Attin wrote:
1)Usually if you go up the hierarchy, say you want to access from a subclass variables or methods of a superclass you don't need to cast because if A is superclass of B then B is A. But you need to cast if you go down the hierarchy, correct?



For variables (including ones that hides other variables), that is correct. If you have an A reference that is pointing to a B instance, then you need to cast in order to use B instance variables.

For instance methods (that correctly overrides), casting is not necessary. Instance methods are polymorphic, meaning the overridden method is called.

Henry
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
remember this rule while casting-
--->(class you are casting) IS-A (class to which you are casting)=>if this statement holds cast is permissible or other wise you will get a ClassCastException,you can always check for it by using instanceof operator before casting to avoid this kind of error.

Jason Attin wrote:1)Usually if you go up the hierarchy, say you want to access from a subclass variables or methods of a superclass you don't need to cast because if A is superclass of B then B is A. But you need to cast if you go down the hierarchy, correct?

i am not pretty sure what you intended to say but it is not possible to perform a cast down the heirarchy.for-ex-
suppose their is  class A,B is a sub-class of A and C is a sub-class of B.

Jason Attin wrote:2)In a slightly different scenario, say we have again superclass A, subclass B extending A and subclass C extending B, and let's say that all the variables are all coderanch, then all the members in A are also inherited by B and by C, so C effectively has members in A. Correct?


Yes all the members of A are inherited in C indirectly and due to the fact that all members are publically accessible,indirectly means via multiple-level-inheritance(i.e.A to B,B to C) but not multiple inheritance.

Kind Regards,
Praveen.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic