• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it the method not overridden?  RSS feed

 
Alexandru Puiu
Greenhorn
Posts: 22
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

So I have the following code:



I don't get it. Why does "super1.method();" not compile? The method in the Sub is called and it doesn't declare any exceptions.

Are the exceptions thrown by the method in Super inherited by the method in Sub? And if so, why "sub.method();" compiles? Or is it more of a technicality? I though that after the method it's overridden, that method "belongs" to the object, not to the reference anymore. Polymorphism, right? I knew that Java still takes a look at the method in Super just to make sure I don't declare a broader exceptions, but I thought that was it.

Thanks in advance!
Alex
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has to do with this line: This is legal because Sub is a Super, right?  So super1 is a type Super.  Therefore when its method() is called, it's Super's, not Sub's.

It may be easier to think of it if we give Sub and Super "real" names, like Car and Toyota.  Toyota is a Car, so is legal, and will call Car's method.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your call, on line 16, to method() may throw an exception. You either need to surround the line with a try/catch or declare that main() throws Exception.

It would be helpful in the future if you would post the complete compilation error message.
 
Alexandru Puiu
Greenhorn
Posts: 22
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, but the method "method()" is overridden at line 25 in Sub. So it is not Super's method anymore. It should be Sub's method.  If you look at line 14, "anotherMethod()" is also overridden and the output is  "In sub". Like it should be. I'm sure that if I write throws Exception is the main method declaration, and put "System.out.println("In sub");" also in the "method()" at line 25, the code will compile and the output will be "In sub".

I was wandering if you guys have an explanation on what is happening behind the curtains. Why doesn't the compiler accepts the call at line 16, considering that the method is overridden and is no longer the one from Super.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you change the line in the main method to this one, it does compile:


So the method is overridden. And when declaring the type as Sub, the compiler is happy because it knows no exception is declared. However when the variable declaration is of type Super, the compiler doesn't "know" that it is a Sub class. Even though it is. Maybe you redefine super1 later to really be a Super. So the compiler is protecting you.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why doesn't the compiler accepts the call at line 16, considering that the method is overridden and is no longer the one from Super.

It's not that the method is overridden, it's that super1 is of type Super, not Sub, so it only sees Super's method.
 
Alexandru Puiu
Greenhorn
Posts: 22
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. Maybe you redefine super1 later to really be a Super. So the compiler is protecting you.
"   -- now I get the logic behind it. Thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!