Ch. 10 - Inheritance

Ace your homework & exams now with Quizwiz!

Protected class members are denoted in a UML diagram with the symbol:

#

A protected member of a class may be directly accessed by:

All of the above (methods of the same class, methods of a subclass, & methods in the same package)

All fields declared in an interface:

Are final and static

Look at the following code and determine what the call to super will do. public class ClassB extends ClassA { public ClassB() { super(10); } }

It will call the constructor of ClassA that receives an integer as an argument

A subclass can directly access:

Only public and protected members of the superclass

If two methods have the same name but different signatures, they are:

Overloaded

If you do not provide an access specifier for a class member, the class member is given ___________ by default.

Package

TRUE/FALSE: When a subclass extends a superclass, the public members of the superclass become public members of the subclass.

True

In UML diagrams, inheritance is shown:

With a line that has an open arrowhead at one end that points to the superclass

When a method is declared with the ____________ modifier, it cannot be overridden in a subclass.

final

When declaring class data members, it is best to declare them as:

private members

Which of the following statements correctly specifies three interfaces?

public class ClassA implements Interface1, Interface2, Interface3

Which of the following statements declares Salaried as a subclass of PayType?

public class Salaried extends PayType

When an "is a" relationship exists between objects, it means that the specialized object has:

All of the characteristics of the general object, plus additional characteristics.

TRUE/FALSE: An abstract class is not instantiated, but serves as a superclass for other classes.

True

A subclass may call an overridden superclass method by:

Prefixing its name with the super key word and a dot (.)

What key word can you use to call a superclass constructor explicitly?

Super

If a class contains an abstract method

The method will have only a header, but not a body, and end with a semicolon

In a class hierarchy:

The more general classes are toward the top of the tree and the more specialized are toward the bottom.

In an interface all methods are:

public access

Look at the following code. Which line will cause a compiler error? Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public final int method1(int a){} Line 5 public double method2(int b){} Line 6 } Line 7 public ClassB extends ClassA Line 8 { Line 9 public ClassB(){} Line 10 public int method1(int b){} Line 11 public double method2(double c){} Line 12 }

10

Look at the following code. The method in line ________ will override the method in line __________. Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public int method1(int a){} Line 5 public int method2(int b){} Line 6 } Line 7 public ClassB extends ClassA Line 8 { Line 9 public ClassB(){} Line 10 public int method1(int b){} Line 11 public int method2(double c){} Line 12 }

10, 4

If ClassC extends ClassB, which extends ClassA, this would be an example of:

A chain of inheritance

When a subclass overloads a superclass method:

Both methods may be called with a subclass object

Like a family tree, a ____________ shows the inheritance relationship between classes.

Class hierarchy

In the following statement, which is the subclass? public class ClassA extends ClassB implements ClassC

ClassA

In the following statement, which is the superclass? public class ClassA extends ClassB implements ClassC

ClassB

Given the following code which of the following is true? public class ClassB implements ClassA{}

ClassB must override each method in Class A

In the following statement, which is the interface? public class ClassA extends ClassB implements ClassC

ClassC

What is wrong with the following code? public class ClassB extends ClassA { public ClassB() { int init = 10; super(40); } }

The call to the method super must be the first statement in the contstructor

Look at the following code. Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public void method1(int a){} Line 5 } Line 6 public class ClassB extends ClassA Line 7 { Line 8 public ClassB(){} Line 9 public void method1(){} Line 10 } Line 11 public class ClassC extends ClassB Line 12 { Line 13 public ClassC(){} Line 14 public void method1(){} Line 15 } Which method will be executed as a result of the following statements? ClassB item1 = new ClassA(); item1.method1();

This is an error and will cause the program to crash.

TRUE/FALSE: Every class has a toString method and an equals method inherited from the Object class.

True

TRUE/FALSE: Every class is either directly or indirectly derived from the Object class.

True

TRUE/FALSE: It is not possible for a superclass to call a subclass's method.

True

TRUE/FALSE: Private members of the superclass cannot be accessed by the subclass.

True

The super statement that calls the superclass constructor:

Must be the first statement in the subclass's constructor

Look at the following code. The method in line _______ will override the method in line _______. Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public int method1(int a){} Line 5 public double method2(int b){} Line 6 } Line 7 public ClassB extends ClassA Line 8 { Line 9 public ClassB(){} Line 10 public int method1(int b, int c){} Line 11 public double method2(double c){} Line 12 }

None of the above

Look at the following code. Which line has an error? Line 1 public interface Interface1 Line 2 { Line 3 int FIELDA = 55; Line 4 public int methodA(double){} Line 5 }

4

