Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A distinguishing feature of objects is that an object’s procedures can access and often modify the data fields of the object with which they are associated (objects have a notion of “this” or “self”). In OO programming, computer programs are designed by making them out of objects that interact with one another. There is significant diversity in object-oriented programming, but most popular languages are class-based, meaning that objects are instances of classes, which typically also determines their type.
1. A First Look 1
Procedural vs Object Oriented
- Focus is on procedures
- All data is shared: no protection
- More difficult to modify
- Hard to manage complexity
Advantages of Object Orientation
- People think in terms of object
- OO models map to reality
- OO models are: Easy to develop & Easy to understand.
2. Principles 2
Fundamental Principles of OOP
In order for a programming language to be object-oriented, it has to enable working with classes and objects as well as the implementation and use of the fundamental object-oriented principles and concepts: inheritance, abstraction, encapsulation and polymorphism.
2.1 Encapsulation 3 4 5
Encapsulation is the packing of data and functions into a single component. The features of encapsulation are supported using classes in most object-oriented programming languages, although other alternatives also exist. It allows selective hiding of properties and methods in an object by building an impenetrable wall to protect the code from accidental corruption.
What it do? We will learn to hide unnecessary details in our classes and provide a clear and simple interface for working with them.
Example: A popular example you’ll hear for encapsulation is driving a car. Do you need to know exactly how every aspect of a car works (engine, carburettor, alternator, and so on)? No – you need to know how to use the steering wheel, brakes, accelerator, and so on.
2.2 Inheritance 6 7
Inheritance is when an object or class is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation (inheriting from an object or class) specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior).
What it do? We will explain how class hierarchies improve code readability and enable the reuse of functionality.
Example: A real-world example of inheritance is genetic inheritance. We all receive genes from both our parents that then define who we are. We share qualities of both our parents, and yet at the same time are different from them.
Example: we might classify different kinds of vehicles according to the inheritance hierarchy. Moving down the hierarchy, each kind of vehicle is both more specialized than its parent (and all of its ancestors) and more general than its children (and all of its descendants). A wheeled vehicle inherits properties common to all vehicles (it holds one or more people and carries them from place to place) but has an additional property that makes it more specialized (it has wheels). A car inherits properties common to all wheeled vehicles, but has additional, more specialized properties (four wheels, an engine, a body, and so forth). The inheritance relationship can be viewed as an is-a relationship. In this relationship, the objects become more specialized the lower in the hierarchy you go.
Look at the image above you will get a point.8 Yes, the derived class can access base class properties and still the derived class has its own properties.
In computer science, abstraction is a technique for managing complexity of computer systems. It works by establishing a level of complexity on which a person interacts with the system, suppressing the more complex details below the current level. The programmer works with an idealized interface (usually well defined) and can add additional levels of functionality that would otherwise be too complex to handle.
What it do? We will learn how to work through abstractions: to deal with objects considering their important characteristics and ignore all other details.
Example: You’ll never buy a “device”, but always buy something more specific : iPhone, Samsung Galaxy, Nokia 3310… Here, iPhone, Samsung Galaxy and Nokia 3310 are concrete things, device is abstract.
2.4 Polymorphism 9
Polymorphism is the provision of a single interface to entities of different types. A polymorphic type is one whose operations can also be applied to values of some other type, or types.
What it do? We will explain how to work in the same manner with different objects, which define a specific implementation of some abstract behavior.
Example: All animal can speak, but dogs woof, cats meow, and ducks quack
There are two types of polymorphism
- Overloading (compile time polymorphism): methods have the same name but different parameters.
- Overriding (run time polymorphism): the implementation given in base class is replaced with that in sub class.
Example 10: Let us Consider Car example for discussing the polymorphism. Take any brand like Ford, Honda, Toyota, BMW, Benz etc., Everything is of type Car.
But each have their own advanced features and more advanced technology involved in their move behavior.
Learn Object Oriented Programming though Mario Game
3.1. Object 11
Objects are key to understanding object-oriented technology. Look around right now and you’ll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.
In mario world,
Mario is an object. Goomba is an object. Koopa is also an object.
Even a coin and a pile are objects
Software objects are conceptually similar to real-world objects: they too consist of state and related behavior.
An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object’s internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object’s methods is known as data encapsulation — a fundamental principle of object-oriented programming.
In Mario world,
Mario has some fields like position (which indicate where Mario stands), state (which indicate whether Mario alive), and some methods like walk , fire or jump.
Goomba has some fields like position (which indicate where Goomba stands), state (which indicate whether Goomba die), and direction (which indicate the direction Goomba moves). Goomba has move method, and jumped_on method (which occurs when it is jumped on by Mario) (Poor Goomba!)
3.2 Class 12
Task 1: With boiler plate code, make an gif image (32×32) Mario fire ball and jump to get coins