WSQ03

--Originally published at chozaoop

Ok so the author here reviews some basic concepts of Object-Oriented Programming, some of these were already reviewed on one of Ken’s classes, but personally i gained some new knowledge here.

All the “snips” and annotations i’ll be taking are from this video. Since i liked the images he used for explaining the concepts, they seems clear and simple.

Encapsulation:

WSQ03

“The idea of encapsulation is simply that the fields of an object, an instance should only be read or written by methods of that instances class…”

“If we want to read or write one of these fields, we should not do so directly from outside the object, only the methods of the class should be directly reading and writing these fields from outside the class, externaly we only interact with the fields indirectly by invoking the methods, the thinking here is that we create a class, the methods you give that class ENCAPSULATE the encompass all the knockledge of what is meant to be done with the data that makes up that data type…”

Inheritance:

WSQ03

“The idea of inheritance is simply that some data types, some classes may overlap, such as that one data type has all the same stuff found in another…”

So in this example above the Cat class inheritances from Mammal and them both inherits from Animal, which means that the Mammal class automatically inherits all of the members from the animal class, and so, the Cat class inherits the members from both of above, even if when you wrote the Cat class you only specified “F” and “G”.

We could say that Mammal and Cat are subtypes of Animal, and Cat is a subtype of Mammal, and so, Animal would be a supertype of Mammal and Cat, kinda easier to say Parent and Child.

Also, let’s say it like this, the Animal class has some methods or states which could be Eating, Sleeping and Breathing, Mammals also have these properties BUT, Mammals also have some “unique” properties such as, hot-blooded or furry idk, but also Cat has some properties which Animal and Mammal don’t such as Purring or Meowing.

Is-a vs. Has-a:

WSQ03

Here we ought to ask ourselves, is a relationship between these two classes an “is-a” relationship or a “has-a” relationship, like the past example, Mammal class was a more specific type of class than Animal, that’s a “Is-a” relationship, which is a proper use of inheritance.

Another example is about a car, a car is composed of many parts, let’s say that we have a Stereo class, and we have a Car class, then it would make sense that the Car class has a “Stereo” field, which is a “Has-a” relationship, since it calls for composition.

It would not make sense to have the Stereo class inherit from the Car class, because the Stereo is not a kind of Car, nor the other way around either, because the Car is not a kind of Stereo, if we were going to extend the car class we would do so as something like: Ford or Sedan or Jeep, something which is a Car, just a more specific type of Car.

Polymorphism:

WSQ03

Ok so here i’ll explain it myself, let’s say we create a method called foo(), in the class “Jack”, Ted inherits is but then we create another one called also foo() in Lisa, then Kate and Mike inherits this, if we call a Kate, Mike or Lisa object, this will invoke the foo() method created in Lisa, but if we do so from Ted or Jack, it will call the method created in Jack.

Kind of vague explanation of Polymorphism, i understand that the word itself means “changing forms”, i might ask Ken for help throught the week or even via Twitter.


WSQ03