The two main components of any object-oriented programming language are classes and objects. Both of these components revolve around real-life entities.
Table of Contents
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:
- Modifiers: Modifiers determine the scope of the object. Classes can either be public, private, or protected depending upon the functionality.
- Class Keyword – When defining a new class, the declaration must start with the keyword ‘class’.
- 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.
- 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.
- 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.
- 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:
- State – The properties and attributes of the object determine its state.
- 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.
- 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.
0 Comments