...

View Full Version : Question about inheritance



Jero
02-15-2008, 03:57 PM
When I have two classes: Super and Sub, and I have Sub to extend Super, I get a compile error I'm unable to understand:


Cannot find symbol - constructor Super()

Super class:

public class Super
{
public Super(String param) {}
}

Sub class:

public class Sub extends Super
{
public Sub(int param) {}
}

Why would I need the constructor Super()?

Thanks in advance.

Jero
02-15-2008, 09:37 PM
Never mind, one of my friends helped my out with this.

Just for the record: it appeared I had to call a constructor of the Super class to make it work:


public class Sub extends Super
{
public Sub(int param)
{
super("Blah.");
}
}

amitthechosen1
02-17-2008, 03:52 PM
If you are defining any constructor in the Sub Class, then the super class needs to have the DEFAULT constructor. Since in the above case, your Super class did not have the default constructor, hence the CTE.

You can alternatively rectify it by defining the default constructor like :

public class Super
{
// default constructor
public Super(){}

public Super(String param) {}

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum