OOP Design Flashcards
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
How does encapsulation improve security in an application?
It prevents unauthorized access to variables, and only lets them be interacted in a way that it has been designed to.
What’s the difference between an abstract class and an interface? When would you use one over the other?
An abstract class can have both abstract and concrete methods, allowing partial implementation, whereas an interface only defines abstract methods. An abstract class should be used as a base class that needs to be inherited, interface should be used when you want to define a behavior that multiple unrelated classes can use.
Can an abstract class have a constructor? If yes, when is it called?
It can have a constructor, even though you can’t directly create an instance of that class. It is used when a concrete subclass is created
Explain why abstraction is important in software design patterns.
Removes unnecessary information. For users it hides implementation details, where the user only needs to focus on results not the process.
What’s the difference between is-a and has-a relationships in inheritance?
Is-a refers to a sub class that inherits a superclass, for example dog is an animal. Has-a is a reference to another class.
Can a child class override a final method in Java? Why or why not?
Final methods cannot be overridden.
What is multiple inheritance and why doesn’t Java support it directly? How does Java handle this limitation?
Multiple inheritance is a child class inheriting properties of many other classes. Java doesn’t support it as it leads to complexity and ambiguity (the diamond problem). It works around it via interfaces.
What is the difference between method overloading and method overriding?
Method overloading is multiple methods of the same name and but different signatures. Method overriding is the process of a subclass overriding its supper class method
How does Java achieve runtime polymorphism?
Method overloading
Can an interface extend another interface? If so, how?
Yes an interface can extend any number of interfaces.
What are default methods in Java interfaces, and why were they introduced?
Default methods allow you to add new functionality to existing and ensure binary compatibility. Allowing for new code to work for older versions.
Explain the Liskov Substitution Principle with a real-world analogy.
Objects of a superclass should be replaceable with objects of a subclass without breaking the functionality of a program. E.g. a paymentMethod superclass, and creditcardPayment or payPalPayment subclass. Wherever you code expects a PaymentMethod, it should be able to use either CreditCardPayment or PayPalPayment
How does dependency injection relate to OOP principles?
Dependency Injection helps enforce OOP principles by, promoting loose coupling between classes, making code more maintainable, and encouraging the use of abstractions and interfaces.
What is the difference between composition and inheritance? Which one would you prefer for re-usability?
Composition is where a class contains objects of other classes to reuse functionality. Inheritance is where class inherits behavior and properties. For re-usability composition is more suitable, as they can be swapped out or modified independently, and better flexibility, also avoids fragile hierarchies.