Inheritance Flashcards

1
Q

What is inheritance?

A

Inheritance is a concept in object-oriented programming where a class can inherit properties and behaviors from another class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an is-a relationship?

A

An is-a relationship is a type of relationship in which a subclass is a more specific version of its superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give an example of an is-a relationship.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a superclass?

A

A superclass is a general class type that has properties and behaviors that can be inherited by its subclasses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a subclass?

A

A subclass is a more specific version of a superclass that inherits properties and behaviors from its superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why do we use superclasses and subclasses in object-oriented programming?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the extends keyword do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the code “public class DVD extends Playable” mean?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does the code “public class CD extends Playable” mean?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the benefits of inheritance?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why does inheritance make maintenance easier?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does inheritance avoid reimplementation of features?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does the inheritance hierarchy of exceptions work in Java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a superclass?

A

A superclass is a class that provides methods and attributes that are common to all of its subclasses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the role of a superclass in object-oriented programming?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can references store instances of subclasses?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Can methods in the superclass implement default behaviors unless overridden by the subclass?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a subclass?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How does a subclass inherit methods and attributes from its superclass?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Can a subclass override methods in its superclass?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Can you call public (and protected) methods of a superclass from a subclass?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the “protected” keyword in Java?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How does “protected” differ from “private”?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is “super” in Java?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How is “super” used to call a method in the superclass?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

How is “super” used to call a constructor in the superclass?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

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?

A

The problem that can occur is a name conflict, where the compiler may not know which method to use in a given context.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is the benefit of defining default behavior for objects of the superclass?

A

The benefit is that it reduces code duplication and makes the code more maintainable.

30
Q

What is the benefit of providing more specific behavior for objects of the subclass?

A

The benefit is that it allows for greater customization and flexibility in how objects of the subclass behave compared to objects of the superclass.

31
Q

When the “play()” method is called on an object that is an instance of a superclass with a matching method in a subclass, which method gets called?

A

The method that gets called depends on the actual object that the variable is referring to at runtime. If the object is an instance of the subclass, then the subclass method will be called. Otherwise, the superclass method will be called.

32
Q

What is method overriding?

A

Method overriding is when a subclass provides a specific implementation for a method that is already defined in its superclass.

33
Q

Why is method overriding useful?

A

Method overriding is useful because it allows for greater customization and flexibility in how objects of the subclass behave compared to objects of the superclass.

34
Q

When a method is overridden in a subclass, which version of the method gets called when the method is called on an object of that subclass?

A

When a method is overridden in a subclass, the version of the method in the subclass gets called when the method is called on an object of that subclass, independent of the type of the reference, because that is the “real” type of the object.

35
Q

What is reference casting?

A

Reference casting is the process of transforming a reference of one data type into another data type.

36
Q

Can you cast any object into any type of reference?

A

No, you can only cast an object into its own type or any of its superclasses. For example, if you have an object of type DVD, you can cast it into the DVD class, the Playable class, the Media class, or the Product class, but not into the Book class or any other unrelated types.

37
Q

What happens if you try to cast an object into an incompatible type?

A

If you try to cast an object into an incompatible type, a ClassCastException is thrown at runtime.

38
Q

How can reference casting be used in practice?

A

Reference casting can be used to access methods and attributes that are specific to a subclass, but not defined in the superclass. For example, if you have a reference of type Playable, you can cast it into a DVD reference to access the “getDirector()” method, which is not defined in the Playable class.

39
Q

What is Object in Java?

A

Object is a built-in class in Java that is the root of all class hierarchies. It is a reference type that can refer to any object in Java.

40
Q

What is the purpose of Object in Java?

A

The purpose of Object in Java is to provide a common interface for all objects in Java, regardless of their specific types. Because every class in Java is a subclass of Object, Object provides a universal set of methods and attributes that can be used on any object.

41
Q

What are some common methods and attributes of Object?

A

Some common methods and attributes of Object include toString(), hashCode(), equals(), getClass(), and wait().

42
Q

How is Object used in practice?

A

Object is used extensively in Java programming to write generic code that can work on any type of object. For example, you can create a method that takes an Object parameter and performs some action on it, without knowing the exact type of the object. This allows you to write more flexible and reusable code that can handle a wide range of objects.

43
Q

What is method overloading?

A

Method overloading is a feature in Java that allows us to define multiple methods with the same name but different parameters.

44
Q

What are the benefits of method overloading?

A

Method overloading can make code more flexible and easier to use, since it allows us to define methods that can accept different types of inputs without needing to come up with different method names for each case. This can make code more readable and easier to maintain.

