Objects In OOPS Explained

In Object Oriented Programming the objects are real word entity such as book, shop, pen, computer, etc. Objects are very important in this paradigm and along with other OOPs concept(class, abstraction, encapsulation, inheritance and polymorphism) it's simply the software development.

Why need an object in OOPS?

Objects are the instances of a class which means this object is instantiated by the class. To understand object are the instance term let's see an example, Consider you have 5 balls in your hand and each ball has its some characteristics like size, color, and type of the ball. Now in programming terms, you can create a class called Ball that has data variable size: integer, color: String, and type: String. In the real world, there are 5 balls so using the concept of object we can create 5 objects in ball class. Objects: ball1, ball2, ball3, ball4, ball5. These 5 ball objects will hold the characteristics of the ball(size, type, and color) so in simple terms object represents the class.

Therefore an object can used anywhere in the software to use the property and behavior in efficient and easy way. In this example we understood that "ball class is an factory or steps to create ball and the object is the ball we created with the property of ball class".

Code for ball class(Java)

class Ball{

//property of ball

int size;

String color;

String type;

//ball class constructor to instantiate the value

Ball(int size, String color, String type){

this.size = size;

this.color = color;

this.type = type;

}

public static void main(String args[]) {

//Creating 5 balls object with some characteristics

Ball ball1= new Ball(5,"red","cricket ball");

Ball ball2= new Ball(5,"green","tennis ball");

Ball ball3= new Ball(5,"blue","foot ball");

Ball ball4= new Ball(5,"yellow","volley ball");

Ball ball5= new Ball(5,"brown","basket ball");

}


In java "new" is the keyword to create an object . After public static void main() we used it to create ball class object.

Key points
  •   Objects are real word entity
  •   They represent the class and holds all the property and behavior
  •   You can create any number of object for a class
  •   After creating object it's important to destroy for better memory management. Almost all    the programming languages automated the object destroying 

 

Post a Comment

Previous Post Next Post
B