Look at the following code. What is missing from ClassA? Line 1 public interface MyInterface Line 2 { Line 3 int FIELDA = 55; Line 4 public int methodA(double); Line 5 } Line 6 public class ClassA implements MyInterface Line 7 { Line 8 FIELDA = 60; Line 9 public int methodB(double) { } Line 10 }

It does not override methodA

Protected members are:

Not quite private and not quite public

If a superclass does not have a default constructor or a no-arg constructor:

Then a class that inherits from it, must call one of the constructors that the superclass does have

Look at the following code. Which line will cause a compiler error? Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public int method1(int a){} Line 5 public final int method2(double b){} Line 6 } Line 7 public ClassB extends ClassA Line 8 { Line 9 public ClassB(){} Line 10 public int method1(int b){} Line 11 public int method2(double c){} Line 12 }

11

Look at the following code. Which line in ClassA has an error: Line 1 public interface MyInterface Line 2 { Line 3 int FIELDA = 55; Line 4 public int methodA(double); Line 5 } Line 6 public class ClassA implements MyInterface Line 7 { Line 8 FIELDA = 60; Line 9 public int methodA(double) { } Line 10 }

8

If a class contains an abstract method:

All of the above (you cannot create an instance of the class, the method will have only a header, but not a body, and end with a semicolon, & the method must be overridden in subclasses)

TRUE/FALSE: All methods in an abstract class must also be declared abstract.

False

TRUE/FALSE: If a method in a subclass has the same signature as a method in the superclass, the subclass method overloads the superclass method.

False

TRUE/FALSE: If two methods in the same class have the same name but different signatures, the second overrides the first.

False

TRUE/FALSE: In an inheritance relationship, the subclass constructor always executes before the superclass constructor.

False

TRUE/FALSE: Inheritance involves a subclass, which is the general class, and a superclass, which is the specialized class.

False

TRUE/FALSE: When an interface variable references an object, you can use the interface variable to call any and all of the methods in the class implementing the interface.

False

When one object is a specialized version of another object, there is this type of relationship between them.

Is a

In the following code, what will the call to super do? public class ClassB extends ClassA { public ClassB() { super(40); System.out.println("This is the last statement "+ "in the constructor."); } }

It will call the constructor of ClassA that receives an integer as an argument

If a subclass constructor does not explicitly call a superclass constructor:

Java will automatically call the superclass's default or no-arg constructor just before the code in the subclass's constructor executes.

Look at the following code. Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public void method1(){} Line 5 } Line 6 public class ClassB extends ClassA Line 7 { Line 8 public ClassB(){} Line 9 public void method1(){} Line 10 } Line 11 public class ClassC extends ClassB Line 12 { Line 13 public ClassC(){} Line 14 public void method1(){} Line 15 } Which method1 will be executed as a result of the following statements? ClassA item1 = new ClassC(); item1.method1();

Line 14

Look at the following code: Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public void method1(int a){} Line 5 } Line 6 public class ClassB extends ClassA Line 7 { Line 8 public ClassB(){} Line 9 public void method1(){} Line 10 } Line 11 public class ClassC extends ClassB Line 12 { Line 13 public ClassC(){} Line 14 public void method1(){} Line 15 } Which method1 will be executed when the following statements are executed? ClassA item1 = new ClassB(); item1.method1();

Line 9

Replacing inadequate superclass methods with more suitable subclass methods is known as what?

Method overriding

What is wrong with the following code? public class ClassB extends ClassA { public ClassB() { super(40); System.out.println("This is the last statement " + "in the constructor."); } }

Nothing is wrong with the code

Which of the following is true about protected access?

Protected members may be accessed by methods in the same package or subclass, even when the subclass is in a different package

If ClassA extends ClassB, then:

Public members in ClassB are public in ClassA, but private members in ClassB cannot be directly accessed in ClassA

In an inheritance relationship:

The superclass constructor always executes before the subclass constructor

Look at the following code: Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public void method1(int a){} Line 5 } Line 6 public class ClassB extends ClassA Line 7 { Line 8 public ClassB(){} Line 9 public void method1(){} Line 10 } Line 11 public class ClassC extends ClassB Line 12 { Line 13 public ClassC(){} Line 14 public void method1(){} Line 15 } Which method will be executed when the following statements are executed? ClassC item1 = new ClassA(); item1.method1();

This is an error and will cause the program to crash


Related study sets

BCOR 199 Chapter 8. Strutting Organizations for Today's Challenges

View Set

which one of the following is not one of the four risk management principles

View Set

RN Mental Health Theories & Therapies Assessment - Mental Health

View Set

NJN History Topic 3 – Ancient Egypt and Kush Study Guide

View Set

Catcher in the Rye Chapters 22-26 Vocabulary

View Set

Unit 6: Environmental Regulations

View Set

Chapter 10 - Emotional and Social Development in Middle Childhood

View Set