What is class / What is class in java
Java Basic Understanding |
A general meaning
of class is set or category of things having some property or
attribute in common and differentiated from other by kind, type or
quality.
Example
: In our Society
people are divided into sets based on social or economical status i.e middle class, upper class, lower class, so we can understand we have grouped the objects according to their common attribute or
properties. That group is collectively called class.
Example of class like :
Employee : All the people who works, is grouped to employee class
they have some common properties like employee id, company name,
designation, salary and so on.
Or
According to oracle A class is the blueprint from which individual
objects are created.
Lets understand this by example
class
Employee{
int employeeId
Employee(int employeeId, String empName){
this.employeeId = employeeId;
this.empName = empName;
}
}
Here Employee is a class which has two properties employeeId and
empName.
Now
we have to understand how me being an
employee will be instance of this class.
I
am an Object (Material things that can be seen or touched) and i do work in company so i belong to employee class.
To represent me as an employee in programming language we have to
create object/instance of Employee class.
let's suppose my employeeId = 123, and
empName = “Santosh” .
Employee emp = new Employee(123, “Santosh”);
Here
emp is called Object
/ Instance of Employee class and this will represent a single
employee whose employeeId = 123 and empName = “Santosh”.
Structure of class :
The
class is keyword in java, To make the class we have to use syntax
as
class <className>{
properties
behaviour
}
Example
class
Employee{
// properties
int employeeId;
String empName;
void doWork(){
System.out.print(“Employee behaviour is to do work”);
}
}
To
run any class independently there must be main method in that class
or
If the class does not have main method, to run method of that class some other class having main method call/run method
with the object of that class like
class Company{
public static void main(String args[]){
Employee emp1 = new Employee(12, “Santosh”);
Employee emp2 = new Employee(13, “Raj”);
emp1.doWork();
emp2.doWork();
}
}
This Company class has two Employee emp1 and emp2 and both are doing
their work as their behaviour.
generate two .class file
One for Employee that is Employee.class and other for Company that is
Company.class.
When we run Company class like java Company, jvm will invoke it’s
main method and main method will create two employee and
employee doWork method will be
called.
Here are some very important one liner about class
There Should be single java class in a file:
This is convention that we should have single java class in a file, although you can make multiple java class in a file but that is not recommended.
Only one public class allowed in a file :
You can make multiple java class in a single .java file but you are allowed to make only one public class in a single java file.
File name should be same as class name :
Your java class file name should be similar to your public class name, you can save your java file with different name also.Example : Your java file name is Employee.java and your class name is Empclass Emp{}You have to compile your class with javac Employee.java that will generate Emp.class file (because your .class file is generated with the class name). Now you have to run this class file like java Emp.classso you are compiling Employee.java and executing Emp.class that is not good practice, it’s recommended that their should be single public class in your file and that file too save with the class name itself.Example}this class should be save as Emp.java
To be continue.....