πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

Class, Objects & Methods in Java  


Real World Example of Class, Methods & Objects

First lets understand what is Class, Methods & Objects in real world, below are some classes

Animal Class
Birds Class
Vehicle Class
Furniture Class
  • A class is a template or blueprint used to categorize objects. For example, we can have classes like Animal, Birds, Vehicles and Furniture. Each class can have multiple objects, which represent real-world entities.
    • The Animal class can have objects such as elephant, tiger, dog etc.
    • The Birds class can have objects like sparrow, peacock etc.
    • Similarly, other classes will have their respective objects.
  • Every class has its own methods, which define the actions that objects can perform:
    • The Animal class may have methods like eat(), run(), sleep() etc.
    • The Birds class may have methods like fly(), eat() etc.
  • Since objects represent real-world entities, they are used to call methods and perform actions.

What is Class in Java ?
  • Class is a blueprint or prototype or template for creating objects in Java.
  • It is not a real-world entity, means it does not exist physically; it only defines how an object should behave.
  • Class does not occupy memory (but objects occupies memory).
  • NOTE : Class metadata (such as the number of variables, variable names, method names, constructors, etc.) is stored in the Method Area.
  • Syntax :
    access-modifiers class ClassName
    {
    	Fields (Instance Variables) – Store object data.
        Constructors – Initialize objects.
        Methods – Define object behavior.
        Static Members – Shared among all objects.
        Nested Classes – Class inside another class.
        Blocks – Static and instance blocks for initialization.
    }
  • Example :
    public class Animal
    {
        //fields
    	int eyes;
        String color;
        
        //method
        void run()
        {
            //body
        }
    }

What is Method in Java ?
  • A method in Java is a block of code that performs a specific task and can be reused multiple times.
  • Syntax :
    access-modifiers return-type methodName(List of Parameters)
    {
    	// body
    }
  • Example :
    public void run(String name)
    {
        System.out.println(name+" is running");
    }
  • We can write computations, data processing, input/output operations, object manipulation, conditional logic etc inside methods to perform specific tasks efficiently.

What is Object in Java ?
  • Object is an instance of a class - Objects are created from a class blueprint and represent real data.
  • Object is a real-world entity - It represents tangible things like a car, person, or book in programming.
  • Object occupies memory - When an object is created (new ClassName()), it gets memory allocated in the heap.
  • Objects has its own :
    • Identity - Unique memory reference assigned by JVM
    • State / Attribute - Data stored in instance variables
    • Behaviour - Actions (methods)
  • Syntax :
    • 1. Creating Objects:
      • ClassName objectName = new ClassName();
        • ClassName objectName; β€” Declares a reference variable objectName of type ClassName.
        • new ClassName(); β€” Creates a new object of ClassName by allocating memory and calling its constructor.
        • = β€” Assigns the reference (address) of the newly created object to the variable objectName.
        • So overall: objectName now holds a reference to a new ClassName object in memory.
    • 2. Access Methods using Objects:
      • objectName.methodName();
    • 3. Access Fields/Variables using Objects
      • objectName.field_name;
  • Example :
    public class Animal
    {
        String color;   //instance variable
        
        void run()  //method
        {
            System.out.println("Im running");
        }
        public static void main(String[] args)
        {
            Animal buzo = new Animal();     //creating object (buzo)
            buzo.run();                     //accessing run() method from buzo object
            buzo.color = "Black";           //setting value in instance variable using . operator
    
            System.out.println("Buzo color is "+buzo.color);    //accessing instance variable
        }
    }

Program 1:
  • Below is the simple program in which we have created one Animal1 class, one run() method and called the run() method using jumbo object.
  • // Define a class named Animal1
    public class Animal1 
    {
        // Method to display a running message
        public void run() 
        {
            System.out.println("I'm running"); // Print a message to the console
        }
    
        // Main method - entry point of the program
        public static void main(String[] args) 
        {
            // Create an instance (object) of the Animal1 class
            Animal1 jumbo = new Animal1();
    
            // Call the run method using the object 'jumbo'
            jumbo.run();
        }
    }
    Output:
    I'm running

Program 2:
  • Below is the simple program having one Animal2 class, two methods i.e. run() and eat() method and jambo object is accessing both the methods.
  • // Define a class named Animal2
    public class Animal2 
    {
        // Method to display a running message
        public void run() 
        {
            System.out.println("I'm running"); // Print a message to the console
        }
    
        // Main method - entry point of the program
        public static void main(String[] args) 
        {
            // Create an instance (object) of the Animal2 class
            Animal2 jumbo = new Animal2();
    
            // Call the run method using the object 'jumbo'
            jumbo.run();
    
            // Call the eat method using the object 'jumbo'
            jumbo.eat();
        }
    
        // Method to display an eating message
        public void eat() 
        {
            System.out.println("I'm eating...!!"); // Print a message to the console
        }
    }
    Output:
    I'm running
    I'm eating...!!

Program 3:
  • Below is the simple program having one Animal3 class, two methods i.e. run() and eat() methods. Then two objects i.e. jambo and buzo objects are accessing both the methods.
  • // Define a class named Animal3
    public class Animal3 
    {
        // Method to display a running message
        public void run() 
        {
            System.out.println("I'm running"); // Print a message indicating the animal is running
        }
    
        // Main method - entry point of the program
        public static void main(String[] args) 
        {
            // Create an instance (object) of Animal3 named 'jumbo'
            Animal3 jumbo = new Animal3();
    
            // Call the run method using the 'jumbo' object
            jumbo.run();
    
            // Call the eat method using the 'jumbo' object
            jumbo.eat();
    
            // Create another instance (object) of Animal3 named 'buzo'
            Animal3 buzo = new Animal3();
    
            // Call the eat method using the 'buzo' object
            buzo.eat();
    
            // Call the run method using the 'buzo' object
            buzo.run();
        }
    
        // Method to display an eating message
        public void eat() 
        {
            System.out.println("I'm eating...!!"); // Print a message indicating the animal is eating
        }
    }
    Output:
    I'm running
    I'm eating...!!
    I'm eating...!!
    I'm running

