OOP Basics

May 11, 2020

There are four fundamental ideas in object-oriented programming:

  • Abstraction
  • Polymorphism
  • Inheritance
  • Encapsulation

the delicious acronym A-P-I-E. A pie!

Abstraction

Abstraction means we focus on the essential qualities of something rather than one specific example. By using abstraction, we automatically discard what’s unimportant or irrelevant.

Focusing on the idea of a person instead of an individual is an example of what fundamental idea in object-oriented programming? abstraction

Encapsulation

The idea of encapsulation is about containing the elements of an object. Not just to keep them together, but to also protect them. We bundle an object’s attributes or data along with the methods that operate on that data within the same unit or the same class. One reason for doing that is to restrict access to some of the object’s components.
One of the principles of encapsulation is that an object should not make anything about itself available except what is absolutely necessary for other parts of the application to work. This is a concept referred to as ‘black boxing’.

Steve is able to turn on and adjust his television even though he does not know how it works internally. This exemplifies which principle of object-oriented programming? encapsulation.

Inheritance

Inheritance enables a new class to receive or inherit the attributes and methods of existing classes using the same implementation which is a great form of code reuse.

Polymorphism

which is a complicated sounding word that simply means having many forms.

Dynamic

One form, called dynamic or run-time polymorphism, allows us to access methods using the same interface on different types of objects that may implement those methods in different ways.
Assume brew method of a coffee maker, there are several ways it could be done. Now in new class of FrenchPress, it replaces the brew method it inherited from the BasicCoffeeMaker through a process called overriding. That allows the FrenchPress to redefine its own unique version of the brew method. – Or perhaps both of these coffee makers inherit from the same abstract class with an abstract brew method. – Or they both agree to implement the same interface. Inheritance, abstract classes, and interfaces are all possible implementations of polymorphism.

Static

The other common form of polymorphism is called static or compile-time polymorphism. And it uses a feature of many object-oriented programming languages called method overloading, not to be confused with method overriding. Overloading allows you to implement multiple methods within a class that have the same name, but a different set of input parameters.
You can also have variations of the method with different numbers of input parameters.
Or overloading a method from the same class that can take different sets of parameters.

P.S. Analysis vs Design

Analysis describes a problem; design describes a solution.

Leave a Reply:

Your email address will not be published. Required fields are marked *