OOPs | Object oriented design

Object oriented design started right from the moment computers where invented, programming was there and programming approaches came into picture. Right from the moment computer was invented, programming was there.Programming in its basic is nothing but giving certain instructions to the computer.

At the beginning of the computing era, the programming was usually limited to machine language programming. These machine language program means those set of instructions that are aimed at particular machine or particular processor, these are in the form of 0’s and 1’s.These are sequence of bits (0100110..). But its quiet difficult to write a program or develop a software in machine language.

It’s actually impossible to develop a software which we are using in today’s scenario using sequence of bits. This was the only reason programmers moved on to the next way of programming and developed something called assembly language which was near to English language and easy to understand. This assemble languages were used in microprocessor. With the invention of microprocessor assemble language flourished and ruled over the industry. But still, it was not enough.So again the programmers came up with the new thing, i.e, Structured Programming and Procedural Programming.

Structured programming –
The basic principal of structured programming approach is to divide a program in functions and modules. The use of modules and functions makes the program more understandable and readable. It helps to write cleaner code and helps to maintain control over each function and modules. This approach gives importance to functions rather than data. It focuses on the development of large software applications for example C was used for Os development. The programming languages: PASCAL(introduced by Blaise Pascal) and C (introduced by Dennis Ritchie) follow this approach.



Procedural programming approach –
This approach is also known as top down approach. In this approach, a program is divided into functions that perform a specific task. This approach is mainly used for medium sized applications. Data is global and all the functions can access the global data.The basic drawback of Procedural Programming Approach is that data is not secured because data is global and can be accessed by any function. Program flow control is achieved through function calls and goto statements. The programming languages: FORTRAN(developed by IBM) and COBOL(developed by Dr. Grace Murray Hopper) follow this approach.

These programming constructs were used in late 1960 or around 1970.There was still some issues with these language, Though they fulfilled the criteria of a well sructured programs, software etc but they were not so structured as the requirement was at that time. They seems to be over generalised and doesn’t corelate with the real time application.

To solve all such kinds of problems, OOP, i.e., object oriented approach was like solution of all the problems.

Object oriented programming (OOP) approach –
The oop concept was basically designed to overcome the drawback of the above programming methodologies. Those were not so close to real world application. The demand were increased and still conventional methods were used. With the emergence of OOP all conventional methods were out of window. OOP methodology not only solved the new era problem but also it brought a revolution in the programming methodology field.

Object oriented programming (OOP) is nothing but which allows to write programs with the help of certain classes and objects and these objects are real time objects. We can say that this approach is very close to real world and its applications because the state and behaviour of these classes and object are almost same with the real world objects.

Lets go deeper to the general concepts of OOP which are given below:

What are Class and object?
It is the basic concept of OOP. It is an extended concept of the structure which is used in C. It is an abstract as well as user defined data type. It consists of several variables and functions. The primary purpose of the class is to store data and information. The members of a class define the behaviour of the class. A class is the blueprint of the object also we can say implementation of class is the object. Class is not visible to the world but the object is.

filter_none

edit
close

play_arrow

link
brightness_4
code

Class car
{
    int car_id;
    char colour[4];
    float engine_no;
    double distance;
  
    void distance_travelled();
    float petrol_used();
    char music_player();
    void display();
}

chevron_right


Here car is a class which has a id, colour and engine no.It resembles to real world car which has the same specifications and these specification can be declared public (visible to everyone outside the class), protected and private (visible to none). Also there lies some methods such as distance_travelled, petrol_used, music_player etc. In the code given below car is a class and c1 is the object of car.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
class car {
public:
    int car_id;
    double distance;
  
    void distance_travelled();
  
    void display(int a, int b)
    {
        cout << "car id is=\t" << a << "\ndistance travelled =\t" << b + 5;
    }
};
  
