OOP Flashcards
What are the 4 pillars of OOP?
Abstractions, Polymorphism, Inheritance, Encapsulation
What is Abstraction?
Abstraction is a programming principle in which we centralize common characteristics and generalize Through abstraction, we hide underlying complexity through a simplified interface.
Think of a car - you do not need to know how the car works, just how to use the accelerator, brake, and steering wheel. Thus, a car “abstracts” away the internal details of the engine, motor, driveshaft, and other parts.
In Java, we achieve abstraction through abstract classes and interfaces.
Abstract classes are more general classes which cannot be instantiated. They instead act as templates for other classes to extend from. Abstract classes can have both concrete and abstract methods - the abstract methods must be implemented by concrete subclasses. Interfaces also cannot be instantiated. They instead serve as contracts for methods that classes must implement. In order to inherit from interfaces, a class declares that it implements some interface, or multiple interfaces. Methods declared on an interface are implicitly public and abstract. Interfaces can have variables, but they are implicitly public, static, and final. Since Java 8, interfaces can also provide method implementations if the method is marked static or default. Abstract classes are better suited for defining common characteristics of objects and are named as nouns by convention, whereas interfaces are better for defining common behavior the implementing class promises to provide.
What is Polymorphism
Polymorphism describes how objects can behave differently in different contexts. The most common examples of polymorphism in Java are method overloading and overriding.
What is Inheritance
In Java (and other OOP languages), classes contain a blueprint for the state and behavior of objects. Most languages have a method whereby classes (and thus, objects) can inherit the state and behavior (read: fields and methods) of other classes. The class from which other classes inherit from is called a “base” or “parent” class, and the class which inherit the parent is called a “child” or “sub”-class.
What is Encapsulation
Encapsulation is the OOP principle of containing related state and behavior together inside a class, and also hiding and preventing change to an object’s data members. When an object is encapsulated, it controls the access to its internal state. This prevents arbitrary external interference, which could bring the object into an invalid or inconsistent state.
What are the principles of SOLID
Single Responsibility - every class should have a single responsibility
Open-closed - open for extension, but closed for modification.
Liskov Substitution - replaceable with instances of their subtypes without altering the correctness of the program.
Interface Segragation - many client-specific interfaces are better than one general-purpose interface.
Dependency Inversion - Abstractions should not depend upon details;
Details should depend upon abstractions.
use the same level of abstraction at a given level.
What is the difference between an abstract class and an interface?
Conceptually, interfaces define behaviors and abstract classes are for concepts and inheritance.
You can implement multiple interfaces, but you can extend only one class.
Can abstract methods have concrete methods? Can concrete (non-abstract) classes have abstract methods?
Abstract classes can have concrete methods
Concrete classes cannot have abstract methods
Can static methods access instance variables? Can non-static methods access static variables?
No, static methods can access instance variables, ?
Yes, non-static methods can access static variables