Object Programming  «Prev 

Understanding objects and classes

In programming terms, an object is a self-contained component that contains properties and methods needed to make a certain type of data useful. The properties of an object are what it knows and its methods represent the services it can provide.
In addition to providing the functionality of the application, methods ensure that an object's data is used appropriately by running checks for the specific type of data being used. They also allow for the actual implementation of tasks to be hidden and for particular operations to be standardized across different types of objects. You will learn more about these important capabilities in Object-oriented concepts: Encapsulation.
Objects are the fundamental building blocks of applications from an object-oriented perspective and provide a blueprint.

Objects in Java:

Let us now look deep into what are objects. If we consider the real-world we can find many objects around us, Cats, Dogs, Humans, etc. All these objects have a state and behavior. If we consider a cat, then its state is
  1. name,
  2. breed,
  3. color,
and the behavior is purring, sleeping, running.
If you compare the software object with a real world object, they have very similar characteristics. Software objects also have a state and behavior. A software object's state is stored in fields and behavior is shown via methods. So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.

Classes in Java:

A class is a blue print from which individual objects are created. A sample of a class is given below:

public class Cat{
  String breed;
  int age;
  String color;

  void purr(){
  }
  
  void hungry(){
  }
   
  void sleeping(){
  }
}

1) A class is to object as blueprint is to house.
A class is to object as blueprint is to house.
A class defines the structure of an object much like a blueprint defines the structure of a house.

2) A single blueprint can be used to build many houses since the blueprint
A single blueprint can be used to build many houses since the blueprint defines the structure and makeup of the houses.

3) Class outline object makeup
Classes are similar to blueprints in this regard, except that they define the makeup of objects.

4) There are many objects of the same kind in the real-world.
There are many objects of the same kind in the real-world. Getting back to the house metaphor, even though houses vary in lots of different ways, they all share common characteristics.

5) Object instance of class
In OOP terms, a house object is an instance of the house class.

6) House specific instance of class
An instance of a class is an object that has been created based on the class (blueprint). Instances of classes are also commonly referred to as instantiated objects.