Revature prep OOP Flashcards
What is OOP
Object Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance.
What is an Object
An Object is an entity that has state and behavior, it can be defined as an instance of a class. Objects contain addresses and take up some space in memory. Objects can communicate without knowing the details of each others data or the code. The only necessary thing is the type of message accepted and the type of response returned by the objects.
What is a Class
Classes are templates or blueprints that are used to create objects and to define object data types and methods. They do not consume space.
Advantages of OOP vs Procedure oriented programming
OOP makes development and maintenance easier whereas POP is not easy to manage if the code grows as project size increases.
OOP provides data hiding whereas POP the global data can be accessed from anywhere.
What is a Constructor
A constructor is a block of code similar to the method. It is called when an instance of the object is created, and memory is allocated for the object. It is a special type of method which is used to initialize the object.
When is a constructor called
When an object is created, compiler makes sure that constructors for all of its sub objects are called. if members have default constructors or constructors without parameters then these constructors are called automatically, otherwise parameterized constructors can be called using initializer list.
Why is it called constructor
It is called constructor because it constructs the values at the time of the object creation. It is not necessary to write a constructor for a class. It is because compilers create a default constructor if your class doesn’t have any.
What are the 3 rules for constructors
- The constructor name must be the same as the class name.
- The constructor must have no explicit return type.
- A Java constructor cannot be abstract, static, final and synchronized.
What are the 2 types of constructors
Default and Parameterized.
What is a default constructor
The default constructor is a no-args constructor that Java compiler inserts on your behalf, it contains a default call to super, which is the default behavior. If you implement any constructor then you no longer receive a default constructor.
What is a parameterized constructor
A constructor which has a specific number of parameters. The parameterized constructor is used to provide different values to the distinct objects. However, you can provide the same values also.
What is constructor overloading
constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types.
Constructor vs Method
Constructor: used to initialize the state of an object, must not have a return type, invoked implicitly, provides default constructor if none in class, name needs to be same as class name.
Method: used to expose the behavior of an object, must have a return type, invoked explicitly, not provided by the compiler, can be same or different name from class name.
What are the OOP pillars
Inheritance, Polymorphism, Encapsulation and Abstraction
What is inheritance
Inheritance is a mechanism in which one object acquires all the properties and behaviors of a parent object. Inheritance represents the IS-A relationship which is also known as a parent-child relationship.