Topic 7.1: Working with Inheritance - Describe inheritance and its benefits Flashcards

1
Q

The requirements for this are that the subclass method must have the same method signature (name, return type, and parameter types) as the method in the superclass.

A

What are the requirements for method overriding?

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

Are private methods in the superclass inherited by the subclass?

A

No, private methods in the superclass are not inherited and cannot be accessed directly by the subclass.

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

What does a subclass inherit from its superclass in Java?

A

This inherits all the accessible fields and methods (public and protected) from its superclass.

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

How can a subclass access private fields of its superclass?

A

A subclass can access these through public or protected methods of the superclass (if available).

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

A subclass can access these through public or protected methods of the superclass (if available).

A

How can a subclass access private fields of its superclass?

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

What is method overriding in Java?

A

This allows subclasses to provide their own implementation for a method defined in the superclass, thereby overriding the original behavior.

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

What are the requirements for method overriding?

A

The requirements for this are that the subclass method must have the same method signature (name, return type, and parameter types) as the method in the superclass.

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

What is one of the benefits of inheritance in Java related to code organization and reuse?

A

Code Reusability - Inheritance allows the reuse of code from the superclass in the subclass, reducing duplication and promoting a modular approach.

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

How does inheritance contribute to organizing classes in Java?

A

Hierarchical Structure - Inheritance allows the creation of class hierarchies, organizing classes based on their relationships.

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

This inherits all the accessible fields and methods (public and protected) from its superclass.

A

What does a subclass inherit from its superclass in Java?

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

How can a subclass use private methods of its superclass?

A

A subclass can use public or protected methods of the superclass to indirectly access and invoke the private methods of the superclass.

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

What is inheritance

A

This is a fundamental concept in object-oriented programming where a class (subclass) can inherit properties (fields and methods) from another class (superclass or parent class).

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

How do you declare a subclass in Java?

A

To create a subclass, use the “extends” keyword in the class declaration, followed by the name of the superclass. For example:

class SubClass extends SuperClass 
{ ... }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does a subclass handle private fields of its superclass?

A

A subclass cannot directly access private fields of its superclass, but it can use getter and setter methods to interact with these fields indirectly.

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

What is the purpose of the @Override annotation in method overriding?

A

The @Override annotation (optional but recommended) is used to indicate that a method in the subclass is intended to override a method from the superclass. It helps catch potential issues during compilation and improves code readability.

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

How does a subclass access inherited methods from its superclass?

A

A subclass can directly access public and protected methods from its superclass using their simple names.

17
Q

How does inheritance support polymorphism in Java?

A

Polymorphism in Java, enabled through method overriding, allows a common type (the superclass) to represent any of its subtypes, including its own type. It means that objects of different classes can be treated uniformly based on their shared superclass. This provides flexibility and extensibility in the code, as the same method call can produce different behavior depending on the actual object type at runtime.

18
Q

Can a subclass access private fields from its superclass directly?

A

No, private fields in the superclass are not inherited and cannot be accessed directly by the subclass.

19
Q

Can a subclass access package-private methods of its superclass?

A

Yes, if the subclass and superclass are in the same package, the subclass can access package-private (default access) methods from the superclass.

20
Q

This allows subclasses to provide their own implementation for a method defined in the superclass, thereby overriding the original behavior.

A

What is method overriding in Java?

21
Q

What happens if a subclass and its superclass are in different packages, and the superclass has package-private methods?

A

In such cases, the subclass will not be able to access the package-private methods of the superclass, as they are not inherited.

22
Q

This is a fundamental concept in object-oriented programming where a class (subclass) can inherit properties (fields and methods) from another class (superclass or parent class).

A

What is inheritance