OOP Flashcards
What are the pillars of OOP?
1) Encapsulation - is distilling a concept down to a discrete set of data and behaviors.
2) Inheritance - is a form of code reuse. When a type extends another type, it incorporates all non-private super-class fields and methods as members.
3) Polymorphism - When one or more subclasses override a super-class method or implement an interface, their behavior is said to by polymorphic. All types have the same method signature, but the outcome differs between types.
4) Abstraction - is the principle of revealing only the essential characteristics of a type.
What is the relationship between a class and object?
In Java, the definitions for object state and behavior are stored in a class. A class is a contract. It specifies the data that makes up each object instance. It defines the behaviors allowed.
In other words, a class is a template for an object. An object is an instance of a class.
Access modifiers (public, protected, private) support which pillar?
Encapsulation
What is inheritance? How does it work?
Inheritance is a code reuse strategy. Through inheritance, a child class gains access to a parent class’s fields and methods. It inherits a parent’s data and behavior. That means the code to create the data and behavior is only stored once, in the parent. When a child extends a parent, it reuses the parent’s code.
Describe a syntax or keyword that supports another pillar.
A class encapsulates code that solves a problem.
I’ve heard the phrase, “prefer composition over inheritance.” What does it mean? Is it right?
With composition, it’s easy to change behavior on the fly with Dependency Injection / Setters. Inheritance is more rigid as most languages do not allow you to derive from more than one type.
How do the access modifiers (public, protected, private) interact with inheritance?
How do I call a parent class’s constructor from a child class?
Through inheritcance.
Tell me about the keyword: this. What does it mean? What does it do?
The this keyword accesses class members. A member is any field or method declared inside the class code block.
What is the maximum number of classes you can extend (inherit)?
In Java, we can only extend a single class.
What is the dot operator?
The (.) operator is also known as member operator it is used to access the member of a package or a class.
Can one class both extend a parent and implement an interface?
What is the maximum number of interfaces you can implement?
As many as needed.
What is state? How does OOP manage it?
What is a constructor?
A constructor is a method-like subroutine that ensures an object is created in a valid state. It ensures an object has the data it needs to operate.
Constructors can call other constructors by using the this keyword: this(argument, anotherAgrument);