• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in Constructors

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Sup{
public Sup(String str){
System.out.println("Super class");
}
}
public class Test2 extends Sup{
public Test2(){
System.out.println("Sub class");
}
public static void main(String[] args) {
Test2 t2 = new Test2();
}
}


When i am trying to compile the above of code ii am facing the following
java.lang.NoSuchMethodError: main
Exception in thread "main"


Can any one please identify the error and provide the solution.
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

please us the "Code" tag to post source code, it makes it much easier to read:



Alex
[ February 08, 2008: Message edited by: Alex Belisle Turcot ]
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

To fix your code, you either need to add a default (no-args) constructor in "Sup" or explicitly call "super" from your "Test2" constructor.


OR


Regards,
Alex
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your main seems good to me, my guess would be how you run it.
What command to you execute to run it ? Or do you run it from your editor ?
You should be running "Test2" which is the class containing the main.

Perhaps you are trying to run "Sup" ?

Regards,
Alex
[ February 09, 2008: Message edited by: Alex Belisle Turcot ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Narayana",
Please check your private messages.
-Ben
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Narayana.Vaddi",
Please check your private messages, again.
-Ben
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Sup{
public Sup(){}
public Sup(String str){
System.out.println("Super class");
}
}
public class Test2 extends Sup{
public Test2(){
System.out.println("Sub class");
}
public static void main(String[] args) {
Test2 t2 = new Test2();
}
}


---Sup() {} added
-- Default constructor (no-arg) are removed when you declare your own constructor. and subclass implicitly calls super() construcor in the first line. you must provide the default constructor for the class Sup
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic