• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Can super class access overridden method in sub class

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tested this code and out put is "class B - return D". I am confused with this out put. getOBJ() is in class A. Now class B extends class A and overrides getOBJ(). If a reference is created of class type A, it can access members and methods available in class A. So in this case, a.getOBJ() should print "class A - return C".

Please explain, why is this rule not followed here and how can reference of type A access methods of sub class B ?

Thanks
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nirjari patel wrote:If a reference is created of class type A, it can access members and methods available in class A.



That's a compile-time rule that determines which method signatures we're allowed to call, and which of the allowed signatures will be called at runtime. This looks only at the declared type of the reference (A in this case), not at the object that the reference points to (B in this case).

Then, at runtime, we look at the actual class of the object (B in this case) and if that class overrides the method with the signature determined at compile time (and in this case it does), then we call that class's version. If not, we move up to its parent class, and then its parent, and so on.

This is the whole point of inheritance and overriding. It's why it exists.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.

One more thing, when I declare all classes in one file with source file name as B. I get errors stating class A must be defined in its own file, class C must be defined in its own file, class D must be defined in its own file..

I read in one of the sample questions mock tests that a file can contain more than one classes. Source file name must be with the name in which main() is. So why does this not work when I am putting all classes in one file with file name as B, as main() is in B.

 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has nothing to do with main(). The file must be named after the top-level public class. main() is irrelevant.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I named this file as B after including all classes in one file. That is giving error.

Now how shall I name this file to include all classes in one source file ?

Thanks
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should not include multiple top-level public classes in one file. Give each a file of their own.

And, always show the error, don't just tell us about it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if you want to have multiple top-level classes in one source file, you have to make sure that at most one of them is public.

This is not good practice though, as already mentioned. I never do it in real code, but occasionally to when I'm writing a tiny throw-away program to test or demonstrate something.
 
I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic