• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I'm having a problem for some reason I am getting a java.lang.NoSuchMethodError exception when I run my program but everything compiles fine,I understand inheritance but for some reason I'm getting this message here is my code.







the exact error is java.lang.NoSuchMethodError: mm2: method(init)() not found

at mm3.(init)(mm3.java:1)
at mm1.main(mm1.java:7)


Thanks guys any help would be appreciated =) thanks
 
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

Does the code compile? ... because you really shouldn't be running code that doesn't compile.

Henry
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NoSuchMethodError(←link) suggests you compiled the code, then removed a method from one class and compiled that class on its own. Now you have removed a method and the rest of the code won't work.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:NoSuchMethodError(←link) suggests you compiled the code, then removed a method from one class and compiled that class on its own. Now you have removed a method and the rest of the code won't work.



the code compiles just as it should, but I didn't remove any methods.I thought that once you declare a public method in one class and if you make it a super class of another by using the keyword extends in the subcless you can you that superclasses method which I called in the main program but get that exception.
 
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
Adam Chalkley wrote:the code compiles just as it should,


The mm3 class does *not* compile, as the mm2 superclass doesn't have the required no-arg constructor.

Adam Chalkley wrote: ... but I didn't remove any methods.


I will speculate that you recently added a constructor to the mm2 class -- which of course, will remove the default no-arg constructor. And you forgot to go back to recompile your mm3 class.

Henry
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Adam Chalkley wrote:
the code compiles just as it should, but I didn't remove any methods.


The mm3 class does *not* compile, as the mm2 superclass doesn't have the required no-arg constructor.

Henry


how come I need a constuctor for that to work? I'm not passing any arquements I'm just trying to print a method from the sub class which should have been inherited from the super class? plus I thought if a no arg constructor is not made that java makes a default no args constuctor
 
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
Adam Chalkley wrote: ... plus I thought if a no arg constructor is not made that java makes a default no args constuctor


Incorrect. If no constructor (at all) is provided then a default no-args constructor is provided. If any constructor is provided, then the java compiler doesn't add any constructor.

BTW, why don't you just try compiling the mm3 class again? You will see that it doesn't compile.

Henry
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Adam Chalkley wrote: ... plus I thought if a no arg constructor is not made that java makes a default no args constuctor


Incorrect. If no constructor (at all) is provided then a default no-args constructor is provided. If any constructor is provided, then the java compiler doesn't add any constructor.

BTW, why don't you just try compiling the mm3 class again? You will see that it doesn't compile.

Henry


yup did not compile at all,did after I added the default constructor,but how come I needed a default constructor on my class mm2 and not need a constructor on mm3 as I'm just calling a method from the superclass?
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not just calling a superclass method in your mm3 class -- the method isn't static, so you can't call it until you construct an mm3 object. And for that you need a constructor. You don't have one, so the compiler inserts a default zero-argument constructor, but that still isn't good enough because that default constructor tries to call the zero-argument constructor of the superclass. Which it can't, because there isn't one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!