7.4k views
2 votes
How would you declare an interface named Sports that has the following (show them in your answer as well): A method called jump that has no parameters and does not return anything. A method called throw that has one parameter called distance and is an integer and does not return anything. 2) [5 pts] Assuming the interface exist from 1) above. Give me the class header for a class named Football that would use this interface. 3) [5 pts] Assuming a parent class called GrandParents exist. How would you create a child class called GrandChild that would be a direct child of the class GrandParents

1 Answer

2 votes

Answer:

See explanation

Step-by-step explanation:

An interface is declared as follows:

interface interface_name{

member fields

methods }

So the Sports interface is declared as:

interface Sports{ }

You can also write its as:

public interface Sports { }

Now this interface has a method jump that has no parameters and does not return anything. So it is defined as:

void jump();

You can also write it as:

public void jump();

Notice that it has a void keyword which means it does not return anything and empty round brackets show that it has no parameters. Lets add this to the Sports interface:

interface Sports{

void jump(); }

You can write it as:

public interface Sports {

public void void jump(); }

Next, this Sports interface has a method throw that has one parameter called distance and is an integer and does not return anything. So it is defined as:

throw(int distance);

You can also write it as:

public void throw(int distance)

Notice that this function has a void return type and one int type parameter distance. Now lets add this to Sports interface:

interface Sports{

void jump();

void throw(int distance); }

This can also be written as:

public interface Sports {

public void jump();

public void throw(int distance); }

Next, the class header for a class named Football that would use this interface is as follows:

class Football implements Sports{ }

You can use the methods of Sports in Football too. So the complete interface with class Football becomes:

interface Sports{

void jump();

void throw(int distance);

}

class Football implements Sports{

public void print(){

}

public void throw(int distance) {

}

Next we have a parent class called GrandParents. So child class called GrandChild that would be a direct child of the class GrandParents is:

class GrandChild extends GrandParents { }

Basic syntax of a parent class and its child class is:

class child_class_name extends parent_class_name

{

//member fields and methods

}

User Forhad Ahmed
by
4.7k points