Object is literally actual thing that we can use for building up the program, and Class is the frame or blueprint that defines the object, which means Class is used to produce the objects, and the objects is created as the class defines and designs.
Most importantly, Class is not an actual thing but the frame to produce the objects.
In easy words, Class is the blueprint, and the certain object(Instance in the other word)—which is made following the class—is the house. In other words, Class is the frame and, the object that is made through the frame is called Instance through the process called "Instantiate."
The most powerful advantage of this programming system(OOP) is that you can produce thousands of instances through building up the well organized blueprint: Class.
Class Name(Capitalized) {
//contents:
int i = 0; // field
void doSomthing() {...} // method
class InnerClass {...} // inner class
}
Class is organized with 4 elements: Field, Method, Constructor, and Inner Class.
(1) field: defines "States" through Variables, which are used in the class.
(2) method: dfines the "Functions"
(3) constructor: makes the "Instances" of certain class.
(4) inner class: the "Classes" inside the class.
As we talked about previously, Instance(Object) is the actual production which is produced by Class.
Instance is also organized with states and functions; generally one instance is made up with various but similar states(Variables) and functions(Methods), and we call those states and functions—including inner classes—as the members of the Instance.
"new" keyword and "point operator(.)":
We can make an Instance of the certain class using new keword, and we can approach to use the Instance' members(Variables & Methods) with the point operater(.).
class Modulers {
public staitc void main(String[] args){
Module module1 = new Module();
Module module2 = new Module();
Module module3 = new Module();
}
}
InstanceName.fieldName(VariableName) // calling the field
InstanceName.methodName() // calling the medthod
CV is the variable we can use anytime withour making the instance of the class that the variable is stated. We can approach and use it like this: "ClassName.variableNameInTheClass"
- Located in the Class area
- static keyword
- Usable anytime
IV is the variables we can use after instantiating the instance of the class that inclues the instance varioable. In other words, it is the variable that is created when the instance of the class is made.
We can approach and use it like this: "instacneName.variableName"
- Located in the Class area
- No static keyword
- Usable when the instance is made
Static keyword is used for Class Members: field, method, and inner class. The members with static keyword is called static member, and they are usable without making instance of the class.
Method refers to the group of codes that has a functional role in class: functions.
Method is organize with two parts: head and body
Take a look this code:
public static int substractNums(int x, int y) //<1> {
int result = x - y; // <2>
return result;
}
<1>: The first part, head is also called method signature, and it consists of Java modifiers(public & static) - returning type - methodName - parameters ins ().
<2>: The second part, body{}, contains the codes, including local variables, that accomplish the certain functions.
methodName should be lower-cased to be distinguished from ClassName.
If we do not want to declare the returning type of the method, put 'void' instead of type keword, such as "int."
If the returning type is declared in the method, we must state 'return' in the body of the method.
We can also use the method of other classes with first, making the instance of the class, second putting the (.) after the instanceName, and last, stating the methodName to use with ();.
The values we putting inside the () when we call the method, to send them to the method as a parameters, is called arguments. They must be the same to the parameters in its types and order.
Method Overloading is the way stating multiple methods having same methodName, in the same Class.
Method Overloading can be made with stating the same methodName and different parameter types or numbers.
The most significant reason why we do overload the method is that we can accomplish various functions with one method, escaping making new methods with different name but the same/similar function.
Next tasks: