• 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

Java Inheritance...

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I was looking through the SCJP from the beginning, and I was just wondering if
when Class A extends Class B, does Class A have access to Class B's Public methods
as well as any Protected?

This is the beginning of what may seem to be noobish questions..

Thanks a lot,

Justin Fox
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

A superclass does not have access to a subclass. A subclass automatically includes all accessible methods from the superclass.

Animal fido = new Dog();

fido has all the public methods of Animal.

But if you want to use the public methods of Dog in Animal, you would have to create a Dog instance. To Animal, Dog is any old class, and it treats it as it would Food or LampPost objects.
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this is just for curiosity than it's ok otherwise in real life application parent class should not access/dependent on child class.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ha, I asked the question backwards lol.

I mean to ask if B has access to the public/protected methods of A.

Thanks for the info. though.

Justin
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, when a method in class B overrides a method in class A. When class A envokes a method in class A that has been overridden by class B, the class B method will execute. In reference to your original question.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes, when a method in class B overrides a method in class A. When class A envokes a method in class A that has been overridden by class B, the class B method will execute. In reference to your original question.



Ok, now I'm getting kind of confused...

If B is the super class and A is the subclass:
no method in B can override a method in class A.

If A is the super class and B is the subclass:
if B overrid a method that it inherited from A,
and A calls its method, A's version will execute. If
B calls the same method, the overrid version in class B will Execute.

Example:



Output:
A
B
B
Special Message

What I'm trying to say is the quote of above (to me) doesn't make much sense
unless there is backwards inheritance or something like that....

Thanks,

Justin Fox

 
Don Solomon
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this...

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lol that's just twisting it around....

you might as well say:

B b = new B();
b.method();

when would this atcually be usefull?

Thanks,

Justin
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because technically, A isn't 'overriding' B's method() method, you're just
using the fact that at runtime the JVM will know that the instance of a is actually
and object of B and call its method instead..

Justin
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider Father as super class and son as sub class.

A son can aquire all the properties from the father whereas the father cannot. This is the basic concept of inheritance.

`
On overriding,

If a son posses some habits, you may tell those habits are aquired from his father, but you cannot say the father aquired the habits from his son. hope you got it
 
Don Solomon
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recommend that you study the core fundamental property of Polymorphism. Very important concept/behavior.

This concept allows one to change behavior without changing interface. Thanks, Don
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand polymorphism and inheritance fully,

What I'm trying to say is, A super class CANNOT override a sub classes method.

Justin

lol nice analogy
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Justin Fox wrote:I understand polymorphism and inheritance fully,

What I'm trying to say is, A super class CANNOT override a sub classes method.

Justin

lol nice analogy

Correct. A superclass doesn't "know" about its subclasses at all.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic