Class Inheritance

Help Questions

AP Computer Science A › Class Inheritance

Questions 1 - 10
1

What does the code print?

class Parent{

final public void show() {

`` System.out.println( "Parent::show() called" );

`` }

}

``

class Child extends Parent {

`` public void show() {

`` System.out.println( "Child::show() called" );

`` }

}

``

public class Main {

`` public static void main(String[] args) {

Parent parent = new Child();

parent .show();

`` }

}

Compiler Error

Parent::show() called

Parent::show() called

Child::show() called

Child::show() called

Runtime Error

Explanation

Final methods can't be overriden. So the code won't compile because of this. Now if the final modifier were to be removed. The code would compile and run and produce:

Child::show()

2

What does the code print?

class Parent{

final public void show() {

`` System.out.println( "Parent::show() called" );

`` }

}

``

class Child extends Parent {

`` public void show() {

`` System.out.println( "Child::show() called" );

`` }

}

``

public class Main {

`` public static void main(String[] args) {

Parent parent = new Child();

parent .show();

`` }

}

Compiler Error

Parent::show() called

Parent::show() called

Child::show() called

Child::show() called

Runtime Error

Explanation

Final methods can't be overriden. So the code won't compile because of this. Now if the final modifier were to be removed. The code would compile and run and produce:

Child::show()

3

What does the code print?

class Parent{

final public void show() {

`` System.out.println( "Parent::show() called" );

`` }

}

``

class Child extends Parent {

`` public void show() {

`` System.out.println( "Child::show() called" );

`` }

}

``

public class Main {

`` public static void main(String[] args) {

Parent parent = new Child();

parent .show();

`` }

}

Compiler Error

Parent::show() called

Parent::show() called

Child::show() called

Child::show() called

Runtime Error

Explanation

Final methods can't be overriden. So the code won't compile because of this. Now if the final modifier were to be removed. The code would compile and run and produce:

Child::show()

4

True or False.

The class BetterMan inherits from the class Man.

public class BetterMan extends Man {

}

True

False

Explanation

The class BetterMan inherits methods from the class man. The keyword "extends" means that BetterMan will get all the methods from Man plus be able to extend the class by adding its own methods. All methods from Man can be used in BetterMan by calling the keyword better.

5

True or False.

The class BetterMan inherits from the class Man.

public class BetterMan extends Man {

}

True

False

Explanation

The class BetterMan inherits methods from the class man. The keyword "extends" means that BetterMan will get all the methods from Man plus be able to extend the class by adding its own methods. All methods from Man can be used in BetterMan by calling the keyword better.

6

True or False.

The class BetterMan inherits from the class Man.

public class BetterMan extends Man {

}

True

False

Explanation

The class BetterMan inherits methods from the class man. The keyword "extends" means that BetterMan will get all the methods from Man plus be able to extend the class by adding its own methods. All methods from Man can be used in BetterMan by calling the keyword better.

7

True or False.

The class BetterMan inherits from the class Man.

public class BetterMan extends Man {

}

True

True

False

False

Explanation

The class BetterMan inherits methods from the class man. The keyword "extends" means that BetterMan will get all the methods from Man plus be able to extend the class by adding its own methods. All methods from Man can be used in BetterMan by calling the keyword better.

8

True or False.

The class BetterMan inherits from the class Man.

public class BetterMan extends Man {

}

True

False

Explanation

The class BetterMan inherits methods from the class man. The keyword "extends" means that BetterMan will get all the methods from Man plus be able to extend the class by adding its own methods. All methods from Man can be used in BetterMan by calling the keyword better.

9

Consider the following code:

public class Rectangle {

private double width, height;

public Rectangle(double w,double h) {

width = w;

height = h;

}

``

public double getArea() {

return width * height;

}

``

public double getPerimeter() {

return 2 * width + 2 * height;

}

}

``

public class Square {

private double side;

public Square(double s) {

side = s;

}

public double getArea() {

return side * side;

}

public double getPerimeter() {

return 4 * side;

}

}

Which of the following represents a redefinition of Square that utilizes the benefits of inheritance?

public class Square extends Rectangle {

public Square(double s) {

super(s,s);

}

}

public class Square extends Rectangle {

private double side;

public Square(double s) {

super(s,s);

}

}

public class Square implements Rectangle {

public Square(double s) {

super(s,s);

}

}

public class Square extends Rectangle {

public Square(double s) {

new Rectangle(s,s);

}

}

public class Square {

private Rectangle r;

public Square(double s) {

r = new Rectangle(s,s);

}

public double getArea() {

return r.getArea();

}

public double getPerimeter() {

return r.getPerimeter();

}

}

Explanation

We know that a square really is just a subclass of a rectangle, for it is merely a rectangle having four sides that are all equal. Using inheritance, you can very easily reuse much of your Rectangle code. First, you need to extend the Rectangle class:

public class Square extends Rectangle { . . .

Next, you can be rid of the field side. This allows you to alter the constructor for Square to call the Rectangle constructor. You do this using super(because Rectangle is the superclass).

After this, you can delete the getArea and getPerimeter methods, for they will be handled by the superclass. This gives you a very simple bit of code!

10

Consider the following code:

public class Rectangle {

private double width, height;

public Rectangle(double w,double h) {

width = w;

height = h;

}

``

public double getArea() {

return width * height;

}

``

public double getPerimeter() {

return 2 * width + 2 * height;

}

}

``

public class Square {

private double side;

public Square(double s) {

side = s;

}

public double getArea() {

return side * side;

}

public double getPerimeter() {

return 4 * side;

}

}

Which of the following represents a redefinition of Square that utilizes the benefits of inheritance?

public class Square extends Rectangle {

public Square(double s) {

super(s,s);

}

}

public class Square extends Rectangle {

public Square(double s) {

super(s,s);

}

}

public class Square extends Rectangle {

private double side;

public Square(double s) {

super(s,s);

}

}

public class Square extends Rectangle {

private double side;

public Square(double s) {

super(s,s);

}

}

public class Square implements Rectangle {

public Square(double s) {

super(s,s);

}

}

public class Square implements Rectangle {

public Square(double s) {

super(s,s);

}

}

public class Square extends Rectangle {

public Square(double s) {

new Rectangle(s,s);

}

}

public class Square extends Rectangle {

public Square(double s) {

new Rectangle(s,s);

}

}

public class Square {

private Rectangle r;

public Square(double s) {

r = new Rectangle(s,s);

}

public double getArea() {

return r.getArea();

}

public double getPerimeter() {

return r.getPerimeter();

}

}

public class Square {

private Rectangle r;

public Square(double s) {

r = new Rectangle(s,s);

}

public double getArea() {

return r.getArea();

}

public double getPerimeter() {

return r.getPerimeter();

}

}

Explanation

We know that a square really is just a subclass of a rectangle, for it is merely a rectangle having four sides that are all equal. Using inheritance, you can very easily reuse much of your Rectangle code. First, you need to extend the Rectangle class:

public class Square extends Rectangle { . . .

Next, you can be rid of the field side. This allows you to alter the constructor for Square to call the Rectangle constructor. You do this using super(because Rectangle is the superclass).

After this, you can delete the getArea and getPerimeter methods, for they will be handled by the superclass. This gives you a very simple bit of code!

Page 1 of 4
Return to subject