Home » What is Object Oriented Programming?
What is object oriented programming?

What is Object Oriented Programming?

What is Object-oriented programming and how to use it using Java ?

All Examples are given in Java

A computer programming paradigm known as object-oriented programming (OOP) arranges the architecture of software around data or objects rather than functions and logic. An object is a data field with particular characteristics and behaviour.

OOP places more emphasis on the objects that programmers desire to manipulate than on the logic necessary to do so. Large, complicated, and actively updated or maintained programmes are a good fit for this method of programming. This encompasses mobile applications as well as design and production software. OOP, for instance, can be applied to manufacturing system simulation software.

In collaborative development, where projects are divided into groups, the method is advantageous due to the organisation of an object-oriented programme. OOP also offers the advantages of efficiency, scalability, and reused code.

Data modelling is the process of gathering all the objects a programmer wants to work with and determining how they relate to one another.

An object can be anything from a physical thing, like a person who has properties like a name and an address, to a small computer programme, like a widget.

Once an object is identified, it is assigned to a class of objects that describes the type of data it contains and any manipulable logic patterns.

Structure of object-oriented programming

The following are some of the components, or building blocks, of object-oriented programming:

Classes: User-defined data types called classes serve as the building blocks for specific objects, attributes, and methods.

example 1.0 - creating a class in Java

//classes examples 

public class Animal {
     int id;
     String Name;
     String type;
    
    public Animal(int id, String Name, String type){
        this.id = id;
        this.Name = Name;
        this.type = type;
    }

    @Override
    public String toString() {
        return "Id " + this.id + "Name"+ this.Name + "type" + this.type;
    }
}

Objects: Objects are instances of a class that were generated using data that was precisely defined. Objects can be an abstract concept or a real-world thing. The description is the only item that is first defined when a class is created.

example 1.1 - Instantiating and Object in Java

//creating object out of Animal class
public class Animal {
     int id;
     String Name;
     String type;
    
    public Animal(int id, String Name, String type){
        this.id = id;
        this.Name = Name;
        this.type = type;
    }

    @Override
    public String toString() {
        return "Id: " + this.id + "Name: "+ this.Name + "type : " + this.type;
    }
}


// this is main class 
public class Main {
    public static void main(String[] args) {
        
        Animal Lion = new Animal(1, "Lion", "Lion");
        System.out.println(Lion);
        
        //output on console will be 
        //Id: 1, Name: Lion ,type : Lion;
    }
}






Methods: Methods are described as internal class functions that describe an object’s behaviours. The first line of code in every method seen in class declarations is a reference to an instance object. Additionally, an object’s internal functions are referred to as instance methods. Programmers utilise techniques to make code reusable or to keep functionality contained within a single object at a time.

example 1.2 - calling a method


//creating object out of Animal class
public class Animal {
     int id;
     String Name;
     String type;
    
    public Animal(int id, String Name, String type){
        this.id = id;
        this.Name = Name;
        this.type = type;
    }
    
    public void AnimalIsRunning(){
    System.out.println("Lion is running");
    }
    
    @Override
    public String toString() {
        return "Id: " + this.id + "Name: "+ this.Name + "type : " + this.type;
    }
}
// this is main class 
public class Main {
    public static void main(String[] args) {
        
        Animal Lion = new Animal(1, "Lion", "Lion");
        Lion.AnimalIsRunning();
        //output on console will be 
        //Lion is Running
    }
}




Attributes: The class template defines attributes, which stand in for an object’s state. Data will be kept in the attributes field of objects. Class characteristics are the property of the class.

example 1.3 - changing attributes


//creating object out of Animal class
public class Animal {
     int id;
     String Name;
     String type;
    
    public Animal(int id, String Name, String type){
        this.id = id;
        this.Name = Name;
        this.type = type;
    }
    
    public void AnimalIsRunning(){
    System.out.println("Lion is running");
    }
    
