Questions on OOPs

1. What is OOPs?
Ans. OOPs stands for Object Oriented Programming specifications. It is a programming technique or paradigm in which every thing revolves around real world entities or objects. Main focus in this technique is on objects rather than actions and data rather than logic. C++, Java, C# are few examples which supports OOPs.

2. What are the core concepts of OOPs?
Ans. OOPs core concepts are :

  1. Abstraction
  2. Encapsulation
  3. Polymorphism
  4. Inheritance
  5. Composition
  6. Association
  7. Aggregation
3. How does OOPs improves software development?
Ans. The key  benefits of OOPs are :
Re-use  of previous  work: using  implementation inheritance  and  object  composition.
Real  mapping  to the  problem  domain:  Objects map to  real  world and represent  vehicles, customers, products etc.  with  encapsulation.
Modular Architecture:  Objects, systems, frameworks etc are  the building blocks of  larger systems. The  increased  quality  and  reduced  development time  are the by-products of the  key  benefits discussed above. If 90% of the  new  application consists of proven  existing  components then only  the remaining  10%  of the code have to be  tested from scratch.

4. What is the difference between Abstraction and Encapsulation?
Ans. Main differences between Abstraction and Encapsulation are :
1. Abstraction provides a general structure of a class and leaves it's implementation detail to the implementer. Encapsulation is used to restrict access to members of an object.
2. Abstraction is implemented using interfaces and abstract classes. Encapsulation is implemented using four types of access specifiers public, private, protected and default. 

5. What is abstract class in Java?
Ans. A class which defines the structure of a class without going into implementation details is known as abstract class. We can not instantiate an abstract class directly. Abstract classes can have abstract as well as non abstract methods. We have to use abstract keyword for defining abstract classes, which then can be extended using extends keyword.

6. What is interface in Java?
Ans. An interface is also used to define structure of a class with full abstraction which means unlike abstract classes interfaces would not provide any implementation of methods.

7. What is the difference between abstract classes and interfaces?
Ans. There are many differences between them, one of the major difference is interface is used to apply full abstraction where as abstract classes can be used for partial abstraction.
For more information please visit : Difference between abstract classes and interfaces

8. What is the difference between method overloading and method overriding?
Ans. In method overloading same method name is used multiple times with different set of parameters in same class whereas in method overriding implementation of method from super class is override in sub classes without changing it's set of parameters.
Another difference is overloading is resolved at compile time because class information is know at compile time but overriding is resolved at runtime because object information is required to resolve overridden methods.

9. What are the rules for method overloading?
Ans. When we are overloading methods we need to take care that their signature should be different which can be achieved either by changing number of arguments or changing type of arguments. If we are trying to overload methods by simply changing it's return type then compiler will throw an exception. 

10. What are the rules for method overriding?
Ans. When we are overriding methods in sub class then we can't change below things :
1. Signature of method can not be changed.
2. Return type of method can not be changed.
3. Overloaded method can not throw higher exception.

11. What is covariant method overriding in Java?
Ans. Consider a scenario where we have a method in a parent class which is returning a general type like List class etc. and in our sub class we have to override the same method but it would return an ArrayList instead of List. We know that return type of overridden method can not be changed, right, but in this case we can change the return type to ArrayList because it is a sub type of List. This is known as covariant method overriding.

12. What is the difference between "IS A" and "HAS A" relationship?
Ans. The "is a" relationship is expressed with inheritance whereas "has a" relationship is expressed with composition. Both techniques are used for code re-usability. Consider below example for understanding both techniques :


"is a" relationship or inheritance is uni-directional. As shown in above example, House is a Building but Building is not a House. In Java programs to establish "is a" relationship we use extends keyword.
In above example there is "has a" relationship between House and Bathroom, it is incorrect to say House is a Bathroom. In order to establish "has a" relationship we create instances of other classes inside the main class.

13. When to use inheritance and composition?
Ans. Below are the rules that we should follow while using these two techniques :
1. Don't use inheritance relationship unnecessary just for code re-use. If there is no "is a" relationship between classes then composition should be used.
2. Overuse of inheritance (using extends keyword) can break all sub classes if super class is modifed hence use it carefully.
3. Do not use inheritance just to get Polymorphism. If there is no "is a" relationship and all we want is polymorphism then we can  use interfaces with object composition.

14. What is difference between Aggregation and Composition?
Ans. Aggregation is an association in which one class belongs to a collection of classes. That class is a part of whole relationship where a part can exist without a whole, which means if whole relationship is deleted then also that class can exists.
For example : Bill of an order placed contains line items which specifies which products we have ordered. If we delete line items from that order then we do not need to delete products. This means there is weaker relationship between line items and products.

Composition is an association in which one class belongs to a collection of classes. This is a part of whole relationship where a part cannot exist without a whole. If whole is deleted then all parts are deleted.
For example : If we delete the Order then all line items in that order will get deleted, because like line items can not exist without an order. 

15. What is difference between implementation inheritance and interface inheritance?
Ans. For answer please visit : OOPs Concepts - 2

Share:

Related Posts:

No comments:

Post a Comment

Popular Posts

Recent Posts

Followers

Total Pageviews