Classes and Objects in Java

Classes and Objects in Java

Class

Think about our life in this world. We are the people living all together. Think about our surroundings. Think about our environments. There are animals, there are trees, there are forests, buildings, vehicles, mountains, sea, rivers and many more unlimited things which are uncountable. Each of them are unique in their own way. Like we can not match or compare trees to mountains, we can not compare vehicles to sea or rivers. This makes no sense, right? These are the elements in our world and they are different for each others. Each one have their own identifier, own symbol, own activities, style, methods, properties, etc.

So just like above class also acts exactly in same way. When you write a program or software you have break it down in multiple part. Each part will have different functionalities and responsibilities. For example if you start to build a software for restaurant management system what will have to do? You will have to break it down to possible part like Customer, FoodMenu, Chef, Waiter, Tables, Payment, Manager and etc. Can see you see any similar thing in these parts? No right? Each one contains each piece of informations, each one act in each way. Each one have different different properties and functions. Customer part can hold the properties like name, address, previousPurchasedFood, phoneNumber and etc. FoodMenu can hold foodName, price, cookTime, flavour and etc. See? Each one contains each piece of information. But all together they can build a complete system which can make a human’s life easier, isn’t it? So these unique parts is called class in java. A class can hold properties, methods. It will define the activities of itself. Class specifies the type of information and most importantly a class will define the behaviour. It is a blue print of your data or information.

In java there are some conventions to declare a class. The first character of words is capital in Name. Java uses “class” keyword before class name to declare a valid class. The body is surrounds by curly brackets {} . Like:

public class Customer{
    
}
public class FoodMenu{

}
. Class can contains methods and the methods name follows the camelCase style. Same style is also applied for fields too. Like:
public class Customer{
    private String name;
    private String address;
    private String phoneNumber;
    private String previousPurchsedFood;

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getAddress(){
        return address;
    }

    public void setAddress(String address){
        this.address = address;
    }

    public String getPhoneNumber(){
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber){
        this.phoneNumber = phoneNumber;
    }

    public String getPreviousPurchsedFood(){
        return previousPurchsedFood;
    }

    public void setPreviousPurchsedFood(String previousPurchsedFood){
        this.previousPurchsedFood = previousPurchsedFood;
    }
}
Its so easy to understand. It is neat and clean.


Objects

Now lets say you have a class. So how do you use it? How do you access the data information, properties or manipulate it. In order to do that you need to create an object of a particular class. Question is what is it and how does it work? An object in Java is an instant of a class. Its like a remote control of your TV. Here remote control is the object and your TV is the class. You can change the channel, volume or color system any time through the remote control. In Java an object act like in this way. You create a class and you create an object of that class to control it. It is so simple as that. In java an object is created by using new keyword. Like this:

public class Resturant{
    public static void main(String [] args){
        Customer cus = new Customer();
        cus.setName("Demo name");
        cus.setAdderss("Demo Address");
        cus.setPhoneNumber("123456");
        
        System.out.println(cus.getName());
        System.out.println(cus.getAddress());
        System.out.println(cus.getPhoneNumber());
    }
}
Here you can see we have created the object of our customer class. Notice that we initialized the Customer by writing Customer cus = new Customer();Here cus is the object variable. Its not necessary to name the variable cus. You can name it whatever you like most. Through the object we use dot notation to access the properties(methods or fields). Like cus.setName("Demo name");

0