45
Q

How does method overloading work?

A

When we call a method that has been overloaded, Java looks at the number and types of the arguments that are passed in and matches them to the closest matching method signature. If there is no exact match, Java tries to find the closest possible match by converting the arguments to match the method signature as closely as possible.

46
Q

What are some things to keep in mind when overloading methods?

A

When overloading methods, it’s important to make sure that the method signatures are distinct enough that Java can tell them apart. This usually means having different parameter types or numbers of parameters. It’s also important to make sure that the method behavior is consistent across all overloaded versions of the method, so that users can expect the same results regardless of which version they use.

47
Q

What is constructor overloading?

A

Constructor overloading is when we define multiple constructors for a class, each with different parameters.

48
Q

Why is constructor overloading useful?

A

Constructor overloading allows us to create objects of the same class with different initial values or configurations, depending on the parameters passed to the constructor.

49
Q

How is constructor overloading similar to using the “super” keyword?

A

Just like using “super”, constructor overloading allows us to reuse code from a superclass or from other constructors in the same class.

50
Q

How do you distinguish between different overloaded constructors?

A

The different constructors must have different parameter lists. This can be achieved by changing the number or types of parameters.

51
Q

Can constructors have different return types?

A

No, constructors do not have a return type, not even void.

52
Q

What is an abstract class?

A

An abstract class is a class that cannot be instantiated and is used to provide a common interface for its subclasses. It can contain both abstract and non-abstract methods.

53
Q

Why would you use an abstract class?

A

You would use an abstract class when you want to define a set of methods that subclasses must implement but don’t want to provide an implementation in the abstract class itself.

54
Q

What is an abstract method?

A

An abstract method is a method that is declared but not implemented in an abstract class. It has no body and ends with a semicolon instead of curly braces. Subclasses of the abstract class must provide an implementation of the abstract method.

55
Q

Can an abstract class be instantiated?

A

No, an abstract class cannot be instantiated because it is not complete. It must be extended by a subclass that provides an implementation for all its abstract methods.

56
Q

What is the difference between an abstract class and an interface?

A

An abstract class can have both abstract and non-abstract methods, while an interface can only have abstract methods. Also, a class can only extend one abstract class, but can implement multiple interfaces. Finally, an abstract class can have instance variables, while an interface cannot.

57
Q

What is an interface in Java?

A

An interface in Java is a way to enforce certain operations that a class must implement. It contains only abstract methods and does not have any data.

58
Q

How is an interface different from an abstract class?

A

An abstract class can contain both abstract and concrete methods, whereas an interface contains only abstract methods. An abstract class can have data members, but an interface cannot have any data members.

59
Q

How do you define an interface in Java?

A

To define an interface in Java, you use the interface keyword, followed by the name of the interface, and then a block of abstract method declarations.

60
Q

How do you implement an interface in Java?

A

To implement an interface in Java, you use the implements keyword, followed by the name of the interface, when you define a class. The class must then provide an implementation for all of the methods declared in the interface.

61
Q

Can a Java class implement multiple interfaces?

A

Yes, a Java class can implement multiple interfaces, but it can only inherit from one superclass.

62
Q

What is the purpose of an interface in Java?

A

The purpose of an interface in Java is to provide a way to enforce certain behavior without specifying the implementation. By defining an interface, you can create a contract that any class implementing that interface must follow. This can make your code more flexible and easier to maintain.

63
Q

Can an interface be instantiated?

A

No, an interface cannot be instantiated.

64
Q

Can an interface act as a reference type?

A

Yes, an interface can act as a reference type.

65
Q

Can an object be assigned to an interface variable?

A

Yes, an object can be assigned to an interface variable as long as the object implements the interface.

66
Q

What happens if an object is assigned to an interface variable that does not implement the interface?

A

If an object is assigned to an interface variable that does not implement the interface, a compile-time error will occur.

67
Q

Can an interface variable call methods that are defined in the interface?

A

Yes, an interface variable can call methods that are defined in the interface.

67
Q

Can an interface variable call methods that are defined in the interface?

A

Yes, an interface variable can call methods that are defined in the interface.

68
Q

Can an interface variable call methods that are implemented differently by different objects that implement the interface?

A

Yes, an interface variable can call methods that are implemented differently by different objects that implement the interface, as long as the methods are defined in the interface.

69
Q

Can an interface variable be used to access fields of an object that implements the interface?

A

No, an interface variable cannot be used to access fields of an object that implements the interface, since interfaces do not define fields.