Java Class and Object: Fundamental Concepts Explained
Table of Content:
Class and Object
Classes and objects 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.
First, let's understand what they are,
What is Class in Java
A class is an entity that determines how an object will behave and what the object will contain. In other words, it is a blueprint or a set of instruction to build a specific type of object.
A class in Java can contain:
- fields
- methods
- constructors
- blocks
- nested class and interface
Syntax to declare a class:
class Class_name {
field or variable ;
method like main() and others ;
}
class Class_name {
field or variable ;
method like main() and others ;
}
What is an Object
An object is nothing but a self-contained component which consists of methods and properties to make a particular type of data useful. Object determines the behavior of the class. When you send a message to an object, you are asking the object to invoke or execute one of its methods.
From a programming point of view, an object can be a data structure, a variable or a function. It has a memory location allocated. The object is designed as class hierarchies.
What is the difference between Object & class?
A class is a blueprint or prototype that defines the variables and the methods (functions) common to all objects of a certain kind.
An object is a specimen of a class. Software objects are often used to model real-world objects you find in everyday life.
Concept of class, Object and methods
You can see the picture of three different breeds of dogs below. Here Dog is a class and three different type of breeds are called as three
different objects like :
Dog1Object
Dod2Object
Dog3Object
List down the differences between them. Some of the differences you might have listed out may be breed, size, color, age etc. If you think for a minute, these differences are also some common characteristics shared by these dogs. These characteristics (breed, size, color, age) can form a fields or data members for your object.
Next, list out the common behaviors of these dogs like eat, run sleep, name etc. So these will be the actions for our objects. These behavoiurs(eat, run sleep, name) is called as methods.
So far we have defined following things:
- Class - Dogs
- Data members - breed, size, color, age etc.
- Methods- eat(), run(), sleep(), name().
Sample Program for the above concept
public class Dog {
// instance variable
String breed;
String size;
String color;
int age;
public static void main(String[] args) {
// create object here
Dog Dog1Object = new Dog();
Dog Dog2Object = new Dog();
Dog Dog3Object = new Dog();
// Data input for Dog Object 1
Dog1Object.breed = "Beagle";
Dog1Object.size = "Large";
Dog1Object.color = "Light Gray";
Dog1Object.age = 5;
// Data input for Dog Object 2
Dog2Object.breed = "Buldog";
Dog2Object.size = "Large";
Dog2Object.color = "Orange";
Dog2Object.age = 6;
// Data input for Dog Object 3
Dog3Object.breed = "German Shepherd";
Dog3Object.size = "large";
Dog3Object.color = "white and Orange";
Dog3Object.age = 6;
// print all data from objects
System.out.println("Dog Object 1: \n Breed: "+Dog1Object.breed+"\n Size: "+Dog1Object.size+"\n Color: "+Dog1Object.color+"\n Age: "+Dog1Object.age);
System.out.println("Dog Object 2: \n Breed: "+Dog2Object.breed+"\n Size: "+Dog2Object.size+"\n Color: "+Dog2Object.color+"\n Age: "+Dog2Object.age);
System.out.println("Dog Object 3: \n Breed: "+Dog3Object.breed+"\n Size: "+Dog3Object.size+"\n Color: "+Dog3Object.color+"\n Age: "+Dog3Object.age);
}
}
Output
Dog Object 1: Breed: Beagle Size: Large Color: Light Gray Age: 5 Dog Object 2: Breed: Buldog Size: Large Color: Orange Age: 6 Dog Object 3: Breed: German Shepherd Size: large Color: white and Orange Age: 6
Practical Approach of Class and Object
main() Method within class
In this example, we have created a Student class that have two data members name and rollNo.
We are creating the object of the Student class by new keyword and printing the objects value.
Here, we are creating main() method inside the class.
class Student{
String name; //field or data member or instance variable
int rollNo;//field or data member or instance variable
public static void main(String args[]){
Student s1=new Student();//creating an object of Student
System.out.println(s1.name); //accessing member through reference variable
System.out.println(s1.rollNo);//accessing member through reference variable
}
}
Output:
null 0 Press any key to continue . . .
It gives null and 0 output because we know that default value number is 0 and for object references it is null.
main() Method outside class
We create classes and use it from another class using instance of that class(object).
It is a better approach than previous one. Let's
see a simple example, where we are having main() method in another class.
We can have multiple classes in different java files or single java file. If you define multiple
classes in a single java source file, it is a good idea to save the file name with the class
name which has main() method.
// this is a different class
class Student{
String name; //field or data member or instance variable
int rollNo;//field or data member or instance variable
}
//this is main class
class MainClass{
public static void main(String args[]){
Student s1=new Student();//creating an object of Student
System.out.println(s1.name); //accessing member through reference variable
System.out.println(s1.rollNo);//accessing member through reference variable
}
}
Output:
null 0 Press any key to continue . . .
It gives null and 0 output because we know that default value number is 0 and for object references it is null.
Initialize object
There is three Ways to initialize object
- By reference variable
- By method
- By constructor
Initialization through reference
class Student{
String name; //field or data member or instance variable
int rollNo;//field or data member or instance variable
}
class MainClass{
public static void main(String args[]){
Student s1=new Student();//creating an object of Student
s1.name = "Ramboo"; //Initialization through reference
s1.rollNo = 21; // Initialization through reference
System.out.println(s1.name+"'s Roll No: "+s1.rollNo);
}
}
Output:
Ramboo's Roll No: 21 Press any key to continue . . .
Initialization through method
class Student{
String name; //field or data member or instance variable
int rollNo;//field or data member or instance variable
void methodforInti(String s, int r)
{
name = s;
rollNo = r;
}
public static void main(String args[]){
Student obj1=new Student();
obj1.methodforInti("Ramboo",21);
System.out.println(obj1.name+"'s Roll No: "+obj1.rollNo);
}
}
Output:
Ramboo's Roll No: 21 Press any key to continue . . .
Initialization and dispaly through method:
class Student{
String name; //field or data member or instance variable
int rollNo;//field or data member or instance variable
void methodforInti(String s, int r)
{
name = s;
rollNo = r;
}
void methodforDisplay()
{
System.out.println(name+"'s Roll No: "+rollNo);
}
public static void main(String args[]){
Student obj1=new Student();
obj1.methodforInti("Ramboo",21);
obj1.methodforDisplay();
}
}
Output:
Ramboo's Roll No: 21 Press any key to continue . . .
Initialization through constructor
class Student{
String name; //field or data member or instance variable
int rollNo;//field or data member or instance variable
Student(String s, int r) // this is a constructor
{
name = s;
rollNo = r;
}
void methodforDisplay()
{
System.out.println(name+"'s Roll No: "+rollNo);
}
public static void main(String args[]){
Student obj1=new Student("Ramboo",21);
obj1.methodforDisplay();
}
}
Output:
Ramboo's Roll No: 21 Press any key to continue . . .
- Question 1: What do you mean by synchronized Non Access Modifier?
- Question 2: Can a class declared as private be accessed outside it's package?
- Question 3: What is an object's lock and which object's have locks?
- Question 4: What are order of precedence and associativity and how are they used?
- Question 5: If a method is declared as protected, where may the method be accessed?
- Question 6: What is the difference between inner class and nested class?
- Question 7: What will be the default values of all the elements of an array defined as an instance variable?
- Question 8: How will you define a software object?
- Question 9: Define class and object with an example.
- Question 10: Why is a class known as composite data type?
- Question 11: Can a vector contain heterogenous objects?
- Question 12: What is the SimpleTimeZone class?
- Question 13: What is Encapsulation?
- Question 14: Define composition?
- Question 15: What is final class?
- Question 16: What is the purpose of the System class?
- Question 17: Which class is the immediate superclass of the Container class?
- Question 18: What is the default value of an object reference declared as an instance variable?
- Question 19: What is the immediate super class of Menu?
- Question 20: What is Nested top-level class?
- Question 21: What is the GregorianCalendar class?
- Question 22: (iii) Write the Java statement for creating an object named 'sifra' of the class 'Robot', which takes three double parameters.
Related Questions
- Assignment 1: Class and Object
- Assignment 2: Object and Class Example: main within class
- Assignment 3: Object and Class Example: main outside class
- Assignment 4: Class and Object Example: Initialization through reference
- Assignment 5: create multiple objects and store information in it through reference variable.
- Assignment 6: Class and Object Example: Initialization through method
- Assignment 7: Class and Object Example: Initialization through constructor
- Assignment 8: Class and Object Example: Area of Rectangle
- Assignment 9: ways to create an object in Java Example 1
- Assignment 10: ways to create an object in Java Example 2
- Assignment 11: Creating multiple objects by one type only
- Assignment 12: Example for understanding class object and method
- Assignment 13: Instance variable and static variable comparison using program
- Assignment 14: Instance variable and static variable comparison using program
- Assignment 15: Instance variable and static variable comparison using program