All Flashcards
What is the primary benefit of using inheritance?
Code reusability. Subclasses inherit methods and instance variables from superclasses, reducing code duplication.
Why is inheritance considered a pillar of object-oriented programming?
Because it promotes abstraction and code organization by establishing hierarchical relationships between classes.
How does inheritance relate to the 'is-a' relationship?
A subclass 'is-a' more specific type of its superclass (e.g., 'APSubject is-a SchoolSubject').
What is the restriction on the number of superclasses a Java class can inherit from?
A Java class can only inherit from one superclass (single inheritance).
What is the role of polymorphism in OOP?
It allows objects of different classes to be treated as objects of a common type (usually a superclass), enabling flexibility and code extensibility.
How does method overriding contribute to polymorphism?
It allows subclasses to provide their own specific implementations of methods defined in the superclass, enabling different behaviors for the same method call.
Explain the concept of code reusability in inheritance.
Subclasses inherit the properties and methods of their superclasses, avoiding the need to rewrite the same code in multiple classes.
What is the significance of the Object class in Java's inheritance hierarchy?
It is the root of the class hierarchy. Every class in Java directly or indirectly inherits from the Object class.
What is the purpose of creating subclasses?
To create more specialized classes that inherit and extend the functionality of a more general superclass.
What is the difference between inheritance and composition?
Inheritance is an 'is-a' relationship, while composition is a 'has-a' relationship. Inheritance creates a tight coupling between classes, while composition promotes loose coupling.
What are the differences between a superclass and a subclass?
Superclass: More general, provides base functionality | Subclass: More specific, inherits from superclass and adds/modifies functionality.
What are the differences between inheritance and composition?
Inheritance: 'is-a' relationship, tight coupling | Composition: 'has-a' relationship, loose coupling.
What are the differences between method overriding and method overloading?
Overriding: Same method signature in subclass and superclass | Overloading: Same method name but different parameters in the same class.
What are the differences between single inheritance and multiple inheritance?
Single Inheritance: A class inherits from only one superclass | Multiple Inheritance: A class inherits from multiple superclasses (not supported in Java directly).
What are the differences between static and dynamic typing?
Static typing: Type checking at compile time | Dynamic typing: Type checking at runtime.
What are the differences between public and private members in the context of inheritance?
Public: Accessible from anywhere | Private: Only accessible within the class where they are defined, not inherited directly.
What are the differences between using 'extends' and 'implements' keywords?
'extends': Used for inheritance between classes | 'implements': Used for a class to implement an interface.
What are the differences between an abstract class and an interface?
Abstract Class: Can have method implementations and instance variables | Interface: Only method signatures (before Java 8) and constant variables.
What are the differences between inheritance and aggregation?
Inheritance: Creates a strong 'is-a' relationship, subclass depends on superclass | Aggregation: Creates a 'has-a' relationship, weaker dependency.
What are the differences between using inheritance and creating separate, unrelated classes?
Inheritance: Promotes code reuse, establishes hierarchical relationships | Separate Classes: No inherent relationship, can lead to code duplication.
How is inheritance applied in GUI frameworks?
GUI components (e.g., buttons, text fields) are often created as subclasses of a base component class, inheriting common properties and behaviors.
How is inheritance used in creating different types of bank accounts?
A base 'Account' class can be a superclass for 'SavingsAccount' and 'CheckingAccount', inheriting common account properties and methods.
How can inheritance be applied in a game development context?
A base 'GameObject' class can be a superclass for different game entities like 'Player', 'Enemy', and 'Item', sharing common properties like position and collision detection.
How is inheritance applied in creating different types of employees in a company?
A base 'Employee' class can be a superclass for 'Manager', 'Developer', and 'SalesPerson', inheriting common employee attributes and methods.
How is inheritance used in representing different types of vehicles?
A base 'Vehicle' class can be a superclass for 'Car', 'Truck', and 'Motorcycle', inheriting common vehicle properties and methods.
How is inheritance applied in creating different shapes in a drawing application?
A base 'Shape' class can be a superclass for 'Circle', 'Rectangle', and 'Triangle', inheriting common properties like color and position.
How is inheritance used in representing different types of media files?
A base 'MediaFile' class can be a superclass for 'AudioFile', 'VideoFile', and 'ImageFile', inheriting common properties like file size and format.
How is inheritance applied in creating different types of users in a social media platform?
A base 'User' class can be a superclass for 'Admin', 'Moderator', and 'RegularUser', inheriting common user attributes and methods.
How is inheritance used in representing different types of geometric figures?
A base 'Figure' class can be a superclass for 'Square', 'Circle', and 'Triangle', inheriting common properties like area and perimeter calculations.
How is inheritance applied in creating different types of network connections?
A base 'Connection' class can be a superclass for 'WifiConnection', 'BluetoothConnection', and 'EthernetConnection', inheriting common connection properties and methods.