3

I am very new to programming and have this humble OOP-related question:

Can we have a module in a OOP-manner written program, that contains 2 or more OOP features? Say, both Encapsulation and polymorphism OR both inheritance and modularity, etc...

3

Strictly speaking, only polymorphism is the defining feature of OOP. The other attributes are incidental, but still useful.

  • Encapsulation is a feature of abstract data types where the externally visible interface and the implementation details are separated so that the implementation details can change without the interface also having to change. Access modifiers like public or private are not necessary for this, but help to avoid depending on implementation details. Encapsulation is used to achieve modularity, but this is possible without OOP.

  • Inheritance has two aspects:

    • one is subtyping, which means that if an object of type Foo can be used wherever an object of type Bar is expected, then Foo is a subtype of Bar. Many OOP languages primarily model this with interfaces. However, this definition assumes we have a meaningful type system, which many like JavaScript do not.

      Type systems are often used to restrict polymorphism: If I call a method foo.method(), the compiler will statically guarantee that foo has a type that provides a suitable method.

    • the other is code reuse. When a class Subclass inherits from a Parent class, the Subclass not only inherits the interface but also all data fields and behaviour. New fields and new methods can be added, and existing behaviour can be overridden.

      As it turns out, this is a quite problematic mechanism for code reuse. This is often stated with the slogan “Composition over Inheritance”.

Of these, encapsulation is the most important. A modular approach makes large software more manageable, and a class is a kind of module. The associations between modules should be few and well-defined. This means that a module must publish a careful interface rather than exposing irrelevant details. It is not always necessary that the modules can be selected or swapped out at runtime.

Only when we have some behaviour where we don't know beforehand which exact function should be executed at runtime, do we need polymorphism. For example, I might have a list of different values that I all want to print out. They might be strings or numbers or birds or cars or BadOopExamples, but they have one thing in common: they can be printed out. An OOP solution would (depending on the language) define an explicit type Printable or an implicit type that each object must have a print() operation. Each value that is put into the list must satisfy this type. I can then use that list and for each Printable p in the list: p.print() and will get some output that depends on each value. While this solution relied on polymorphism, other design techniques such as encapsulation were irrelevant. The various object-oriented design patterns all rely on polymorphism.

So while encapsulation is really important, polymorphism is really powerful. Many designs combine both strategies, but good design does not rely on chasing some buzzword (which OOP has become). It is possible that a good design ends up using none of these techniques, but they are still a good guideline. Especially software testing becomes much easier when we have small polymorphic modules, since the polymorphism can be used to test one module in isolation.

1

OO is a paradigm - a collection of ideas/methodologies for modelling solutions to problems. You can think of OO as being "an approach to problem solving", or even a mindset.

Encapsulation and Modularity are closely related to each other. Inheritance is a prerequisite for Polymorphism. These concepts aren't mutually exclusive, in fact they frequently work together.

Inheritance and Polymorphism are tools offered by programming languages which support OO. Encapsulation and Modularity is about your design, and the way you structure your code.

As a programmer in a language such as Java or C# you might use inheritance and polymorphism to enhance encapsulation, and to create modular code, or to extend somebody else's code without breaking encapsulation.

The goal of OO is to help humans manage complexity, and programmers are regularly asked to solve enormous complex problems which they can't fit into their head all at once.

It's much easier for humans to solve problems when things are split into managable chunks. So when a program needs to solve an enormous problem (or many enormous problems), it's desirable for the solution to consist of many small self-contained classes or modules which each have a clear, well-defined purpose; preferring small functions which are much easier to understand than those with 'bloated' logic.

0

Yes. In fact inheritance and encapsulation are often used to achieve polymorphism. What all of these words mean will become clearer when you collect some practical Object-Oriented programming experience.

  • Hello Philipp, can you please expand your answer a bit more with an extra example for a widely used combination besides inheritance & encapsulation ? ... There are some reasons why I think this is important... Thank you, and would thank you again, – JohnDoea Feb 17 '16 at 22:45
  • Hope you didn't miss my message.. – JohnDoea Feb 19 '16 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.