Object-Oriented Programming Flashcards

1
Q

Object-Oriented Programming

A

a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:

Object
Classes
Inheritance
Polymorphism
Abstraction
Encapsulation

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

Object Oriented Programming Pillars

A

Inheritance
Polymorphism
Encapsulation
Abstraction

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

Advantages of Inheritance

A

For Method Overriding (so runtime polymorphism can be achieved).
For Code Reusability.

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

Class

A

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. Term used in inheritance.

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

Subclass/ child class

A

Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class. Term used in inheritance.

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

Super Class/Parent Class:

A

Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class. Term used in inheritance.

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

Reusability

A

As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class. Term used in inheritance.

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

Extends keyword

A

The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of “extends” is to increase the functionality.

17
Q

Example of Inheritance

A

As displayed in the above figure, Programmer is the subclass and Employee is the superclass. The relationship between the two classes is Programmer IS-A Employee. It means that Programmer is a type of Employee.

18
Q

Types Of Inheritance In Java

A

On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.

In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later.

19
Q

Why multiple inheritance is not supported in Java?

A

To reduce the complexity and simplify the language, multiple inheritance is not supported in java.

Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class.

Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error.

20
Q

Polymorphism

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.

21
Q

Method Overloading

A

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

If we have to perform only one operation, having same name of the methods increases the readability of the program.

Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs.

22
Q

Different ways to overload a method?

A

There are two ways to overload the method in java

By changing number of arguments
By changing the data type

23
Q

Method Overriding

A

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.
In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.

24
Q

Usage and Rules for Method Overriding

A

Usage :
Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
Method overriding is used for runtime polymorphism
Rules :
The method must have the same name as in the parent class
The method must have the same parameter as in the parent class.
There must be an IS-A relationship (inheritance).

25
Q

Can a static method be overridden?

A

No, a static method cannot be overridden. It can be proved by runtime polymorphism, so we will learn it later.

It is because the static method is bound with class whereas instance method is bound with an object. Static belongs to the class area, and an instance belongs to the heap area.

26
Q

Method Overloading vs Method Overriding

A

Method Overloading :

Method overloading is used to increase the readability of the program.
Method overloading is performed within class.
In case of method overloading, parameter must be different.
Method overloading is the example of compile time polymorphism.
In java, method overloading can’t be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.

Method Overriding :
Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
Method overriding occurs in two classes that have IS-A (inheritance) relationship.
In case of method overriding, parameter must be same.
Method overriding is the example of run time polymorphism.
Return type must be same or covariant in method overriding.

27
Q

Encapsulation

A

Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a capsule which is mixed of several medicines.
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.
The Java Bean class is the example of a fully encapsulated class.

28
Q

Advantages of Encapsulation

A

By providing only a setter or getter method, you can make the class read-only or write-only. In other words, you can skip the getter or setter methods.
It provides you the control over the data. Suppose you want to set the value of id which should be greater than 100 only, you can write the logic inside the setter method. You can write the logic not to store the negative numbers in the setter methods.
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.
The encapsulate class is easy to test. So, it is better for unit testing.

29
Q

Access Modifiers

A

here are two types of modifiers in java: access modifiers and non-access modifiers.
The access modifiers in java specifies accessibility (scope) of a data member, method, constructor or class.
There are 4 types of java access modifiers:

private
default
protected
public
There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc. Here, we will learn access modifiers.

30
Q

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.

31
Q

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.

Eg : The classes Piece of Luggage and Piece of Cargo partially share the same attributes. From a domain perspective, the two classes are also very similar. During generalization, the shared characteristics are combined and used to create a new superclass Freight . Piece of Luggage and Piece of Cargo become subclasses of the class Freight. Therefore the properties that are common to the classes Piece of Luggage and Piece of Cargo are placed in the superclass Freight - Identification, Weight and ID-Number are those properties.

32
Q

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.

In the previous example for Generalization, we saw that the classes Piece of Luggage and Piece of Cargo shared similar properties that were placed in a superclass called Freight. When it comes to Specialization, if there is a property that is only applicable to a specific subclass, such as Degree of Hazardousness, that property is placed in the class Piece of Cargo where-in this class also has all the properties of the Freight class with the concept of Generalization.

33
Q

Ways to achieve abstraction?

A

There are two ways to achieve abstraction in java

Abstract class (0 to 100%)
Interface (100%)

34
Q

Abstract Class

A

A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.

Some points to remember :
An abstract class must be declared with an abstract keyword.
It can have abstract and non-abstract methods.
It cannot be instantiated.
It can have constructors and static methods also.
It can have final methods which will force the subclass not to change the body of the method.

35
Q

Abstract Method

A

A method which is declared as abstract and does not have implementation is known as an abstract method.

[abstract void printStatus(); //no method body and abstract ]