OOP Design Flashcards
(38 cards)
What is inheritance in object-oriented programming?
The process of a class inheriting attributes and methods from a base class to a derived class
Inheritance establishes ‘is a’ relationships among classes.
What is a Superclass?
Class whose properties (methods and variables) are inherited by another class
Also known as a parent class.
What is a Subclass?
Inherits from the superclass using the extends keyword
It can use or override the parent’s methods.
What does the super keyword refer to?
Refers to the immediate parent class object
Used to call the parent’s constructor or methods.
What are the benefits of inheritance?
Code reusability, allows sharing common functionality among related classes
Inheritance promotes a hierarchical class structure.
Define encapsulation.
Process of hiding ‘sensitive data’ from users via access modifiers
Uses public methods to alter or view data instead of direct manipulation.
What is the access level of private fields?
Only accessible within the same class
This access level enhances security.
What is the access level of protected fields?
Accessible within the same package and subclasses
This allows controlled access for derived classes.
What is the access level of public fields?
Accessible from any other class
Public fields can be accessed directly from outside the class.
What does the default access modifier mean?
Accessible only within the same package
Default access is less restrictive than private but more than protected.
Why should fields be private?
Improves security and structure/modularity
Hiding fields prevents unauthorized access.
What are the benefits of using getters and setters?
Improves security and structure/modularity
Allows controlled access to private fields.
What are the benefits of encapsulation?
- Data hiding: prevents direct access to internal data
- Improved maintainability: changes to internal implementation won’t affect external code
As long as the interface remains the same.
Define abstraction.
Hiding unnecessary information and only displaying necessary information
This simplifies the interface for the user.
What is an abstract class?
Cannot be instantiated directly, can have abstract methods and regular methods
Abstract methods do not have an implementation.
What are interfaces in programming?
Defines a contract that implementing classes must follow
Interfaces allow for a consistent method signature across different classes.
What are the benefits of abstraction?
- Hides implementation details
- Focus on result not process
Users interact through abstract interfaces.
Define polymorphism.
One interface, many implementations
This allows different classes to be treated as instances of the same class through a common interface.
What is Compile-Time Polymorphism?
Achieved through method overloading, where multiple methods share the same name but have different signatures
Also known as static binding.
What is Run-Time Polymorphism?
Achieved through method overriding, where a subclass provides its own implementation of a method defined in the parent class
Also known as dynamic binding.
What are the overriding rules?
- Must have the same method signature
- The overridden method cannot have a more restrictive access modifier
- Can call the parent’s overridden method using super
These rules ensure proper method overriding.
What is the instanceof operator used for?
Used to verify whether an object is an instance of a particular class or interface
This operator helps in type checking at runtime.
What are the advantages of using encapsulation in a class?
Improves security and modularity, only allows a class to be interacted the way it is designed to be.
Why should class variables typically be marked as private?
To prevent unauthorized access or changing