7

Today I had an interview for software engineering position. I have read many things from stackoverflow for the interview. They asked me about the normal things realated to OOP. But they also asked me about these:

Is Encapsulation possible without inheritance?

Is Abstraction possible without inheritance?

Is Polymorphism possible without inheritance?

I have answered these according to my knowledge of OOP. I don't know whether it is right or wrong. Can anyone tell me the correct answer of these with some related examples.

Thanks

ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80
Muhammad Noman
  • 1,344
  • 1
  • 14
  • 28

6 Answers6

9
  • Is Encapsulation possible without inheritance?

Yes, because Encapsulation is the ability to hide a class properties from the outside world by means of access methods.

  • Is Abstraction possible without inheritance?

Well, abstraction can refer to many things, but talking about OOP: No, an abstract class cannot be used directly, you can only instantiate inherited classes.

  • Is Polymorphism possible without inheritance?

Yes, polymorphism is the construction of a single interface to several types of objects, for instance, a single function call that can receive different classes or data types as arguments. They can be inherited or not.

manolonte
  • 169
  • 4
  • So, in encapsulation. You are saying that you have one class in which you have all methods and you want to hide that from outside the world? right? then where is your access method? If it's in the same class? then not they are unhide? – Muhammad Noman Apr 11 '16 at 15:26
  • I meant you want to hide the properties of the class or instance from the outside world, so you build methods (setters, getters, etc.) to avoid direct access to them. That's encapsulation. – manolonte Apr 11 '16 at 15:43
  • The "etc" part is essential. Some people believe encapsulation is only about providing getters and setters without realizing that it is actually about bundling together data and its associated behavior (i.e. methods that make use of that data). [http://www.javaworld.com/article/2075271/core-java/encapsulation-is-not-information-hiding.html](http://www.javaworld.com/article/2075271/core-java/encapsulation-is-not-information-hiding.html) – Piovezan Jun 16 '16 at 13:40
2

Encapsulation is definitely possible without inheritance. Encapsulation is the concept of hiding data that from outside objects that should not be able to manipulate it. An example of encapsulation would be private fields of an object in Java. You can then use public methods (such as getters and setters, or other calculating methods) to manipulate the data only as needed.

Abstraction and Polymorphism, however, are directly related to inheritance.

Abstraction is when you take away the implementation details of an object and create an abstract class or an interface (speaking in terms of Java). This will act as a contract for what any implementing or inheriting class will need to include in the detailed implementation. The abstract class will have method signatures, but no body; the inheriting class will implement the bodies.

Polymorphism is the ability to implement something abstract in different forms. For example, if you have an abstract class called Animal that contained a speak() method, you could create a Dog class that inherits from Animal and implement speak() to print "woof", while a Cat() class would implement speak() to print "meow".

Note that it does depend on which type of polymorphism is being examined. You can, as stated in another answer, have method/function parameter polymorphism, and as stated that is possible without inheritance.

Nick Suwyn
  • 501
  • 1
  • 5
  • 21
1

The answer to all three questions is Yes, it's possible to do them without inheritance, but the real answer is "It depends on the language they're implemented in".

I mean, consider that the very first C++ compiler actually compiled it's code to standard C, which is not an OOP language. Obviously, the code had to be compiled to a language that did not support inheritance.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
0

Well, my understanding:

  • Encapsulation is just an idea to prevent direct modification to an instance's state, it can be done even without OOP.

  • Abstraction is generalization of classes(object templates), it cannot be done without inheritance. It focuses on common-contract-terms.

  • Polymorphism means same action but different behaviors. Usually it works with Abstraction/Interfaces. It cannot be done without OOP

0
  • Encapsulation is possible without inheritance:

    simply add private attribute to a class and use setter and getters to access this attribute.

  • Abstraction by it self is possible without inheritance: You can make a class abstract and it does not require any inheritance.

If the question asked was : can an Abstract class be used without inheritance?

Then no, it cannot be used without inheritance because it needs to be instantiated first and therefore it requires inheritance. But Abstraction by itself does not need inheritance.

  • Polymorphism is not possible without inheritance: This is because polymorphism between the two type of objects must have something in common for it work. This could simple even by Object class in case of java, from which all classes are derived.
Rana
  • 1,675
  • 3
  • 25
  • 51
  • That depends entirely on the language. For instance, in Smalltalk, you can achieve Polymorphism without inheritance, because Smalltalk is a late binding language that allows you to treat two objects as the same as long as they both implement the same methods (including signatures) – Erik Funkenbusch Apr 11 '16 at 19:05
  • Good to know. So there isn't a correct answer for this question until a language is specified? – Rana Apr 11 '16 at 19:08
0

I would say

Encapsulation - yes. For instance a function manipulating a static variable declared inside it (like in c++). Or c code hiding non-exported variables and functions from other compilation units.

Abstraction - yes. "Concepts" in c++ are an example of this. In c++ you can write a routine and say "this code compiles as long as the argument type defines a less than operator".

Polymorphism - yes. A simple function pointer can be used to provide a pluggable implementation.

Alexander Torstling
  • 18,552
  • 7
  • 62
  • 74