• Post Reply Bookmark Topic Watch Topic
  • New Topic

making sure my knowlege of OOP is correct  RSS feed

 
john hog
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preparing for interviews and I want to make sure what I understand is correct, and if there is anything to add or change

Abstraction : The concept of only showing what an object can do but not how. We hide the internal representation(does this just mean methods are not implemented? what about instance variables?) and show WHAT an object is capable of doing such as ADD or SUBTRACT or SORT. An abstract class is meant to be extended so each concrete implementation of the abstract class has its own implementation logic. The user does not need to know how things work(how do I expand on this).

Encapsulation: The act of hiding things that can change such as instance variables or methods. We do not want the client to incorrectly use the variables and by using GETTERS and SETTERS we can validate user input. A well encapsulated class is also acting as a single unit, in the future if a change needs to be made then you go directly to the encapsulated class and no other class should be affected. Encapsulation hides how something works so a client does not create code based on the logic, in case the logic is changed in the future.

Inheritance: The concept of creating strong relationship between objects. When extending a class you inherit all inheritable properties and you can add more or override things. This allows you to be able to use a subtype for a method requiring an abstract or super type because a subtype IS - A supertype.

Polymorphism: Kind of confused on how to explain this because I feel it has to do with the same as inheritance. Is it inheritance or polymorphism that lets you override things or is it both?

how would you explain all of these concepts in an interview
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try our campfire stories. There is one about polymorphism (←link).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john hog wrote:Abstraction :

Only thing I can see there is that "abstraction" is itself an abstract concept (recursion? ), so abstract classes aren't really part of the definition. They are a mechanism used to allow "partial" implementations. Although it's certainly true that the best ones (eg, AbstractList) exhibit many of the properties of abstraction.

Encapsulation:

Again, you need to be careful with examples. Many would say that getters and setters actually break encapsulation (especially the latter), by providing users with methods that are too closely related to a class's internals. You might be interested in this article.

Inheritance: The concept of creating strong relationship between objects.

I think you can go further and say "hierarchical", since inheritance requires a hierarchy.

Polymorphism: Kind of confused on how to explain this because I feel it has to do with the same as inheritance. Is it inheritance or polymorphism that lets you override things or is it both?

Inheritance is the structure, overriding is the vehicle. Some of the meaning can be derived from the word (which is really a portmanteau) "Poly" = many, "morph" = change, so it refers to the fact that the same method can be overridden for different children of a superclass, providing "many behaviours".
The classic example is the draw() method of a Shape class (or interface). A Circle class will implement it differently from a Rectangle class, but if you have an array of Shapes, you can call draw() on any one of them, and it will behave as it's supposed to for the object it's called on.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!