OOP Design Flashcards

(38 cards)

1
Q

What is inheritance in object-oriented programming?

A

The process of a class inheriting attributes and methods from a base class to a derived class

Inheritance establishes ‘is a’ relationships among classes.

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

What is a Superclass?

A

Class whose properties (methods and variables) are inherited by another class

Also known as a parent class.

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

What is a Subclass?

A

Inherits from the superclass using the extends keyword

It can use or override the parent’s methods.

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

What does the super keyword refer to?

A

Refers to the immediate parent class object

Used to call the parent’s constructor or methods.

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

What are the benefits of inheritance?

A

Code reusability, allows sharing common functionality among related classes

Inheritance promotes a hierarchical class structure.

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

Define encapsulation.

A

Process of hiding ‘sensitive data’ from users via access modifiers

Uses public methods to alter or view data instead of direct manipulation.

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

What is the access level of private fields?

A

Only accessible within the same class

This access level enhances security.

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

What is the access level of protected fields?

A

Accessible within the same package and subclasses

This allows controlled access for derived classes.

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

What is the access level of public fields?

A

Accessible from any other class

Public fields can be accessed directly from outside the class.

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

What does the default access modifier mean?

A

Accessible only within the same package

Default access is less restrictive than private but more than protected.

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

Why should fields be private?

A

Improves security and structure/modularity

Hiding fields prevents unauthorized access.

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

What are the benefits of using getters and setters?

A

Improves security and structure/modularity

Allows controlled access to private fields.

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

What are the benefits of encapsulation?

A
  • 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.

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

Define abstraction.

A

Hiding unnecessary information and only displaying necessary information

This simplifies the interface for the user.

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

What is an abstract class?

A

Cannot be instantiated directly, can have abstract methods and regular methods

Abstract methods do not have an implementation.

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

What are interfaces in programming?

A

Defines a contract that implementing classes must follow

Interfaces allow for a consistent method signature across different classes.

17
Q

What are the benefits of abstraction?

A
  • Hides implementation details
  • Focus on result not process

Users interact through abstract interfaces.

18
Q

Define polymorphism.

A

One interface, many implementations

This allows different classes to be treated as instances of the same class through a common interface.

19
Q

What is Compile-Time Polymorphism?

A

Achieved through method overloading, where multiple methods share the same name but have different signatures

Also known as static binding.

20
Q

What is Run-Time Polymorphism?

A

Achieved through method overriding, where a subclass provides its own implementation of a method defined in the parent class

Also known as dynamic binding.

21
Q

What are the overriding rules?

A
  • 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.

22
Q

What is the instanceof operator used for?

A

Used to verify whether an object is an instance of a particular class or interface

This operator helps in type checking at runtime.

23
Q

What are the advantages of using encapsulation in a class?

A

Improves security and modularity, only allows a class to be interacted the way it is designed to be.

24
Q

Why should class variables typically be marked as private?

A

To prevent unauthorized access or changing

25
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.
26
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.
27
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
28
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.
29
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.
30
Can a child class override a final method in Java? Why or why not?
Final methods cannot be overridden.
31
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.
32
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
33
How does Java achieve runtime polymorphism?
Method overloading
34
Can an interface extend another interface? If so, how?
Yes an interface can extend any number of interfaces.
35
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.
36
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
37
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.
38
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.