Inheritance Flashcards
What is inheritance?
Inheritance is a concept in object-oriented programming where a class can inherit properties and behaviors from another class.
What is an is-a relationship?
An is-a relationship is a type of relationship in which a subclass is a more specific version of its superclass.
Give an example of an is-a relationship.
A mammal is an animal. This is an example of an is-a relationship because a mammal is a more specific version of the general class type animal.
What is a superclass?
A superclass is a general class type that has properties and behaviors that can be inherited by its subclasses.
What is a subclass?
A subclass is a more specific version of a superclass that inherits properties and behaviors from its superclass.
Why do we use superclasses and subclasses in object-oriented programming?
We use superclasses and subclasses to factor out common behaviors and attributes into a superclass, which can be inherited by its subclasses. This reduces code redundancy and makes it easier to maintain and update the code.
What does the extends keyword do?
The extends keyword is used in object-oriented programming to create inheritance relationships between classes. It indicates that a subclass is inheriting properties and behaviors from a superclass.
What does the code “public class DVD extends Playable” mean?
This code is creating a class called DVD that is inheriting properties and behaviors from the Playable class. The DVD class is a subclass of the Playable class.
What does the code “public class CD extends Playable” mean?
This code is creating a class called CD that is inheriting properties and behaviors from the Playable class. The CD class is a subclass of the Playable class.
What are the benefits of inheritance?
The benefits of inheritance in object-oriented programming include:
All data and methods are located in one location, making the code easier to manage.
Maintenance is easier because changes made to the superclass are automatically inherited by its subclasses.
Avoidance of code redundancy and reimplementation of features.
Data and operations are available to all subclasses, making it easier to reuse code.
Exceptions in Java form an inheritance hierarchy, making it easier to handle errors in code.
Why does inheritance make maintenance easier?
Inheritance makes maintenance easier because changes made to the superclass are automatically inherited by its subclasses. This means that instead of making changes to each subclass individually, you can make changes to the superclass, and those changes will propagate to all of its subclasses.
How does inheritance avoid reimplementation of features?
Inheritance avoids reimplementation of features by allowing subclasses to inherit properties and behaviors from their superclass. This means that if a superclass already has a certain feature implemented, its subclasses do not need to reimplement that feature.
How does the inheritance hierarchy of exceptions work in Java?
In Java, exceptions form an inheritance hierarchy, which means that each exception class is a subclass of the Throwable class. This allows for more specific handling of exceptions, with subclasses inheriting properties and behaviors from their parent exception classes.
What is a superclass?
A superclass is a class that provides methods and attributes that are common to all of its subclasses.
What is the role of a superclass in object-oriented programming?
The role of a superclass in object-oriented programming is to provide common behaviors and attributes that can be inherited by its subclasses. This reduces code redundancy and makes it easier to maintain and update the code.
Can references store instances of subclasses?
Yes, references can store instances of subclasses. This is because a subclass is a type of its superclass, and can therefore be treated as an instance of that superclass.
Can methods in the superclass implement default behaviors unless overridden by the subclass?
Yes, methods in the superclass can implement default behaviors unless overridden by the subclass. This means that subclasses can inherit the default behavior of a superclass method, or they can choose to override that behavior with their own implementation.
What is a subclass?
A subclass is a class that inherits the methods and attributes of its superclass, and can also add its own additional methods and attributes.
How does a subclass inherit methods and attributes from its superclass?
A subclass inherits methods and attributes from its superclass by extending the superclass. This means that the subclass has access to all of the methods and attributes of the superclass, and can use them as if they were its own.
Can a subclass override methods in its superclass?
Yes, a subclass can override methods in its superclass. This is done by giving the method in the subclass the exact same declaration as the method in the superclass. This allows the subclass to provide its own implementation of the method, rather than using the implementation from the superclass.
Can you call public (and protected) methods of a superclass from a subclass?
Yes, you can call public (and protected) methods of a superclass from a subclass. This is because subclasses have access to all of the public and protected methods and attributes of their superclass.
What is the “protected” keyword in Java?
In Java, the “protected” keyword is an access modifier that allows a subclass to access a member (variable or method) of its superclass. Protected members can be accessed by any subclass that is on the extends path in the class hierarchy.
How does “protected” differ from “private”?
The “private” keyword in Java limits access to a member to only the class in which it is defined, while the “protected” keyword allows any subclass to access the member as long as it is on the extends path in the class hierarchy.
What is “super” in Java?
In Java, “super” is a keyword that refers to the superclass of a class. It can be used to call methods or access variables in the superclass from a subclass.
How is “super” used to call a method in the superclass?
To call a method in the superclass from a subclass, you can use the “super” keyword followed by the name of the method you want to call. For example, “super.myMethod()” will run “myMethod” in the superclass.
How is “super” used to call a constructor in the superclass?
To call a constructor in the superclass from a subclass, you can use the “super” keyword followed by the appropriate arguments for the constructor you want to call. For example, “super(…)” can be used to call the constructor of the superclass with the specified parameters. This is useful for reusing initialization code in the superclass when constructing a subclass.
What is the problem that can occur when both a superclass and its subclass have a method with the exact same name, return type, and parameters?
The problem that can occur is a name conflict, where the compiler may not know which method to use in a given context.
Why would a developer want to define a method with the same name, return type, and parameters in both a superclass and its subclass?
A developer might want to do this to provide default behavior for all objects of the superclass while also providing more specific behavior for objects of the subclass.