0

So I'm quite new to OOP and Java in general. I've been going through my textbook on the 4 most fundamental concepts of OOP programming but it's quite confusing.

The textbooks states "A class can change its functioning or variable type and the user would not need to change their code" as an advantage of Encapsulation, but to me that sounds like Abstraction...

Also, the textbook lists Overriding and Overloading as forms of inheritance but Google is telling that Overriding and Overloading are forms of Polymorphism.

Can someone clear this up for me?

Don Branson
  • 13,631
  • 10
  • 59
  • 101
SimpleJack
  • 151
  • 1
  • 12
  • I looked up Polymorphism on Wikipedia. It gives slightly different definitions. I think you might have to accept that the terms themselves don't have universally agreed definitions. Computer Science is still a bit of an Art. – markspace Sep 14 '14 at 20:27
  • Josh Bloch gives a good example as to how encapsulation helps users modify APIs without breaking the code of whoever is using it ([YouTube - How to write good API desgin and why it matters](http://www.youtube.com/watch?v=heh4OeB9A-c)). – Vince Sep 14 '14 at 20:37
  • As for polymorphism and inheritence, they usually go hand-in-hand. Polymorphism is the act of communicating with objects of different types through a single interface. If `Poodle` and `Beagle` extend `Dog`, then `Dog` can be used as an interface for objects of those two types, since they will inherit methods from `Dog`. Overriding allows you to change the implementations; what happens when you communicate using the interface. – Vince Sep 14 '14 at 20:38

3 Answers3

1
  1. You are fully right that it is abstraction which allows the implementation to change without affecting its user. However, encapsulation is a key feature in building bullet-proof abstractions: without it a user of your object stays independent of implementation only if he follows the convention never to access internal state of the object.

  2. Overloading has definitely, positively, nothing to do with inheritance. It is a (weak) kind of polymorphism, but most of all, it is just an allowance to use the same name for several completely distinct methods. It has next to nothing to do with OOP and is fully applicable to any other programming paradigm, such as procedural or functional.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Wikipedia calls "overloading" *ad hoc polymorphism* and says it is a form of polymorphism. I think I'm going to go with "these terms do not have universally agreed definitions." – markspace Sep 14 '14 at 20:30
  • Yes, *polymorphism* is quite a loose term and its content is highly context-dependent. – Marko Topolnik Sep 14 '14 at 20:32
  • @MarkoTopolnik thank you very much. perhaps its time to get a better textbook. – SimpleJack Sep 14 '14 at 20:44
1

"A class can change its functioning or variable type and the user would not need to change their code" as an advantage of Encapsulation, but to me that sounds like Abstraction ...

This means that as long that you keep the same class definition and public interface, you may change the implementation of your class without forcing users of your class to adapt their code. So they don't have to deal with the internal changes made in your class. The public interface of your class includes all methods and members exposed by your class.

Ricardo Leon
  • 125
  • 13
0

Maybe this questions and answers from StackOverflow can help you:

What is the main difference between Inheritance and Polymorphism?

How encapsulation is different from abstraction as a object oriented concept in java?

Community
  • 1
  • 1
margabit
  • 2,924
  • 18
  • 24