OOP Concepts in Java Flashcards

1
Q

What is method overloading?

A

a feature that allows a class to have two or more methods having same name, if their argument lists are different. In the last tutorial we discussed constructor overloading that allows a class to have more than one constructors having different argument lists.

Argument lists could differ in –

  1. Number of parameters.
  2. Data type of parameters.
  3. Sequence of Data type of parameters.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is method overriding?

A

Declaring a method in subclass which is already present in parent class.

Example:
One of the simplest example – Here Boy class extends Human class. Both the classes have a common method void eat(). Boy class is giving its own implementation to the eat() method or in other words it is overriding the method eat().
class Human{
   public void eat()
   {
      System.out.println("Human is eating");
   }
}
class Boy extends Human{
   public void eat(){
      System.out.println("Boy is eating");
   }
   public static void main( String args[]) {
      Boy obj = new Boy();
      obj.eat();
   }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Differences between overloading and overriding?

A
  • Overloading happens at compile-time while Overriding happens at runtime: The binding of overloaded method call to its definition has happens at compile-time however binding of overridden method call to its definition happens at runtime.
  • Static methods can be overloaded which means a class can have more than one static method of same name. Static methods cannot be overridden, even if you declare a same static method in child class it has nothing to do with the same method of parent class.
  • The most basic difference is that overloading is being done in the same class while for overriding base and child classes are required. Overriding is all about giving a specific implementation to the inherited method of parent class.
  • Static binding is being used for overloaded methods and dynamic binding is being used for overridden/overriding methods.
  • Performance: Overloading gives better performance compared to overriding. The reason is that the binding of overridden methods is being done at runtime.
  • private and final methods can be overloaded but they cannot be overridden. It means a class can have more than one private/final methods of same name but a child class cannot override the private/final methods of their base class.
  • Return type of method does not matter in case of method overloading, it can be same or different. However in case of method overriding the overriding method can have more specific return type (refer this).
  • Argument list should be different while doing method overloading. Argument list should be same in method Overriding.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is association in Java?

A

Association establish relationship between two classes through their objects. The relationship can be one to one, One to many, many to one and many to many.

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

What is aggregation in Java?

A

Aggregation is a special form of association. It is also a relationship between two classes like association, however its a directional association, which means it is strictly a one way association. It represents a Has-A relationship.

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

Difference between Association, Aggregation and Composition?

A

Association is a relationship between two separate classes which can be of any type say one to one, one to may etc. It joins two entirely separate entities.

Aggregation is a special form of association which is a unidirectional one way relationship between classes (or entities), for e.g. Wallet and Money classes. Wallet has Money but money doesn’t need to have Wallet necessarily so its a one directional relationship. In this relationship both the entries can survive if other one ends. In our example if Wallet class is not present, it does not mean that the Money class cannot exist.

Composition is a restricted form of Aggregation in which two entities (or you can say classes) are highly dependent on each other. For e.g. Human and Heart. A human needs heart to live and a heart needs a Human body to survive. In other words when the classes (entities) are dependent on each other and their life span are same (if one dies then another one too) then its a composition. Heart class has no sense if Human class is not present.

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

What are the principles of OOP?

A

Encapsulation, Inheritance, and Polymorphism.

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

What is encapsulation?

A

Encapsulation is:

  • Binding the data with the code that manipulates it.
  • It keeps the data and the code safe from external interference.

Encapsulated code should have following characteristics:

  • Everyone knows how to access it.
  • Can be easily used regardless of implementation details.
  • There shouldn’t any side effects of the code, to the rest of the application.

The idea of encapsulation is to keep classes separated and prevent them from having tightly coupled with each other. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class.

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

What is inheritance?

A

Inheritance is the mechanism by which an object acquires the some/all properties of another object.

Inheritance allows a class to use the properties and methods of another class. In other words, the derived class inherits the states and behaviors from the base class. The derived class is also called subclass and the base class is also known as super-class. The derived class can add its own additional variables and methods. These additional variable and methods differentiates the derived class from the base class.

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

What is polymorphism?

A

Polymorphism is the capability of a method to do different things based on the object that it is acting upon. In other words, polymorphism allows you define one interface and have multiple implementations.

It plays an important role in allowing objects having different internal structures to share the same external interface.

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

Two common uses of polymorphism?

A

Method overloading and method overriding.

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

What are the two types of polymorphism?

A

There are two types of polymorphism in java - runtime polymorphism (dynamic polymorphism) and compile time polymorphism (static polymorphism).

Method overriding is a perfect example of runtime polymorphism. In this kind of polymorphism, reference of class X can hold object of class X or an object of any sub classes of class X.

Compile time polymorphism is nothing but the method overloading in java. In simple terms we can say that a class can have more than one methods with same name but with different number of arguments or different types of arguments or both. To know more about it refer method overloading in java.

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

What are the types of inheritance in Java?

A

Single, multiple, multilevel, and hybrid.

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

What is single inheritance?

A

Single inheritance is damn easy to understand. When a class extends another one class only then we call it a single inheritance. The below flow diagram shows that class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A.

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

What is multiple inheritance?

A

“Multiple Inheritance” refers to the concept of one class extending (Or inherits) more than one base class. The inheritance we learnt earlier had the concept of one base class or parent. The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes.

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

What is multilevel inheritance?

A

Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class. As you can see in below flow diagram C is subclass or child class of B and B is a child class of A.

17
Q

What is hybrid inheritance?

A

In simple terms you can say that Hybrid inheritance is a combination of Single and Multiple inheritance. A typical flow diagram would look like below. A hybrid inheritance can be achieved in the java in a same way as multiple inheritance can be!! Using interfaces. yes you heard it right. By using interfaces you can have multiple as well as hybrid inheritance in Java.

18
Q

Name some programming techniques.

A

a) Unstructured Programming (Assembly language programming)
b) Procedural Programming (Assembly language, C programming)
c) Object Oriented Programming (C++, Java, Smalltalk, C#, Objective C)

19
Q

What is unstructured programming?

A

This consists of just writing the sequence of commands or statements in the main program, which modifies the state maintained in Global Data. Example: Assembly Language programs.

20
Q

Limitations of unstructured programming?

A

a) The data is global and code operates on it
b) As the size of code increases, maintenance is a problem
c) Does not have independent data for processing
d) The concept of local variables did not exist
e) Reusability of code was not supported