What is Class?
Answer:
Class is a user defined data type. It is a capability of object oriented programming by which user can make a user-defined data type that can works as a built in data type.There are some variable and some function exists in the class. By creating a class, the using variable in the class and the function behaves as a unit which is different from others.
Declaration of class:
class ClassName{
Member variable Declaration;
Member function definition;
}
class Result{
String subjectName;
int mark1 ;
int mark2 ;
double result( ){
return result = (mark1 + mark2) / 2;
}
}
Fig-01
What is Object?
Answer:
Class data type variable is called object. That means, object is like a variable which data type is class type.When we create class, compiler doesn’t create any memory allocation for the class. When we create an object of the class, compiler than create memory allocation for the objects member variable and the member methods or functions.
Here for every member variables compiler create memory allocation but for methods, compiler creates only one memory allocation.
Since all of the objects of a class use same method, there is no necessity to create memory allocation for that. But since every data type different from others, so different types of memory allocation is needed.
class Result{
String subjectName;
int mark1 = 80 ;
int mark2 = 84;
double resultExam( ){
return result = (mark1 + mark2) / 2;
}
}
class ResultTest{
public static void main(String[] args){
Result r = new Result( );
r.resultExam( );
}
}
Output
82
In the main method of the program “ResultTest” r is an object of Result class using new operator. Then r object get the resultExam( ) method. And finally output will be 82 which is defined in the result variable.
No comments:
Post a Comment