OOP Flashcards
What is a Java Object?
Any entity that has state and behavior. An Object can be defined as an instance of a class. The state of an object is stored in fields (variables), while methods (functions) display the object’s behavior. Objects are created at runtime from templates, which are also known as classes. An object contains an address and takes up some space in memory. A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc. an object is created using the keyword “new”.
What is a Java Class?
A class can also be defined as a blueprint or template tused to create objects, and to define object data types and methods.
What are the advantages of OOP over Procedure Oriented Programming?
- OOPs makes development and maintenance easier whereas in a procedure-oriented programming language it is not easy to manage if code grows as project size increases.
- OOPs provides data hiding whereas in a procedure-oriented programming language a global data can be accessed from anywhere.
What is a Constructor?
A special method of a class that initializes a newly created object of that type. The sole purpose of the constructor is to initialize the data fields of objects in the class.
When is a Constructor called?
When an object is created, compiler makes sure that constructors for all of its subobjects (its member and inherited objects) are called. If members have default constructors or constructor without parameter then these constructors are called automatically, otherwise parameterized constructors can be called using initializer list.
What are the three rules defined for a Constructor?
- Constructor name must be the same as its class name
- A Constructor must have no explicit return type
- A Java constructor cannot be abstract, static, final, and synchronized
Differences between Constructor and Method?
Constructor :
A constructor is used to initialize the state of an object. A constructor must not have a return type. The constructor is invoked implicitly. The Java compiler provides a default constructor if you don't have any constructor in a class. The constructor name must be same as the class name.
Method :
A method is used to expose the behavior of an object. A method must have a return type. The method is invoked explicitly. The method is not provided by the compiler in any case. The method name may or may not be same as class name.
What are the 4 pillars of OOP?
Inheritance
Encapsulation
Polymorphism
Abstraction
What is Inheritance? Explain and give an example?
Inheritance is which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also. Inheritance represents the IS-A relationship which is also known as a parent-child relationship. An example would be having a class called mammal that has a method eat. Then creating a class dog that extends mammal and now dog class has acquired the method eat.
What is Polymorphism? Explain and give an example?
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
What is method overloading?
If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.
What are the different ways to overload a method?
By changing number of arguments
By changing the data type
For example you can have two methods with same name different parameters of two methods same name one of int and one of double.
What are the differences between method overloading and overriding?
One difference is that overloading happens at compile time while overriding happens at run-time.
Overloading increases readability while overriding is used to provide specific implementation of the method.
Overloading is performed within the class while overriding is performed in two classes that have a inheritance relationship
Overloading must have different parameters while overriding while overriding return types must be the same or covariant.
What is Encapsulation?
Encapsulation refers to bundling data and the methods that operate on that data into a single unit. We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.
What are the advantages of Encapsulation?
By providing only a setter or getter method, you can make the class read-only or write-only. It provides you the control over the data. It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members.