Classes
A class in Java is a set of objects which shares common characteristics/ behavior and common properties/ attributes. It is a user-defined blueprint or prototype from which objects are created. For example, Student is a class while a particular student named Ravi is an object.
Properties
- Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created.
- Class does not occupy memory.
- Class is a group of variables of different data types and a group of methods.
- A Class in Java can contain:
- Data member
- Method
- Constructor
- Nested Class
- Interface
Declaration
Syntax:
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // Java Program for class example public class Student { // data member (also instance variable) int id = 001; // data member (also instance variable) String name = "Wrapline"; public static void main(String args[]) { // creating an object of // Student Student s1 = new Student(); System.out.println(s1.id); System.out.println(s1.name); } } |
Output:
1 2 | 001 Wrapline |
Components
- Modifiers: A class can be public or has default access.
- Class keyword: class keyword is used to create a class.
- Class name: The name should begin with an initial letter (capitalized by convention).
- Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
- Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
- Body: The class body is surrounded by braces, { }.
Objects
An object in Java is a basic unit of Object-Oriented Programming and represents real-life entities. Objects are the instances of a class that are created to use the attributes and methods of a class. A typical Java program creates many objects, which as you know, interact by invoking methods.
Components
- State: It is represented by attributes of an object. It also reflects the properties of an object.
- Behavior: It is represented by the methods of an object. It also reflects the response of an object with other objects.
- Identity: It gives a unique name to an object and enables one object to interact with other objects.
Ex: dog
Declaration
When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.
As we declare variables like (type name;). This notifies the compiler that we will use the name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variables , the type must be strictly a concrete class name. In general, we can’t create objects of an abstract class or an interface.
1 | Dog tuffy; |
If we declare a reference variable(tuffy) like this, its value will be undetermined(null) until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | // Class Declaration public class Dog { // Instance Variables String name; String breed; int age; String color; // Constructor Declaration of Class public Dog(String name, String breed, int age, String color) { this.name = name; this.breed = breed; this.age = age; this.color = color; } // method 1 public String getName() { return name; } // method 2 public String getBreed() { return breed; } // method 3 public int getAge() { return age; } // method 4 public String getColor() { return color; } @Override public String toString() { return ("Hi my name is " + this.getName() + ".\nMy breed,age and color are " + this.getBreed() + "," + this.getAge() + "," + this.getColor()); } public static void main(String[] args) { Dog tuffy = new Dog("tuffy", "papillon", 5, "white"); System.out.println(tuffy.toString()); } } |
Output:
1 2 | Hi my name is tuffy. My breed,age and color are papillon,5,white |
Ways to Create an Object
Using new keyword
It is the most common and general way to create an object in Java.
Ex:
1 2 | // creating object of class Test Test t = new Test(); |
Using Class.forName(String className) method
Ex:
1 2 3 | // creating object of public class Test // consider class Test present in com.p1 package Test obj = (Test)Class.forName("com.p1.Test").newInstance(); |
Using clone() method
Ex:
1 2 3 4 | // creating object of class Test Test t1 = new Test(); // creating clone of above object Test t2 = (Test)t1.clone(); |
Deserialization
Ex:
1 2 3 | FileInputStream file = new FileInputStream(filename); ObjectInputStream in = new ObjectInputStream(file); Object obj = in.readObject(); |
Anonymous Objects
Anonymous objects are objects that are instantiated but are not stored in a reference variable.
- They are used for immediate method calls.
- They will be destroyed after method calling.
- They are widely used in different libraries. For example, in AWT libraries, they are used to perform some action on capturing an event(eg a key press).
- In the example below, when a key button(referred to by the btn) is pressed, we are simply creating an anonymous object of EventHandler class for just calling the handle method.
Ex:
1 2 3 4 5 6 7 | btn.setOnAction(new EventHandler() { public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); |
Difference between Java Class and Objects
Class | Object |
---|---|
Class is the blueprint of an object. It is used to create objects. | An object is an instance of the class. |
No memory is allocated when a class is declared. | Memory is allocated as soon as an object is created. |
A class is a group of similar objects. | An object is a real-world entity such as a book, car, etc. |
Class is a logical entity. | An object is a physical entity. |
A class can only be declared once. | Objects can be created many times as per requirement. |
An example of class can be a car. | Objects of the class car can be BMW, Mercedes, Ferrari, etc. |