Classes and Objects in Java

by | May 12, 2021 | Java

Home » Java » Classes and Objects in Java

The two main components of any object-oriented programming language are classes and objects. Both of these components revolve around real-life entities.

Classes

Classes are object factories in the sense that these entities act as the prototypes from which objects are created. Objects created from the same class share the fields of the class and behave in a similar way. Therefore, classes also determine object behaviour. Class declaration contains the following components as listed below:

  1. Modifiers: Modifiers determine the scope of the object. Classes can either be public, private, or protected depending upon the functionality.
  2. Class Keyword – When defining a new class, the declaration must start with the keyword ‘class’.
  3. Class Name – The name of the class must start with a capital letter followed by an alphanumeric combination. Developers conventionally start a class name with capital letters. However, this is not a mandate.
  4. Super Class – If the class you are creating is a subclass, then it must have the name of the superclass in its declaration. It should be preceded by the keyword ‘extends’. Multiple inheritance is not allowed in Java therefore a class can only extend one superclass at a time.
  5. Interfaces – A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
  6. Body – ‘{ }’ encloses the body of the class. It contains the class’ functionality.

Constructors initialize the fields of a class thereby aid in the creation of new objects. Fields are essentially variables that set the tone of a class. These fields govern the behaviour of the objects. Methods inside a class render functionality to the class and its objects.

Object

Objects are the fundamental units of OOP and they represent real-life entities. A Java program creates many objects. Every object consists of the following three attributes:

  1. State – The properties and attributes of the object determine its state.
  2. Behaviour – The methods of the object determine how it behaves. The manner in which the object responds to other objects represents a major part of object behaviour.
  3. Identity – Identity is the name given to the object. This represents how the object differs from other objects.

Example of an Object: Dog

  • Identity: Name of the Dog
  • State/Attributes: Breed, Age, and Colour.
  • Behaviours: Bark, Sleep, Eat.

Sample Code

The following code represents a Dog class. We will be creating objects of the Dog class.

public class Dog
{
String name;
String breed;
int age;
String colour;
public Dog (String name, String breed, int age, String Colour)
{
this.name = name;
this.breed = breed;
this.age = age;
this.colour = colour;
}
public String getName()
{
return name;
}
public String getBreed()
{
return breed;
}
public int getAge()
{
return age;
}
public String getColour()
{
return colour;
}
@Override
public String toString()
{
return (“The name of the dog” + this.getName() + “The breed, age, and colour are” + this.getBreed() + this.getAge() + this.getColour());
]
public static void main (String args [])
{
Dog obj = new Dog (“Victor”, “dalmatian”, 5, “white”);
}
}

 

OUTPUT

The name of the Dog is Victor.
The breed, age, and colour are dalmatian, 5, white.

The class has a single constructor that initiates all the fields (variables) of the class.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author