    @Override
    public String toString() {
        return "Id: " + this.id + "Name: "+ this.Name + "type : " + this.type;
    }
}
// this is main class 
public class Main {
    public static void main(String[] args) {
        
        Animal Lion = new Animal(1, "Lion", "Lion");
        //changing an attribute
        Lion.type = "Lioness";
        System.out.println(Lion.type);
        //output on console will be 
        //Lioness
    }
}

Principles of Object oriented programming

Object-oriented programming is based on the following principles:

Encapsulation: According to this idea, only certain information is exposed while all essential information is kept inside an item. Each object’s implementation and state are kept secretly inside a designated class. This class is not accessible to other objects, nor do they have the power to modify it. Only a limited set of public functions or methods can be called by them. Data concealing with this feature enhances programme security and prevents inadvertent data corruption.

example 2.0 - Encapsulating or privatising the fields

public class Animal {
     private int id;
     private String Name;
     private String type;

    public Animal(int id, String Name, String type){
        this.id = id;
        this.Name = Name;
        this.type = type;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "Id " + this.id + "Name"+ this.Name + "type" + this.type;
    }
}

// this is main class 
public class Main {
    public static void main(String[] args) {
        
        Animal Lion = new Animal(1, "Lion", "Lion");
        //changing an attribute
        Lion.setType("Lioness");
        System.out.println(Lion.getType());
        //output on console will be 
        //Lioness
    }
}


Abstraction:  Objects conceal any extraneous implementation code and only expose internal mechanisms that are necessary for the use of other objects. The functionality of the derived class can be increased. This idea can make it simpler for developers to add or update something later.

example 2.1 - using abstraction

// Abstract class
abstract class Animal {
  // Abstract method (does not have a body)
  public abstract void animalSound();
  // Regular method
  public void sleep() {
    System.out.println("Zzz");
  }
}

// Subclass (inherit from Animal)
class Lion extends Animal {
  public void animalSound() {
    // The body of animalSound() is provided here
    System.out.println("The Lion says: roar roar");
  }
}

class Main {
  public static void main(String[] args) {
    Lion Simba = new Lion(); // Create a Lion object
    Simba.animalSound();
    Simba.sleep();
  }
}

Inheritance :Code from other classes may be reused by classes. The ability to assign relationships and subclasses allows developers to reuse similar functionality while yet retaining a distinct hierarchy. This attribute of OOP speeds up development and provides more accuracy by requiring a more in-depth investigation of the data.

example 2.2 - using inheritance


class Animal {
  protected String familyName = "Animal";     
  public void eat() {                 
    System.out.println("Animal is eating");
  }
}

class Lion extends Animal {
  private String lionName = "Simba";    // Lion attribute
  public static void main(String[] args) {
    Lion simba = new Lion();

    // Call the eat() method (from the Animal class) on the Lion object
    simba.eat();
    System.out.println(simba.familyName + " " + simba.lionName);
  }
}

Polymorphism: Objects can have multiple forms and are intended to have shared behaviours. There will be less requirement for duplicating code because the programme will discern which usage or meaning is required for each execution of that object from a parent class. The functionality of the parent class is then expanded by a child class that is formed. Different sorts of objects can pass via the same interface thanks to polymorphism.

example 2.3 - using polymorphism




class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Lion extends Animal {
  public void animalSound() {
    System.out.println("The Lion roars");
  }
}

class Cat extends Animal {
  public void animalSound() {
    System.out.println("The cat mewos");
  }
}


    What are examples of object-oriented programming languages?

    For example, popular pure OOP languages include:

    1. Ruby
    2. Scala
    3. JADE
    4. Emerald

    Programming languages designed primarily for OOP include:

    1. Java
    2. Python
    3. C++

    Other programming languages that pair with OOP include:

    1. Visual Basic .NET
    2. PHP
    3. JavaScript

    More Reading

    Post navigation

    Leave a Comment

    Leave a Reply

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