OOP Flashcards
What are the 4 pillars of OOP?
Abstraction, Polymorphism, Inheritance, Encapsulation
Abstraction
The process of hiding complex processes behind a simple user interface. This can be seen with the usage of the Abstract keyword, which is a non-access modifer, used for classes and methods. Abstract classes are restricted classes that cannot be used to instantiate objects. Abstract methods do not have a body and can only be used in abstract classes.
Polymorphism
Means many forms and extends from inheritance. Refers to the ability for for parts of code to be reused and modified based on the context of its usage. Examples are Method overloading and method overriding.
Inheritance
Refers to the adaptation of variables and methods from the parent class to the child classes. The child classes can then change the values of the variables and methods for its own specific usage. Multiple inheritance is not supported in Java.
Encapsulation
The wrapping of blocks of code into a single unit and then selectively giving or restricting access to that code through the use of access modifiers. Is also seen with the usage of getter and setter methods which further protects your code from being altered.
What is the difference between an abstract class and an interface?
You cannot instantiate an abstract class, and forces programmers to instantiate its child classes. While an interface is more like a contract and declares behaviors that other classes must implement, or that class itself must be abstract. In an interface methods are implicitly abstract.
Can abstract classes have concrete methods? Can concrete (non-abstract) classes have abstract methods?
An abstract class can only have abstract methods. A concrete class can have abstract methods.
Can static methods access instance variables? Can non-static methods access static variables?
No a static method cannot access an instance variable. Yes non-static methods can access static variables. Static methods are methods that belong to a class rather than an instance of a class
What are the implicit modifiers for interface variables? Methods?
Interface methods are implicitly abstract and public. All instance variables are implicitly constant, static and final.
What is the difference between method overloading and overriding? What are the rules for changing the method signature of overloaded methods?
Method overloading is when methods have the same name but different types and/or number of parameters. Method overriding has the same name and same parameters but occurs when a child class inherits a method from a public class and then changes the implementation of that method for its own use.
What are covariant return types? What rules apply to return types for overridden methods?
Covariant return types are the return types of overriden methods. It allows the program to narrow down the return type of an overridden method without any need to cast the type or check the return type.
When do you see extends or implements keywords?
Extends is used for classes. Implements is used for interfaces.