• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me understand why code does not compile  RSS feed

 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Wizards:

In studying for an exam at school, I have been going over practice questions and encountered a disconnect. The question below does not compile. The compiler's wrath is directed at line 10. So I thought OK, NewClass must be out of scope.




And then the code below compiles just fine and it has the same situation (lines 10 & 11) where one class instantiates another at default access.



So now I do not understand the top question why it did not compile?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Apart from the class name, what is the difference?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as Stefans mentioned you can't instantiate class like you used for 'NewClass'. You have to use new keyword as shown by him for 'Test' class.
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, I can't believe I didn't see that. It's the little things that trip me up the hardest.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry about it. We have all made that sort of mistake.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Roberts wrote:Wow, I can't believe I didn't see that. It's the little things that trip me up the hardest.

I think it's a cognitive problem that comes from name "NewClass", the same thing that may have filled in the missing "the" in this sentence when you read it. I fell victim to it just now, too, BTW -- I didn't notice the missing new either.
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well while we are on the subject, I typed up the code below and was wondering how overloaded methods are chosen with objects?



Lines 24, 26, and 28 all call the method in class A. I thought the methods had to be at or above the class in the inheritance structure? When I call aObj.m(bObj) the method m inside class A is still used?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overloaded methods are a form of early binding. They are resolved at compile time according to the types of the parameters.
Overridden methods are late binding - resolved at runtime according to the actual class of the object the method is being invoked on.

Because the method m has a different method signature, class B does not actually override m, but overloads it.
So Class B has two methods called m, one of which takes parameter type A, the other B

>When I call aObj.m(bObj) the method m inside class A is still used?
Correct.
Your object aObj has a declared type A and a runtime type of A. It only knows about the method in A, so that is the one that gets invoked.

What about a call to

 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the class B is a subtype of class A then bObj “IS-AN” A.
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
calls the method inside the B class. That much makes sense.

I guess what is confusing me is I read that an object inherits all methods in its ancestral line and in the case of



I have called the m() inside class B (right?) but have passed it a class A object. The m() inside class B accepts only B objects as parameter. I expected it to call the method inside class A.

And this line of code is even more confusing because I have called the method inside class A and passed it a class B object. Class A does not inherit from B. In this case I expected the compiler to blow up.

I am utterly confused.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Roberts wrote:
I guess what is confusing me is I read that an object inherits all methods in its ancestral line and in the case of



I have called the m() inside class B (right?) but have passed it a class A object. The m() inside class B accepts only B objects as parameter. I expected it to call the method inside class A.


The B class has two m() methods (after all, we are talking about overloading here). One declared in the B class definition, and one inherited from the A class. You passed it a parameter that matches exactly one of the two methods. So, why did you not expect that method to be called?

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Roberts wrote:
And this line of code is even more confusing because I have called the method inside class A and passed it a class B object. Class A does not inherit from B. In this case I expected the compiler to blow up.

I am utterly confused.


Class B extends class A... so doesn't that means that an instance of class B IS-A instance of class A? An instance of B is supposed to work everywhere that expects an A, so why should it "blow up"?

Henry
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Travis Roberts wrote:
And this line of code is even more confusing because I have called the method inside class A and passed it a class B object. Class A does not inherit from B. In this case I expected the compiler to blow up.

I am utterly confused.


Class B extends class A... so doesn't that means that an instance of class B IS-A instance of class A? An instance of B is supposed to work everywhere that expects an A, so why should it "blow up"?

Henry


Well I was just trying to communicate the chaos in my mind. I do not know why it would compiler error, that was just my assumption. Obviously, I do not yet understand overloading and polymorphism.

I know that the reference variable is not totally arbitrary, when I try to use reference variable of type Object it will not work. So in the case of

aObj.m(bObj);

Does the reference variable aObj not matter?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Roberts wrote:
aObj.m(bObj);

Does the reference variable aObj not matter?


What do you mean by "not matter"? As already mentioned in a previous post, the compiler uses the type to determine the method to call. And since it is a class A instance, it will call the m() method that takes a class A instance, as that is the only method that matches.

Henry
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Travis Roberts wrote:
aObj.m(bObj);

Does the reference variable aObj not matter?


What do you mean by "not matter"? As already mentioned in a previous post, the compiler uses the type to determine the method to call. And since it is a class A instance, it will call the m() method that takes a class A instance, as that is the only method that matches.

Henry


Yes, but I am passing in an instance of B class, and the m() method in the A class accepts a A class object as a parameter, not B class..

Maybe I just need to study more. The online book that comes with the class I am taking doesn't go over the codes I posted - at least not that I can recall - but I found them in a practice test.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Roberts wrote:
Yes, but I am passing in an instance of B class, and the m() method in the A class accepts a A class object as a parameter, not B class..


Since you mentioned that you understood polymorphism (in a previous post) ... then ... since an instance of B class IS-A instance of A class, passing an instance of B class in place of an A class is perfectly valid.

[EDIT: rereading the previous post, you mentioned that you did not understand polymorphism ... yeah, you are going to need to understand it, and understand why the subclass IS-A type of the super class]

Henry
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!