What is Class in Java?
Class are a blueprint or a set of
instructions to build a specific type of
object. It is a basic concept of
Object-Oriented Programming, which revolve around the real-life entities. Class
in Java determines how an object will behave and what the object will contain.
• Classes combine data and the methods (code) to manipulate the data
• Classes are a template used to create specific objects
• All Java programs consist of at least one class.
• Two types of classes
– Application/Applet classes
– Service classes
Why Use Classes?
• Usually, the data for a program is not simply one item.
• Often we need to manage entities like students, books, flights, etc.
• We need to be able to manipulate such entities as a unit.
• Classes allow us to separate the data for each object, while using
common code to manipulate each object.
Syntax
class <class_name>{
attributes
method
}
Where attributes and method are the member of the class
A simple class example
Suppose, Student is a class and student's name, roll number, age are its fields and info() is a method. Then class will look like below.
class Student.
{
String name;
int rollno;
int age;
void info(){
}
}
|