OOP Flashcards

1
Q

What is a Java Object?

A

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”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Java Class?

A

A class can also be defined as a blueprint or template tused to create objects, and to define object data types and methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the advantages of OOP over Procedure Oriented Programming?

A
  1. 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.
  2. OOPs provides data hiding whereas in a procedure-oriented programming language a global data can be accessed from anywhere.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a Constructor?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When is a Constructor called?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the three rules defined for a Constructor?

A
  1. Constructor name must be the same as its class name
  2. A Constructor must have no explicit return type
  3. A Java constructor cannot be abstract, static, final, and synchronized
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Differences between Constructor and Method?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the 4 pillars of OOP?

A

Inheritance
Encapsulation
Polymorphism
Abstraction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Inheritance? Explain and give an example?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Polymorphism? Explain and give an example?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is method overloading?

A

If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the different ways to overload a method?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the differences between method overloading and overriding?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Encapsulation?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the advantages of Encapsulation?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the 4 types of java access modifiers?

A

Private
Default
Protected
Public

17
Q

What is Abstraction?

A

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don’t know the internal processing about the message delivery.

Abstraction lets you focus on what the object does instead of how it does it.

18
Q

What is Generalization?

A

Generalization is the process of extracting shared characteristics from two or more classes, and combining them into a generalized superclass. Shared characteristics can be attributes, associations, or methods.

19
Q

What is Specialization?

A

Specialization means creating new subclasses from an existing class. If it turns out that certain attributes, associations, or methods only apply to some of the objects of the class, a subclass can be created.

20
Q

What are the two ways to achieve Abstraction?

A
Abstract class
Interface