This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Java Inheritance... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Inheritance..." Watch "Java Inheritance..." New topic
Author

Java Inheritance...

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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


You down with OOP? Yeah you know me!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
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

Joined: Aug 16, 2007
Posts: 1374
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

Joined: Jan 24, 2006
Posts: 802
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

Joined: Jul 20, 2008
Posts: 48
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.


Software development is an exercise in thinking not coding.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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

Joined: Jul 20, 2008
Posts: 48
Try this...

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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

Joined: Jan 24, 2006
Posts: 802
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

Joined: Dec 29, 2008
Posts: 162
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


The greater danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it
Don Solomon
Ranch Hand

Joined: Jul 20, 2008
Posts: 48
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

Joined: Jan 24, 2006
Posts: 802
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

Joined: Oct 13, 2005
Posts: 37953
    
  22
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.
 
 
subject: Java Inheritance...
 
Similar Threads
WA #1.....word association
New In Java need a little help please
O'Reilly, Manning, Wrox, or Other
double + double =?
Polymorphism? Quick Question