Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Inheritance...

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 48946
60
  • Mark post as helpful
  • send pies
  • 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.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Don Solomon
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • 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
  • 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: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this...

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Sujai Kaarthik
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • 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: 48
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Pie
Posts: 48946
60
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic