250,134 views
14 votes
14 votes
Create a class called Jack that extends Leo. This class must have the following behaviors/methods. You must use the inheritance to reuse the methods from the parent classes. Solution without using the inheritance will get zero points. re-writting the code that already exists will get zero credit. A call to the methods in the parent classes must be donemethod description

method1 display "Jack 1 Leo 1 "
method2 displays "Don 2 Jack 2"
method3 displays "Jack 3 Leo 3 Don 2 "
toString displays" Jack 3 Leo 1"

User Dawoud
by
2.4k points

1 Answer

7 votes
7 votes

Answer:

public class Leo{

public String method1(){

return "Jack 1 Leo 1";

}

public String method2(){

return "Don 2 Jack 2";

}

public String method3(){

return "Jack 3 Leo 3 Don 2";

}

public String toString(){

return "Jack 3 Leo 1"

}

}

public class Jack extends Leo{

}

Step-by-step explanation:

Leo is the parent class and jack inherits all of its attributes from Leo

So when you call the following code in the main method:

Jack j = new Jack();

System.out.println(j.method1);

it should print Jack 1 Leo 1

This is because the program first checks if method 1 exists in the Jack class

Because it doesn't exist it then goes to the parent class which is Leo.

And in the Leo class method1 displays "Jack 1 Leo 1 "

So it prints that

User Jchitel
by
2.6k points