JAVA CLASS AND OBJECTS (OOP 1) |
1) Classes in Java:
A class is a blueprint for creating objects. It defines the data and behavior of the objects that are instances of the class. In Java, a class is defined using the "class" keyword, followed by the name of the class. The class definition consists of variables and methods, and it is a template for creating objects. Variables are used to store data, and methods are used to perform operations on that data.A Java class is defined using the following syntax:
class ClassName { // Variables (also known as fields or member variables) type variable1; type variable2; ... // Methods (also known as member functions) type method1(parameters) { // Method implementation } type method2(parameters) { // Method implementation } ...
}
- ClassName is the name of the class. Class names in Java follow naming conventions where the first letter of each word in the class name is capitalized.
- type is the data type of the variable or the return type of the method. For example, int, String, double, char, etc.
- variable1, variable2, etc. are the member variables of the class. They store the data for the class.
- method1, method2, etc. are the member functions or methods of the class. They define the behavior of the class.
- parameters are the input parameters that can be passed to the method. They can be of any data type or combination of data types.
It's important to note that the class definition is only a blueprint for creating objects. You need to create objects using the new operator and access the members of the class using the object reference.
For example, consider the following class definition:
class Student { String name; int age; void display() { System.out.println("Name: " + name); System.out.println("Age: " + age); } }
2) Objects in Java:
An object in Java is an instance of a class, which is a blueprint for creating objects. Objects contain their own set of variables and methods, which are defined in the class. An object represents a real-world entity and encapsulates its state and behavior.
To create an object in Java, you use the new operator, followed by the name of the class and parentheses:
ClassName objectName = new ClassName();
For example, if you have a class named Student:
class Student { String name; int age; void display() { System.out.println("Name: " + name); System.out.println("Age: " + age); } }
You can create an object of the class Student as follows:
Student s1 = new Student();
Once you have created an object, you can access its variables and methods using the dot (.) operator and the object reference:
s1.name = "John"; s1.age = 22; s1.display();
In this example, the object s1 has its own copy of the variables name and age, and it also has access to the method display.
class Dog { String breed; int age; String name; void bark() { System.out.println("Woof! Woof!"); } void displayDogDetails() { System.out.println("Breed: " + breed); System.out.println("Age: " + age); System.out.println("Name: " + name); } } public class Main { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.breed = "Labrador"; dog1.age = 5; dog1.name = "Max"; dog1.displayDogDetails(); dog1.bark(); Dog dog2 = new Dog(); dog2.breed = "Poodle"; dog2.age = 3; dog2.name = "Lucy"; dog2.displayDogDetails(); dog2.bark(); } }
In this example, the class Dog defines the data (breed, age, name) and behavior (bark, displayDogDetails) of a dog. The class Main creates two objects of the class Dog, dog1 and dog2. Each object has its own set of variables, and they access the methods of the class to display their details and bark.
The output of this program will be:
Breed: Labrador Age: 5 Name: Max Woof! Woof! Breed: Poodle Age: 3 Name: Lucy Woof! Woof!
This example demonstrates the use of classes and objects in Java. The class Dog defines the blueprint for creating objects, and the objects dog1 and dog2 are instances of the class Dog. Each object has its own set of variables and accesses the methods of the class to display its details and bark.
In conclusion, classes and objects are an integral part of Java programming. A class acts as a blueprint for creating objects, defining the data and behavior of objects. Objects are instances of a class, and each object has its own set of variables and methods. Understanding the concepts of classes and objects is essential for writing efficient and effective Java programs. With this knowledge, you can write well-structured programs that encapsulate data and behavior, making your code more modular, maintainable, and reusable. I hope this blog has provided a clear understanding of the concepts of Java classes and objects and has given you the foundation to start writing your own Java programs. Happy coding!