Dawood Ahmad

Microsoft Certified Solution Developer

Back-end Developer

Front-end Developer

Freelancer

0

No products in the cart.

Dawood Ahmad

Microsoft Certified Solution Developer

Back-end Developer

Front-end Developer

Freelancer

Detail

Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP)

OOPObject Oriented Programming, OOP, is the most influential paradigm of our time. This handout summarizes the most basic style, elements, and vocabulary of OOP that are common to all OOP languages. OOP languages can have weird features, but the basic ideas of OOP are pretty straightforward.

 

OOP

In OOP, the logical arrangement of the code is changed. Instead of an informal arrangement of functions into different files, functionality is officially and tightly grouped with the type that it operates on. The OOP style groups all the operations together according to what they operate on ā€” all the hash table operations are part of the hash table class, all the string operations are part of the string class. Put another way: if variables are nouns and functions are verbs, then OOP divides up everything around the nouns.

Class

The most basic concept in OOP is the Class. A class is like a type in the classical language. Instead of just storing size and structural information for its data, a class also stores the operations which will apply to the data. Class = Storage +
Behavior. A class is like an Abstract Data Type in Pascal or Cā€” it creates a logical coupling between data and the operations on that data. Bundle the verbs with their nouns.

Object

An object is a run-time value that stores the state and belongs to some class. Objects know what class they belong to, and so they automatically know what operations they are capable of. The word “instance” is another word for “object”.

Encapsulation

“Encapsulation” refers to protecting the internals of an object from direct manipulation by the client. The client can send messages, but the client cannot change the bits in the object directly. In C, it’s just a convention that the client
should not mess with or depend on the implementation of an Abstract Data Type (ADT). With encapsulation, the compiler enforces the separation. The class implementation can indicate which parts of the implementation are protected so that the client code accessing those parts will not compile. Or put another way: the client cannot mess with the object’s state ā€” the client can only send messages. The object’s state is only touched by its own methods. Once those methods are correct and debugged, the object can be given to any client, and that client should not be able to perform an operation on the object which puts it in an incorrect state.

Hierarchy

Classes in OOP are arranged in a tree-like hierarchy. A class’ “superclass” is the class above it in the tree. The classes below a class are its “subclasses.” The semantics of the hierarchy is that classes have all the properties of their superclasses. In this way, the hierarchy is generally up towards the root and specific down towards its leaves. The hierarchy helps add logic to a collection of classes. It also enables similar classes to share properties through “inheritance” below. A hierarchy is useful if there are several classes that are fundamentally similar to each other. In C++, a “base class” is a synonym for superclass, and a “derived class” is a synonym for subclass.


Inheritance

“Inheritance” is the process by which a class inherits the properties of its superclasses. Methods, in particular, are inherited. When an object receives a message, it checks for a corresponding method in its class. If one is found, it is
executed. Otherwise, the search for a matching method travels up the tree to the superclass of the object’s class. This means that a class automatically responds to all the messages of its superclasses.

Overriding

When an object receives a message, it checks its own methods first before consulting its superclass. This means that if the object’s class and its superclass both contain a method for a message, the object’s method takes precedence. In
other words, the first method found in the hierarchy takes precedence. This is known as “overriding,” because it gives a class an easy way to intercept messages before they get to its superclass. Most OOP languages implement overriding
based on the run-time class of objects. In C++, run-time overriding is an option invoked with the “virtual” keyword.

Polymorphism

A big word for a simple concept. Often, many classes in a program will respond to some common message. In a graphics program, many of the classes are likely to implement the method “drawSelf().” In the program, such an object can safely be sent the drawSelf() message without knowing its exact class since all the classes implement or inherit drawSelf(). In other words, you can send the object a message without worrying about its class and be confident that it will just do the right thing. Polymorphism is important when the code is complex enough that you are no longer sure of the exact class of an object ā€” you can just send it a message and rely on it doing the right thing at run time based on its class.

Taggs:
2 Comments
  • Malik Abdul Rahman 1:03 am July 11, 2021 Reply

    nice work

  • M Ahmad S 5:01 pm July 11, 2021 Reply

    Good read.

Write a comment