int main()
{
    car c1; // Declare c1 of type car
    c1.car_id = 321;
    c1.distance = 12;
    c1.display(321, 12);
  
    return 0;
}

chevron_right


Data abstraction –
Abstraction refers to the act of representing important and special features without including the background details or explanation about that feature. Data abstraction simplifies database design.



  1. Physical level:
    It describes how record is stored. These are often hidden from the user. It can be described with block of storage.
  2. Logical level:
    It describes data stored in database, and relationship among the data.The programmers generally work at this level as they are aware of the function need to maintain the relationships between the data.
  3. View level:
    Application program hide details of data type and information for security purpose. This level is generally implemented with the help of GUI and details which are meant for user are shown up.

Encapsulation –
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of wrapping of data and methods that work on that data within one unit, e.g., a class in Java. This concept is also often used to hide the internal state representation, of an object from the outside.

Inheritance –
Inheritance is the capability of one class of things to inherit capabilities or properties of another class called parent class. When we write a class we inherit properties from another classes. So when we create a class we do not need to write all the properties and functionalities again and again, as these can be inherited from another class which posses it. Inheritance allows user to reuse the code whenever possible and reducing redundancy of code.

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        System.out.println("GfG!");
  
        Dog dog = new Dog();
        dog.name = "Bull dog";
        dog.color = "Brown";
        dog.bark();
        dog.run();
  
        Cat cat = new Cat();
        cat.name = "Rag doll";
        cat.pattern = "White and slight brownish";
        cat.meow();
        cat.run();
  
        Animal animal = new Animal();
  
        animal.name = "My favourite pets";
  
        animal.run();
    }
}
  
class Animal {
    String name;
    public void run()
    {
  
        System.out.println("Animal is running !");
    }
}
  
class Dog extends Animal { 
  
/// class dog is the child class and  animal is parent
  
    String color;
    public void bark()
    {
        System.out.println(name + " Wooh ! Wooh !"
                           + "I am of colour " + color);
    }
}
  
class Cat extends Animal {
  
    String pattern;
  
    public void meow()
    {
        System.out.println(name + " Meow ! Meow !"
                           + "I am of colour " + pattern);
    }
}

chevron_right


Polymorphism –
Polymorphism is the ability for a message or data to be processed in more than one form.It allows to perform the same task in various ways. It consists of method overloading and method overriding, i.e., write the method once and perform a number of tasks using the same method name.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
void output(float);
void output(int);
void output(int, float);
  
int main()
{
    cout << "\nGfG!\n";
    int a = 23;
    float b = 2.3;
  
    output(a);
    output(b);
    output(a, b);
  
    return 0;
}
  
void output(int var)
{ // same function name but different task
    cout << "Integer number:\t" << var << endl;
}
  
void output(float var)
{ // same function name but different task
    cout << "Float number:\t" << var << endl;
}
  
void output(int var1, float var2)
{ // same function name but different task
    cout << "Integer number:\t" << var1;
    cout << " and float number:" << var2;
}

chevron_right


Some important points to know about OOP:

  1. OOP treat data as a critical element.
  2. Emphasis is on data rather than procedure.
  3. Decomposes the problem into simpler modules.
  4. Doesn’t allow data to freely flow in the entire system.
  5. Data is protected from external functions.

Advantages of OOPs –

  • It models real world very well.
  • With OOP, programs are easy to understand and maintain.
  • OOP offers code reusability. Already created classes can be reused without having to write them again.
  • OOP facilitates Quick Development of program as parallel development of classes is possible.
  • With OOP, programs are easier to test, manage and debug.

Disadvantages of OOP –

  • With OOP, classes sometimes tend be overly generalised.
  • The relations among classes become artificial at times.
  • The OOP programs design is tricky and require appropriate knowledge. Also one needs to do proper planning and proper design for OOP programming.
  • To program with OOP, programmer need proper skills such as design skills, programming skills, thinking in terms of objects and classes etc.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AhmedHamouda