Program 4:
  • Below is the another program with parameters in methods.
  • // Define a class named Animal4
    public class Animal4 
    {
        // Method to display that an animal is running
        public void run(String name) 
        {
            System.out.println(name + " is running");
        }
    
        // Main method - program entry point
        public static void main(String[] args) 
        {
            // Create an object 'jumbo' of Animal4
            Animal4 jumbo = new Animal4();
            jumbo.run("Jumbo"); // Call run method with "Jumbo"
            jumbo.eat("Jumbo"); // Call eat method with "Jumbo"
    
            // Create another object 'buzo' of Animal4
            Animal4 buzo = new Animal4();
            buzo.eat("Buzo"); // Call eat method with "Buzo"
            buzo.run("Buzo"); // Call run method with "Buzo"
        }
    
        // Method to display that an animal is eating
        public void eat(String name) 
        {
            System.out.println(name + " is eating...!!");
        }
    }
    Output:
    Jumbo is running
    Jumbo is eating...!!
    Buzo is eating...!!
    Buzo is running

Program 5:
  • Below is the another program with multiple parameters in methods.
  • // Define a class named Animal5
    public class Animal5 
    {
        // Method to display that an animal has run a certain distance
        public void run(String name, int distance_km) 
        {
            System.out.println(name + " has run " + distance_km + " km");
        }
    
        // Main method - program entry point
        public static void main(String[] args) 
        {
            // Create an object 'jumbo' of Animal5
            Animal5 jumbo = new Animal5();
            jumbo.run("Jumbo", 5); // Call run method with name "Jumbo" and distance 5 km
            jumbo.eat("Jumbo", "grass"); // Call eat method with name "Jumbo" and food "grass"
    
            // Create another object 'buzo' of Animal5
            Animal5 buzo = new Animal5();
            buzo.eat("Buzo", "meat"); // Call eat method with name "Buzo" and food "meat"
            buzo.run("Buzo", 12); // Call run method with name "Buzo" and distance 12 km
        }
    
        // Method to display that an animal is eating a specific dish
        public void eat(String name, String dish) 
        {
            System.out.println(name + " is eating " + dish);
        }
    }
    Output:
    Jumbo has run 5 km  
    Jumbo is eating grass  
    Buzo is eating meat  
    Buzo has run 12 km

Program 6:
  • Below is the another program with instance variables and method parameter.
  • // Define a class named Animal6
    public class Animal6 
    {
        // Declare instance variables
        int no_of_eyes; // Variable to store the number of eyes
        String color; // Variable to store the color of the animal
    
        // Method to display the details of an animal
        public void details(String name) 
        {
            System.out.println("-------Details of " + name + "-------");
            System.out.println("Eyes : " + no_of_eyes);
            System.out.println("Color : " + color);
        }
    
        // Main method - program entry point
        public static void main(String[] args) 
        {
            // Create an object 'jumbo' of Animal6 and assign values
            Animal6 jumbo = new Animal6();
            jumbo.no_of_eyes = 2;
            jumbo.color = "Brown";
            jumbo.details("Jumbo"); // Call details method for 'jumbo'
    
            // Create another object 'buzo' of Animal6 and assign values
            Animal6 buzo = new Animal6();
            buzo.no_of_eyes = 2;
            buzo.color = "Black";
            buzo.details("Buzo"); // Call details method for 'buzo'
        }
    }
    Output:
    -------Details of Jumbo-------
    Eyes : 2
    Color : Brown
    -------Details of Buzo-------
    Eyes : 2
    Color : Black

Program 7: (Best Practice Program)
  • In above programs we have created main method in Animal class which is not good practice, we should always create main method in main class only, below is the program.
  • // Define a class named Animal7
    class Animal7 
    {
        // Method to display a running message
        void run() 
        {
            System.out.println("I'm running");
        }
    }
    
    // Define the main class MainApp7
    public class MainApp7 
    {
        // Main method - program entry point
        public static void main(String[] args) 
        {
            // Create an object 'buzo' of Animal7
            Animal7 buzo = new Animal7();
            buzo.run(); // Call the run method
        }
    }
    Output:
    I'm running

Program 8: (Best Practice Program)
  • Here we have created Animal8, Birds8 class with different methods and also created Main class as separate class which is good practice.
  • // Define a class named Animal8
    class Animal8 
    {
        // Method to display a running message
        void run() 
        {
            System.out.println("I'm running");
        }
    }
    
    // Define a class named Birds8
    class Birds8 
    {
        // Method to display a flying message
        void fly() 
        {
            System.out.println("I'm flying");
        }
    }
    
    // Define the main class MainApp8
    public class MainApp8 
    {
        // Main method - program entry point
        public static void main(String[] args) 
        {
            // Create an object 'buzo' of Animal8 and call the run method
            Animal8 buzo = new Animal8();
            buzo.run();
    
            // Create an object 'sparrow' of Birds8 and call the fly method
            Birds8 sparrow = new Birds8();
            sparrow.fly();
        }
    }
    Output:
    I'm running
    I'm flying