Which of the following is true about class inheritance

This book is now obsolete Please use CSAwesome instead.

One of the really useful features of Object-Oriented programming is inheritance. You may have heard of someone coming into an inheritance, which often means they were left something from a relative that died. Or, you might hear someone say that they have inherited musical ability from a parent. In Java all classes can inherit object fields and methods from another class. The class being inherited from is called the parent class or superclass. The class that is inheriting is called the child class or subclass.

When one class inherits from another, we can say that it is the same kind of thing as the parent class (the class it inherits from). For example, a car is a kind of vehicle. This is sometimes called the is-a relationship, but I prefer is-a kind of. A motorcycle is another kind of vehicle. All vehicles have a make, model, and year that they were created. All vehicles can go forward, backward, turn left and turn right.

Which of the following is true about class inheritance

Figure 1: A UML Class Diagram Showing Inheritance

A UML (Unified Modeling Language) class diagram shows classes and the relationships between the classes as seen in Figure 1. An open triangle points to the parent class. The parent class for Car and Motorcycle is Vehicle. The Vehicle class has two child classes or subclasses: Car and Motorcycle.

11.4. Specifying the Parent Class¶

How is a parent class specified? Use the Java keyword extends after the class name and then followed by the parent class name to specify the parent class as shown below.

public class Car extends Vehicle
public class Motorcycle extends Vehicle

Note

While a person has two parents, a Java class can only inherit from one parent class. If you leave off the extends keyword when you declare a class then the class will inherit from the Object class. The Person class declared below will inherit from the Object class.

11.5. Why Use Inheritance?¶

Inheritance allows you to reuse data and behavior from the parent class. It is useful for generalization in which case you may notice that several classes share the same data and/or behavior and you pull that out into a parent class. Customers and Employees are both people so it makes sense use the general Person class. It is also useful for specialization which is when you want most of the behavior of a parent class, but want to do at least one thing differently and/or add more data. An example of specialization is the Employee class below. An employee is a person but also has a unique id. A customer is a person, but also has a credit card.

Which of the following is true about class inheritance

Figure 2: A UML Class Diagram Showing Inheritance

Test Your Understanding

    10-3-1: If you don’t specify the parent class in a class declaration which of the following is true?

  • It doesn't have a parent class.
  • If no parent class is specified using the extends keyword, the current class will still have a parent class.
  • It inherits from the Object class.
  • If no parent class is specified using the extends keyword, the parent class will be Object.
  • It inherits from the Default class.
  • There isn't a class named Default.
  • It inherits from the Parent class.
  • There isn't a class named Parent.

    10-3-2: If the class Vehicle has object fields of make and model and the class Car inherits from the class vehicle will a car object have a make and model?

  • Yes
  • Yes, a child class inherits all the parent class object field and methods.
  • No
  • Why would inheritance be useful if you didn't actually get anything from the parent class?

    10-3-3: If I had a class ParkingGarage should it inherit from the class Vehicle?

  • Yes
  • Is a parking garage a kind of vehicle?
  • No
  • No, a parking garage is not a kind of vehicle. Instead it has vehicles in it which implies that the ParkingGarage class would have a field that tracks the vehicles in it.

    10-3-4: In Java how many parents can a class have?

  • 0
  • In Java a class always has at least one parent class. If none is specified the default is Object.
  • 1
  • All classes in Java have one and only one parent class.
  • 2
  • While that is how it works with humans, is that how Java works?
  • infinite
  • This is true for interfaces, but not parent classes.

You have attempted of activities on this page

Which of the following are correct statements about the limitation of kotlin data classes in Android?

Data Class Limitations They can't be open , abstract , sealed or inner classes. The compiler forbids manually implementing copy() and componentN() methods. Any parent interface or class of a data class must not have a copy() method.

Which of the following is true about the lifecycle of a single activity?

Which of the following is true about the lifecycle of a single activity? Choose as many answers as you see fit. onStart() can be called multiple times, while onCreate() can only be called once. onStop() can be called multiple times, while onPause() can only be called once.

Which of the following is an example of a class Kotlin?

To define a class in Kotlin, class keyword is used: class ClassName { // property // member function ... .. ... } Here, we defined a class named Lamp . The class has one property isOn (defined in same way as variable), and two member functions turnOn() and turnOff() .

How do you mark a property to be used only inside its class and subclasses?

How do you mark a property to be used only inside its current class? - Use the override keyword. - Use the val keyword. - Use the private keyword.