Classes are fundamental in the OOPS programming language. It's also known as a blueprint or template in Object Oriented Programming language and without class program doesn't exist.
Syntax:
class <class-name>{
};
class is the keyword to create a class in Java or other OOPS languages. A class can contains any number of data variables(attributes) and functions(methods). This data variable and methods can be accessed using the instance of the class which is known as an object. Objects are the instances of the class and it's represent the class.
Class basic terms,
Data variables: What the class looks like
Method: What the class can perform
Object: Represent the class
Constructor: Initialize the global vairable
To Understand class concepts in a better way here is an example, let's consider human as a class and you all know class contains variable, methods, and objects (used to access the methods and data variables). In the human class, the data variables are ears, hands, legs, and mouth and the methods are earing(), handshake(), and walk().
Now the object uses the data variable and methods to act like human anywhere just by calling. Simply class is a recipe that gives characteristics to an object. This is how programmers implement real-world entities as programs.
Java Class Example:
class Human{
Ear ears;
Hand hand;
Leg legs;
void earing(){
//method1
};
void handshake(){
//method2
};
void walk(){
//method3
};
public static void main(String args[]){
Human humanObject = new Human(); // object creation
humanObject.ears();
}
Class Data Variables
- Integer
- String
- Float
- Character
- Boolean