File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes inhertiance question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inhertiance question" Watch "inhertiance question" New topic
Author

inhertiance question

Emnaki Chih
Ranch Hand

Joined: Sep 01, 2004
Posts: 30


When I run this, it produces 1, 1 and 0 as output. When does the two printNum() produce different results?
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
it's easy to reason out given the output, why don't you tell us what you think is going on behind the scenes?
There's no magic at all, it's purely logical.

think different instances, think different classes.


42
Emnaki Chih
Ranch Hand

Joined: Sep 01, 2004
Posts: 30
Alright here is what I thought would happen. I increase super.num by one, which should increase the variable in the testor class by one. then I print the super.num, it prints the num value in the testor class. Now I call printNum(), because there is not over-ridding printNum method, it calls the printNum() in the super class testor. Then I call printNum() belonging to the testor class, which should call the same method as the previous printNum() right? Obviously this is not the case.
[ February 13, 2005: Message edited by: Emnaki Chih ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
remember your integer is an instance variable.
What does this mean for the value it has at any point?
Emnaki Chih
Ranch Hand

Joined: Sep 01, 2004
Posts: 30
Ok, is this what is happening? When I make an object of the Subclass, it also makes an object of the superclass. This object being different from the object I created in the main method. Therefore the two printNum methods belong to two differnt objects of the testor class. This makes sense, but is this right?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Emnaki Chih:
When I make an object of the Subclass, it also makes an object of the superclass.
Not exactly, though it seems to work that way. When you subclass (extend) a class, you add new state (variables) and behavior (methods). In your example, Test "is a" testor -- it just has different behavior.

Here's a more concrete example that may help explain it. You would extend class Animal with classes Dog and Cat. Dogs are Animals and Cats are Animals. Both are just more specialized versions of Animal.

Any feilds and methods defined in the superclass (that are not private) will be inherited by and accessible in the subclass. There's no need to write "super.num" since Test inherits num from testor.

If that makes sense, try to explain what's happening with the output now. If it doesn't (inheritence isn't easy), ask more questions and we'll go from there.

Here are a couple tips. Try to stick to Leading capital letters for classes and interfaces; your method names already match the standard Java style, but testor should be Testor. If you don't declare any constructors, Java gives you a public no-argument constructor for free, so "public Test(){}" is unnecessary.
Emnaki Chih
Ranch Hand

Joined: Sep 01, 2004
Posts: 30
I see. So the subclass does not create another object of the superclass, it IS another object of the superclass.


Any feilds and methods defined in the superclass (that are not private) will be inherited by and accessible in the subclass. There's no need to write "super.num" since Test inherits num from testor.


When I drop the super from my num, the results are 1, 0, 1 which is different from the original result.... why is that?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Well, the browser just ate my very long explanation of why this is so, so I'm going to summarize it. I'd just like to say that as far along in the world of computing as we are, I'm $^&#&*@ amazed that we still tolerate computers losing our work due to trivial bugs. Auto-save should be a "solved problem" -- even for browser text entry fields.

Okay, I feel better. On with the summary...

The reason it behaves this way is because Test is an inner class of its own super class. An inner class is a class defined inside a method. Instances of it have access to the instance variables of the object on which the method was called.

As a (very poor) example, imagine a really big dog eating a small dog. Now if you ask the small dog to bark, it can bark itself or it can make the big dog bark.

So in the modify() method of Test, it has access first to the "num" from the testor instance that created it. Second it has access to its own "num" inherited from the testor class. But the rules of inner classes mean that the containing instance wins out. "super.num" allows modify() to reference its own "num" variable.

With this example, there is a testor instance that "contains" a Test instance, which as you pointed out is also a testor itself. This makes "num" inside Test's methods ambiguous to us, but Java defines rules that the compiler uses to disambiguate it. This is similar to the rule that local variables declared inside a method "hide" class and instance variables of the same name.This code prints 2 1. The "this." is similar to the "super." in your example. So I just tried changing "super." to "this." and received the errorNow I'm really confused as I just noticed it is declared private. I assume there must be some rule that states that inner classes can break the rules of private access, but only if they prefix the variable with "super." first. That makes no sense to me, but then again I rarely use inner classes, and when I do it's not like this.

I can't think of any reason you'd want to have an inner class be declared inside itself.

Anyway, I'm going to go play a little FarCry and pretend the genetically engineered mutants are actually my browser.
 
Consider Paul's rocket mass heater.
 
subject: inhertiance question
 
Similar Threads
Javaprepare Question
Polymorphic Synchronized methods
Overriding static methods!
confused 'bout construcor
A quick question on threads