Java OOPs Flashcards

1
Q

What is OOPs?

A

OOPs (Object Oriented Programming) is a type of programming which is based on objects rather than just functions and procedures.

Individual objects are grouped into classes

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

What are the main features of OOPs?

A
  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an object?

A

An object is a unit of OOPs and is an instance of a class

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

What is a class?

A

A class is the blueprint for an object in Java, it defines the objects behavior/state

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

What is Inheritance?

A

Inheritance is the process in which the Child class acquires the properties of a Parent class using the ‘extends’ keyword

Advantage: Code reusability

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

Types of Inheritance

A

Single Inheritance: One Child class extends one Parent class

Multilevel Inheritance: Class A extends Class B and Class B extends Class C

Hierarchical Inheritance: Many child classes extends one Parent class

Hybrid Inheritance: Combination of Single and Multilevel Inheritance

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

Why is multiple inheritance not supported in Java?

A

If one Child class extends 2 Parent classes and both parent classes contain the same method, then the Child class would run into problems when it inherits them

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

What is Encapsulation?

A

Encapsulation is the process of binding data (Variables) and code (methods) into a single unit

Advantage: Security

Ex: Java Class (methods + variables) = capsule

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

How do you achieve Encapsulation?

A

Encapsulation can be achieved by:
- Creating PRIVATE variables in a class
- Providing Getter and Setter methods to make variables only accessible through those methods

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

What is Abstraction?

A

Abstraction is the process of hiding the implementation and providing only the necessary data at high level to the users

Advantage: Security

Ex: ATM machine (only the service is visible, not the implementation)

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

How do you achieve Abstraction in Java?

A

Abstract Class (Partial Abstraction)
Interface (Pure Abstraction)

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

What is an Abstract Class?

A

A class is made abstract when only partial implementation of the class is known

It can have abstract and non abstract methods, constructors and static methods

It can not be instantiated

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

What is an Interface?

A

It is a completely abstract class that is used to group related methods with empty bodies. uses the “implements” keyword in a class, which must give the empty methods their bodies

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

What are the differences between Abstract Class and Interface?

A

Abstract Class
- Abstract classes can have abstract and non abstract methods
- Does not support multiple Inheritance
- Uses the keyword ‘extends’
- Ex: public abstract class Shape{public abstract void draw();};

Inheritance
- Inheritance can only have abstract methods
- Supports multiple Inheritance
- Uses the keyword ‘implements’
- Ex: public interface Drawable{void draw();};

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

What is Polymorphism?

A

Polymorphism is the process in which one object is used in many forms

It occurs when we have classes that are related to each other by inheritance

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

What are the types of Polymorphism?

A

Static / Compile Time Polymorphism - Method Overloading

Dynamic / Run Time Polymorphism - Method Overriding

17
Q

What is Method Overloading?

A

Same method name, but different method arguments

Advantage: Makes code simpler

Ex: void add(int a, int b); void add(int a, int b, int c);

18
Q

What is Method Overriding?

A

Same method, same arguments. It is used in Parent-Child relationship/inheritance classes

Advantage: Code reusability

19
Q

Can we overload a static method in Java?

A

Yes, you can overload a static method in Java as they are with different method signatures, this no problem will occur

20
Q

Can we override a static method in Java?

A

No, you cannot override a static method because static methods belong to a class and resolved at compile time using the type of reference variable

21
Q

Can we override a private method in Java?

A

No, since the private method is only accessible and visible inside the class they are declared

22
Q

What are the rules for method overriding in Java?

A

If the Parent class throws checked exceptions, then the Child class should throw that exception or its subtype exception

But no restriction for unchecked exceptions as both classes are independent

23
Q

Is Java 100% Object Oriented?

A

No, because primitive data types are not considered objects, the same with static classes, as they can be used without an object

24
Q

What is the difference between String, StringBuilder, and StringBuffer?

A
  • A string is an object representation of char values which is immutable, internally, strings are a special type of char array
  • StringBuilder and StringBuffer both create mutable strings objects, but the difference between the two is that StringBuffer is thread safe, whereas StringBuilder is not
25
Q

Why are Strings immutable?

A

Strings are immutable because string objects are cached in the string pool and since all the strings in the pool are shared between multiple clients, there is a risk that a change would effect other clients

26
Q

Does finally always execute in Java?

A

Not is System.exit() is called or the system crashes

27
Q

What methods does the Object Class have?

A

All classes are rooted in the Object class, so all have access to
- protected Object clone() throws CloneNotSupportedException
Creates and returns a copy of this object
- public boolean equals(Object obj)
Indicates whether some other object is “equal to” this one
- protected void finalize() throws Throwable
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object
- public final Class getClass()
Returns the runtime class of an object
- public int hashCode()
Returns a hash code value for the object
- public String toString()
Returns a string representation of the object

28
Q

How can you make your class immutable?

A
  • Declare the class as final so it cant be extended
  • Make all the fields private so that direct access is not allowed
  • Dont provide setter methods for variables
  • Make all mutable fields final so that its value can be assigned only once
  • Initialize all the fields via a constructor performing a deep copy
  • Perform cloning of objects in the getter methods to return a copy rather than returning the actual object reference
29
Q

What is a singleton class in Java and how do we make a class singleton?

A

A Singleton class is a class where only one instance can be created at any time in one JVM

Ex:
public class Animal {
private static Animal single_instance = null;
private Animal(){};
public static Animal.getInstance(){
if(single_instance == null)
single_instance = new Animal();
return single_instance;
}
}