Object Oriented Concepts

Object-Oriented Programming (OOPs) Concept helps in reducing the code complexity and allows the reusability of code. OOPs is a programming paradigm that brings together data and methods in a single entity called an object. 

What are Object Oriented Concepts?

1. Objects & Classes

Objects are the primary unit of OOPS representing real-life entities like inheritance, hiding, and polymorphism. They are invoked with the help of methods. These methods are declared within a class. 

2. Abstraction

Abstraction means showing only the relevant details to the end-user and hiding the irrelevant features that serve as a distraction. For example, a simple ATM provides minimal information about how it is built, that is implementation details are hidden. 

Example 1

class Bike

{

     Bike()

     {

           System.out.println("The Street Bob. ");

     }

     

    void weight()

    {

       System.out.println("Light on its feet with a hefty : 630 lbs.");

    }

class Bullet extends Bike

 {

      void drive()

     {

          System.out.println("Old-school yet relevant.");

     }

 }

public class Abstraction 

 {

      public static void main (String args[])

      {

            Bike obj = new Bullet();

            obj.drive();

            obj.weight();

      }

 }

3. Encapsulation

Encapsulation is a means of binding data variables and methods together in a class. Only objects of the class can then be allowed to access and modify this data. This is known as data hiding and helps in the insulation of data (Encapsulation). 

When we expose class A’s data directly to another class B and B can change A’s data, then data encapsulation of A is considered to be broken. This should not happen.

Example 1

class Encapsulate 

{           private String name; 

  private int height; 

             private int weight;  

             Encapsulate( int newWeight, String newName, int newHeight)

             {

       weight = newWeight;

                      name = newName;

  height = newHeight;

  }

  public int getHeight()  

  {         return Height; 

  } 

  public String getName()  

  {            return Name; 

  } 

      

  public int getWeight()  

  {        return Weight; 

  } 

   

  // avoid setter methods.

public class TestEncapsulation 

{     

    public static void main(String[] args)  

    { 

          Encapsulate obj = new Encapsulate(60, “Code”, 5); 

          System.out.println("My name: " + obj.getName()); 

          System.out.println("My height: " + obj.getWeight()); 

          System.out.println("My weight " + obj.getHeight());    

    }

}

4. Inheritance – Single, Multi-level, Hierarchical and Multiple

Inheritance is the process by which one class inherits the functions and properties of another class. The main function of inheritance is the reusability of code. Each subclass only has to define its features. The rest of the features can be derived directly from the parent class.

Single Inheritance – Refers to a parent-child relationship where a child class extends the parent class features. Class Y extends Class X.

Multi-level Inheritance – Refers to a parent-child relationship where a child's class extends another child’s class. Class Y extends Class X. Class Z extends Class Y.

Hierarchical Inheritance – This refers to a parent-child relationship where several child classes extend one class. Class Y extends Class X, and Class Z extends Class X.

Multiple Inheritance – Refers to a parent-child relationship where one child class is extending from two or more parent classes. Some languages like Java do not support this inheritance.

Example Program of Inheritance in Java

class Animal 

{

      void habit()

       {       System.out.println("I am nocturnal!! ");    }

}

class Mammal extends Animal 

{    

      void nature()

      {   System.out.println("I hang upside down!! ");    }

}

class Bat extends Mammal 

{

     void hobby()

     {      System.out.println("I fly !! ");    }

}

public class Inheritance 

{

      public static void main(String args[])

     {

             Bat b = new Bat();

             b.habit();

             b.nature();

             b.hobby();

     }

}

5. Polymorphism – Static and Dynamic

It is an object-oriented approach that allows the developer to assign and perform several actions using a single function. Static Polymorphism is based on Method Overloading, and Dynamic Polymorphism is based on Method Overriding.

Example Program of Static Polymorphism with Method Overloading

class CubeArea 

{

double area(int x)

{         return 6 * x * x;     }

}

class SphereArea 

{         double area(int x)

          {   return 4 * 3.14 * x * x;  }

}

class CylinderArea 

{

double area(int x, int y)

{     return x * y;               }

}

public class Overloading

{

       public static void main(String []args)

       {

CubeArea ca = new CubeArea();

  SphereArea sa = new SphereArea();

  CylinderArea cia = new CylinderArea();

System.out.println("Surface area of cube = "+ ca.area(1));System.out.println("Surface area of sphere= "+ sa.area(2)  System.out.println("Surface area of cylinder= "+ cia.area(3,4));

          }

}

Example Program of Dynamic Polymorphism with Method Overriding 

class Shape 

{

    void draw()

    {

        System.out.println("Your favorite shape");

    }

    void  numberOfSides()

    {

         System.out.println("side = 0");

    }

}

class Square extends Shape 

{

    void draw()

    {

        System.out.println("SQUARE ");

    }

    void numberOfSides()

    {

        System.out.println("side = 4 "); 

    }

}

class Pentagon extends Shape 

{

    void draw()

    {

        System.out.println("PENTAGON ");

    }

    void numberOfSides()

    {

        System.out.println("side= 5"); 

    }

}

class Hexagon extends Shape 

{

    void draw()

    {

        System.out.println("HEXAGON ");

    }

    void numberOfSides()

    {

        System.out.println("side = 6 ");

    }

}

public class Overriding{

    public static void main(String []args){

        Square s = new Square();

        s.draw();

        s.numberOfSides();

        Pentagon p = new Pentagon();

        p.draw();

        p.numberOfSides();

        Hexagon h = new Hexagon();

        h.draw();

        h.numberOfSides();

     }

}

Follow are the key points for earning the Object Modelling badge.

  1. The class should have compact methods. The number of lines in each method must be in the range of 10 to 20. Lengthy methods should be broken into multiple compact methods.
  2. The class should not contain too many methods and variables. Lengthy classes must be broken down into smaller classes.
  3. The class should have a single responsibility, and that class should have only one job (Refer to SRP document).
  4. The class should follow the abstraction principle. Must have private variables and public methods. 
  5. Avoid having public variables and too many private methods. Having public variables can lead to encapsulation breaking.  
  6. Avoid inheritance depth of more than 3 (Multilevel inheritance ) 
  7. Classes should not break encapsulation.

Multiple Choice Questions

1. Which of these features of OOPs would indicate code reusability?

a) Polymorphism

b) Abstraction

c) Inheritance

d) Encapsulation

Answer (c)

2.  Which of these inheritances is shown in case we inherit some base class from another class, then one of the classes derives it?

a) Single

b) Multiple

c) Multi-level

d) Hierarchical

Answer (c)

3. Which of the following is not related to Object Oriented Programming

a) Class and object

b) Structure and union 

c) Constructor and destructor

d) Inheritance and Polymorphism

Answer (b)

4. Wrapping data and its related functionality into a single entity is known as 

a) Abstraction

b) Modularity

c) Polymorphism

d) Encapsulation

Answer (d)

5. What does modularity mean?

a) Hiding part of the program

b) Subdividing the program into small independent parts

c) Overriding parts of the program

d) Wrapping things into a single unit

Answer (b)

6. What is Instantiation in terms of OOP terminology?

a) Creating an instance of a class

b) Copying an instance of a class

c) Deleting an instance of a class

d) Modifying an instance of a class 

Answer (a)

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us