Class And Objects in OOPs

Back to home
Logicmojo - Updated Jan 3, 2023



Class

A class is an object's blueprint or template. It's a data type that's been specified by the user. A class defines variables, constants, member functions, and other functionality.It does not utilise any memory during execution. It's worth noting that classes aren't considered data structures. It's a logical thing. It's the best example of data binding I've ever seen.

In general, class declarations can have the following elements in the following order:

  1. Modifiers: A class might be open to the public or have default permissions.

  2. class keyword: To construct a class, use the class keyword.

  3. Class name: The first letter of the name should be capitalised (capitalized by convention).

  4. Superclass(if any): If there is one, the name of the class's parent (superclass), followed by the term extends. A class can only extend (subclass) one parent at a time.

  5. Interfaces(if any): If the class implements any interfaces, a comma-separated list of them, prefixed by the keyword implements. More than one interface can be implemented by a single class.

  6. Body: The body of the class is surrounded by braces.

Constructors are used to create new objects from scratch. Fields are variables that offer information about the state of the class and its objects, and methods are used to implement the class's and objects' actions. In real-time applications, many types of classes are employed, such as nested classes, anonymous classes, and lambda expressions.

Objects

It represents real-life entities and is a fundamental unit of Object-Oriented Programming.A typical Java application generates a huge number of objects that communicate with one another via methods. The following elements make up an object:

  1. 1. State: The qualities of an object represent it.It also reflects an object's attributes.

  2. 2. Behavior: It is represented via an object's methods. It also reflects the interaction of an object with other objects.

  3. 3. Identity:It assigns a unique name to an object and allows it to connect with other objects.

A dog is an example of an item.

Objects are objects that can be discovered in the real world. Objects such as "circle," "square," and "menu" may be found in a graphics software, for example. Objects such as "shopping cart," "customer," and "product" may be found in an online shopping system.

Implementation in Java

public class Dog
{
	String name;
	String breed;
	int age;
	String color;

	public Dog(String name, String breed,
				int age, String color)
	{
		this.name = name;
		this.breed = breed;
		this.age = age;
		this.color = color;
	}

	public String getName()
	{
		return name;
	}

	public String getBreed()
	{
		return breed;
	}

	public int getAge()
	{
		return age;
	}

	public String getColor()
	{
		return color;
	}

	@Override
	public String toString()
	{
		return("Hi my name is "+ this.getName()+
			".\nMy breed,age and color are " +
			this.getBreed()+"," + this.getAge()+
			","+ this.getColor());
	}

	public static void main(String[] args)
	{
		Dog tuffy = new Dog("tuffy","papillon", 5, "white");
		System.out.println(tuffy.toString());
	}
}

In this class, there is only one constructor. A constructor can be identified by its declaration, which has the same name as the class and has no return type. The Java compiler classifies constructors according to the number and type of arguments. The Dog class's constructor accepts four arguments. For those parameters, the following sentence provides the values "tuffy," "papillon," "5", and "white":

Difference Between Classes And Objects

Class Object
It is a logical entity. It is a real-world entity.
It is conceptual. It is real.
It binds data and methods together into a single unit. It is just like a variable of a class.
It does not occupy space in the memory. It occupies space in the memory.
It is a data type that represents the blueprint of an object. It is an instance of the class.
It is declared once. Multiple objects can be declared as and when required.
It uses the keyword class when declared. It uses the new keyword to create an object.
A class can exist without any object. Objects cannot exist without a class.



With this article at Logicmojo, you must have the complete idea of Classes and Objects in OOps.