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?
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