Unit 9: Inheritance

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

The super keyword can be used from the subclass for the following:

All of these choices (To call the superclass constructor, To call a public method in the superclass, To refer to the superclass object)

From a design perspective, what goes in the subclass?

All of these options (Modified/Overridden methods, Additional instance variables specific to the subclass, Additional methods specific to the subclass)

If a class does not extend any class, what happens?

By default, we extend the Object class

What types of things do we put in the Superclass?

Common Attributes and Behaviors

If the Student class and Worker class both extend the Person class, how can I create an array to store 10 objects of any of the three classes?

Person[] people = new Person[10];

What is the definition of polymorphism?

Polymorphism lets a method do different things depending on the type of the object it is called on.

Which of the following best describes a Subclass / Superclass relationship?

Dog / Animal

Which of the following methods would you find in the Object class?

toString

Approximately how many methods are contained in the Object class for us to use?

11

Given the following code: public class Animal { public void speak(String sound) { System.out.println(sound); } } public class Dog extends Animal { public void bark() { super.speak("Bark"); } } Which of the following lines would generate a compile time error when placed in a main method?

Animal karel = new Dog(); karel.bark();

When does a method override occur?

Anytime a subclass method signature matches that of the superclass

Given the following code: public class Dog { /* implementation not shown */ } public class MixedDog extends Dog { /* implementation not shown */ } What is the correct way to create an array list to store both MixedDog objects and Dog objects?

ArrayList<Dog> dogs = new ArrayList<Dog>();

Methods in the Object class are useful as is and seldom need to be overridden.

False

True or False: 'Is A' relationships are symmetrical.

False

When we talk about to classes having a Superclass/Subclass relationship, which type of relationship do we have?

Is A

What does the Object class equals() method compare? Choose the best answer.

It compares whether two variables are represented by the same object.

When a program runs, how does Java choose which methods to run?

It looks first in the instantiated class, then checks the superclasses if the method is not found.

How does Java determine the order to execute methods between superclasses and subclasses?

Java first looks in a subclass for a method signature. If it is found there, it executes that method. Otherwise it checks the superclass methods

If a superclass constructor exists and a subclass doesn't explicitly call the that constructor, what happens?

Java implicitly calls the no-argument superclass constructor

Given the following code: public class Dog { private String type; public Dog (String theType) { type = theType; } /* other methods omitted */ } public class MixedDog extends Dog { private String breed1; private String breed2; public MixedDog (String theType, String breedOne, String breedTwo) { super(theType); breed1 = breedOne; breed2 = breedTwo; } /* other methods omitted */ } The following code segment appears in a method in another class. Dog clover = new Dog("Beagle"); // Line 5 Dog butch = new MixedDog("Beabull", "Beagle", "Bulldog"); // Line 12 MixedDog bailey = new MixedDog("Pitsky", "Pitbull", "Husky"); // Line 20 MixedDog macey = new Dog ("Chug"); // Line 22

Line 22 will error

In order for a program to compile, any method that an object uses ...

Must be defined in the class where the object is declared.

Given the following code: public class Animal { public void speak(String sound) { System.out.println(sound); } } public class Dog extends Animal { public void bark() { super.speak("Bark"); } } Which of the following lines would generate a run time error when placed in a main method?

None of these will generate a run time error. (Dog karel = new Dog(); karel.bark(); Animal karel = new Animal(); karel.speak("Woof"); Animal karel = new Dog(); karel.bark();)

Every object created in Java can use the Object class methods since the object class is at the top of the hierarchy.

True

True or False: A subclass can be extended by another class to become a superclass.

True

How does a Subclass call the Superclass constructor?

Using the super keyword in the first line of the subclass constructor.

Why might we want to override a superclass method? Select the best option.

We override a method because a subclass needs to define a behavior in a different way than the superclass.

Why do we use the @Override in our code?

When we use it, the compiler checks that we are actually overriding the method we intended to.

Does a Subclass need to have a constructor?

Yes, the subclass has its own constructor, either explicit or implicitly created.

Which keyword do we use to establish the inheritance relationship between a subclass and a superclass?

extends

Given the following class structure: public class Person { /* implementation not shown */ } public class Student extends Person { /* implementation not shown */ } Which of the following objects can be created? Person obj1 = new Student(); Student obj2 = new Student(); Student obj3 = new Person(); Person obj4 = new Person();

obj1, obj2, obj4

Given the following code, what is the correct way for the subclass to call the speak method in the Animal class? public class Animal { public void speak(String sound) { System.out.println(sound); } } public class Dog extends Animal { public void bark() { /* Missing code */ } }

super.speak("Bark");

Which of the following is the correct syntax to call the superclass constructor?

super(parameters)


संबंधित स्टडी सेट्स

Psych Flash Cards Ch. 13 (Early Adulthood)

View Set

Chapter 9 - Social Media and Data Analytics

View Set

Inheritance and Composition Chapter 11

View Set

Chem AP Classroom Midterm Review

View Set