OOP Flashcards

1
Q

Object Oriented Programming

A

Object means a real-world entity such as a pen, chair, table, computer, watch, etc. It’s a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts

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

Object

A

Any entity that has state and behavior is known as an object. For example a chair, pen, table, keyboard, bike, etc. It can be physical or logical.

An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other’s data or code. The only necessary thing is the type of message accepted and the type of response returned by the objects.

Example: 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.

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

Class

A

A class, in the context of Java, are templates that are used to create objects, and to define object data types and methods. Core properties include the data types and methods that may be used by the object

A class can also be defined as a blueprint from which you can create an individual object. Class doesn’t consume any space.

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

Advantages of OOPs over Procedure Oriented Programming

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

Constructor

A

In Java, a constructor is a block of codes 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.

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

Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn’t have any.

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

Rules to remember while creating a constructor

A

There are three rules defined for the 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Types of Constructors

A
  • Default constructor

- parameterized constructor

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

Default Constructor

A

The default constructor is a no-args constructor that the 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.

Note : If there is no constructor in the class, the compiler adds a default constructor

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

No-Argument Constructor

A

public Bicycle() {

gear = 1;

cadence = 10;

speed = 0;

}

Bicycle yourBike = new Bicycle(); invokes the no-argument constructor to create a new Bicycle object called yourBike.

Note : The diagram below describes that a default constructor is added by the compiler when there is no constructor declared in the class.

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

Parameterized Constructor

A

A constructor which has a specific number of parameters is called a parameterized constructor.

The parameterized constructor is used to provide different values to the distinct objects. However, you can provide the same values also.

class Student4{

int id;  

String name;  

Student4(int i,String n){  

id = i;  

name = n;  

}  

void display(){System.out.println(id+" "+name);}  

public static void main(String args[]){  

Student4 s1 = new Student4(111,"Karan");  

Student4 s2 = new Student4(222,"Aryan");   

s1. display();  
s2. display();  

}

}

In the above example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.

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

Constructor Overloading

A

In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.

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.

class Student5{

int id;  

String name;  

int age;  

//creating two argument constructor  

Student5(int i,String n){  

id = i;  

name = n;  

}  

//creating three argument constructor  

Student5(int i,String n,int a){  

id = i;  

name = n;  

age=a;  

}  

void display(){System.out.println(id+" "+name+" "+age);}  

public static void main(String args[]){  

Student5 s1 = new Student5(111,"Karan");  

Student5 s2 = new Student5(222,"Aryan",25);  

s1. display();  
s2. display();  

}

}

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

Constructor vs 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
14
Q

Object Oriented Programming Pillars

A

Inheritance
Polymorphism
Encapsulation
Abstraction

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

Inheritance

A
  • Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly