0

I'm trying to explain java Polymorphism to my self so I've simply created a project showing that Family is the SuperClass and SubClasses areBrothersSisters`

The thing is when I compile I receive an error saying that Cannot find the Constructor Sisters Cannot find the Constructor Brothers

Could someone explain to me?

Thanks guys.

class Family {

private String name,age;

public Family(String name,String age){

    this.name = name;
    this.age = age;

}

public String toString(){

    return "name : " + name + "\tage " + age ;
}
}

class Brothers extends Family{

public Brothers(String name, String age){
    super(name,age);
}
}
class Sisters extends Family{

public Sisters(String name, String age){
    super(name,age);
}

 }

class FamilyTest{

public static void main(String[] args){

Family[] Member= new Family[3];

Member[1] = new Sisters("LALA",22);
Member[2] = new Brothers("Mike",18);
 }
 }
1
  • 1
    When learning about inheritance and polymorphism is is very important to keep the terminology straight and intuitive. Here: Is Brother a kind of Familiy? No, it is a FamilyMember. Please use this "... is a kind of ..." test upfront and before declaring classes. Commented Oct 9, 2011 at 13:17

3 Answers 3

3

You have age definded as String but you pass an integer to it.

Member[1] = new Sisters("LALA", "22");
Member[2] = new Brothers("Mike", "18");

should work but I would advice you to change age from String to int.

Sign up to request clarification or add additional context in comments.

Comments

1
public static void main(String[] args)
{
Family[] Member= new Family[3];

Member[1] = new Sisters("LALA","22");
Member[2] = new Brothers("Mike","18");

}

Replace the main() with this code,

The error was : arguments for the constructors of sisters and brothers were String, but you passed age as an Integer .

Sugggestion : you may change the type of age to int, which is more correct.

Comments

0

Please note that this is just one of the types of polymorphism you can use in Java, others are Generics and Function overloading

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.