• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Inheritance

 
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Henry
 
Marshal
Posts: 80634
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic