Java Tutorials
JVM | What is Java Virtual Machine & its Architecture
What is JVM? Java Virtual Machine (JVM) is a engine that provides runtime environment to drive the Java...
Classes and Objects in Java are the fundamental components of OOP's. Often there is a confusion between classes and objects. In this tutorial, we try to tell you the difference between Class and Object in Java.
First, let's understand what they are,
Syntax
class <class_name>{
field;
method;
} Object is an instance of a class. An object in OOPS is nothing but a self-contained component which consists of methods and properties to make a particular type of data useful. For example color name, table, bag, barking. When you send a message to an object, you are asking the object to invoke or execute one of its methods as defined in the class.
From a programming point of view, an object in OOPS can include a data structure, a variable, or a function. It has a memory location allocated. Java Objects are designed as class hierarchies.
Syntax
ClassName ReferenceVariable = new ClassName();
A Class in object oriented programming is a blueprint or prototype that defines the variables and the methods (functions) common to all Java Objects of a certain kind.
An object in OOPS is a specimen of a class. Software objects are often used to model real-world objects you find in everyday life.
Let's take an example of developing a pet management system, specially meant for dogs. You will need various information about the dogs like different breeds of the dogs, the age, size, etc.
You need to model real-life beings, i.e., dogs into software entities.
Moreover, the million dollar question is, how you design such software?
Here is the solution-
First, let's do an exercise.
You can see the picture of three different breeds of dogs below.
Stop here right now! List down the differences between them.
Some of the differences you might have listed out maybe breed, age, size, color, etc. If you think for a minute, these differences are also some common characteristics shared by these dogs. These characteristics (breed, age, size, color) can form a data members for your object.
Next, list out the common behaviors of these dogs like sleep, sit, eat, etc. So these will be the actions of our software objects.
So far we have defined following things,
Now, for different values of data members (breed size, age, and color) in Java class, you will get different dog objects.
You can design any program using this OOPs approach.
While creating a class, one must follow the following principles.
// Class Declaration
public class Dog {
// Instance Variables
String breed;
String size;
int age;
String color;
// method 1
public String getInfo() {
return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is: "+color);
}
public static void main(String[] args) {
Dog maltese = new Dog();
maltese.breed="Maltese";
maltese.size="Small";
maltese.age=2;
maltese.color="white";
System.out.println(maltese.getInfo());
}
} Output: Breed is: Maltese Size is:Small Age is:2 color is: white
In previous program, we are creating main() method inside the class. Now, we create classes and define main() method in another class. This is a better way than previous one.
// Class Declaration
class Dog {
// Instance Variables
String breed;
String size;
int age;
String color;
// method 1
public String getInfo() {
return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is: "+color);
}
}
public class Execute{
public static void main(String[] args) {
Dog maltese = new Dog();
maltese.breed="Maltese";
maltese.size="Small";
maltese.age=2;
maltese.color="white";
System.out.println(maltese.getInfo());
}
} Output:
Breed is: Maltese Size is:Small Age is:2 color is: white
Summary:
What is JVM? Java Virtual Machine (JVM) is a engine that provides runtime environment to drive the Java...
What is User Defined Exception in Java? User Defined Exception or custom exception is creating your...
What is a Prime Number? A prime number is a number that is only divisible by 1 or itself. For...
Java is a programming language and a computing platform for application development. It was first...
What is Command Line Argument in Java? Command Line Argument in Java is the information that is...
What is Exception in Java? Exception in Java is an event that interrupts the execution of program...