First lets understand what is Class, Methods & Objects in real world, below are some classes
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.
}
public class Animal
{
//fields
int eyes;
String color;
//method
void run()
{
//body
}
}
access-modifiers return-type methodName(List of Parameters)
{
// body
}
public void run(String name)
{
System.out.println(name+" is running");
}
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
.
objectName
now holds a reference to a new ClassName
object in memory.
objectName.methodName();
objectName.field_name;
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
}
}
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();
}
}
I'm running
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
}
}
I'm running I'm eating...!!
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
}
}
I'm running I'm eating...!! I'm eating...!! I'm running
// 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...!!");
}
}
Jumbo is running Jumbo is eating...!! Buzo is eating...!! Buzo is running
// 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);
}
}
Jumbo has run 5 km Jumbo is eating grass Buzo is eating meat Buzo has run 12 km
// 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'
}
}
-------Details of Jumbo------- Eyes : 2 Color : Brown -------Details of Buzo------- Eyes : 2 Color : Black
// 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
}
}
I'm running
// 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();
}
}
I'm running I'm flying
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.