What is class in java [what are classes in java]
To understand class in java, we fist have to check meaning of class as it's show on google "class is a set or category of things having some common properties".
Let's understand these with some examples
Example : #1
Let's discussed about the people who works in a company or organisation, we says these people Employee so they are in Employee class category.
Now let's understand how they are grouped to Employee class. These people have some common properties like Employee Id, Employee Name, Employee Company Name, Employee Designation and so on. All the people who works has these common properties so based on these properties they are grouped into a class called Employee.
Example : #2
In our Society people are divided into sets based on social or economical status i.e upper class, middle class, lower class, so we can understand we have grouped the people in our society according to their common attribute or properties. That group is collectively called class.
So class is nothing but concept and object are the actual instances of class, we grouped thing based on common properties of objects and called that group collectively as class.
In other words we can say that grouping objects based on their common properties form a category that is also called class.
Example :
here we have used keyword public followed by class followed by class name Employee. The public keyword means this Employee class can be used by other class.
Now let's see some of the properties and method of Employee class
We have defined a variable or properties namely "name" of type String and two method namely getName, and setName(String name).
Method : If we define Method, we can say methods are operation or function that can be called.
Here public is used with method says this method can be called from other classes.
Return Type : Now If we look at the getName method it is returning some value of String type on the other hand setName(String name) method has return type void, it means no value is returned from this method.
Parameter : If we look at the setName method it requires some information to be passed on by calling method, This information is called parameter setName method has one parameter named newName of type String. It means caller should pass one String parameter and expect nothing to be returned.
Method Signature : The full declaration of method is called method signature.
Example :
public int calculateCube(int number);
In this example the return type is int method is calculateCube parameter is number which is of int type.
class diagram for above class is as :
this class should be saved with the name Employee.java, because this class is public so your file name should match with your public class name in the file. otherwise there will be Compilation Error.
#2. You can have multiple class in your single file but only one class allowed to be public and that file should be saved with the name of that public class name.
Example :
In this example we have two class namely public Employee and Address so you must have to save this file with the name Employee.java otherwise there will be compilation error.
If you compile the file like javac Employee.java compiler will generate two file with the .class extension as Employee.class and Address.class It means each class has it's .class file once .java file is compiled.
#3. If your file does not have any public class, means classes from that file can not be used outside of the package but you can save your file with any name you want. once the class is compiled .class file is generated according to the class you have written in your file not like your file name.
In our Society people are divided into sets based on social or economical status i.e upper class, middle class, lower class, so we can understand we have grouped the people in our society according to their common attribute or properties. That group is collectively called class.
So class is nothing but concept and object are the actual instances of class, we grouped thing based on common properties of objects and called that group collectively as class.
In other words we can say that grouping objects based on their common properties form a category that is also called class.
Java class syntax
class is a keyword in java. to make java class we have to use this keywordExample :
public class Employee{
}
}
here we have used keyword public followed by class followed by class name Employee. The public keyword means this Employee class can be used by other class.
Now let's see some of the properties and method of Employee class
public class Employee{
private String name;
public String getName(){
return name;
}
public void setName(String newName){
name = newName;
}
}
private String name;
public String getName(){
return name;
}
public void setName(String newName){
name = newName;
}
}
We have defined a variable or properties namely "name" of type String and two method namely getName, and setName(String name).
Method : If we define Method, we can say methods are operation or function that can be called.
Here public is used with method says this method can be called from other classes.
Return Type : Now If we look at the getName method it is returning some value of String type on the other hand setName(String name) method has return type void, it means no value is returned from this method.
Parameter : If we look at the setName method it requires some information to be passed on by calling method, This information is called parameter setName method has one parameter named newName of type String. It means caller should pass one String parameter and expect nothing to be returned.
Method Signature : The full declaration of method is called method signature.
Example :
public int calculateCube(int number);
In this example the return type is int method is calculateCube parameter is number which is of int type.
class diagram for above class is as :
class diagram |
Classe Name Vs Files Name Important point
#1. The General convention says, you should have only one class in a single file.
Example :
public class Employee{
}
}
#2. You can have multiple class in your single file but only one class allowed to be public and that file should be saved with the name of that public class name.
Example :
public class Employee{
}
class Address {
}
}
class Address {
}
If you compile the file like javac Employee.java compiler will generate two file with the .class extension as Employee.class and Address.class It means each class has it's .class file once .java file is compiled.
#3. If your file does not have any public class, means classes from that file can not be used outside of the package but you can save your file with any name you want. once the class is compiled .class file is generated according to the class you have written in your file not like your file name.
No comments:
Post a Comment