Mohammad Fadin
Mohammad Fadin

Reputation: 579

Java Polymorphism code "Cannot find Constructor`

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);
 }
 }

Upvotes: 0

Views: 223

Answers (3)

COD3BOY
COD3BOY

Reputation: 12112

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.

Upvotes: 1

Ben
Ben

Reputation: 13645

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

Upvotes: 0

Tobias
Tobias

Reputation: 9380

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.

Upvotes: 3

Related Questions