Code States BE 부트캠프 #9

John Jun·2023년 4월 21일
0

INDEX


1. Class & Object (OOP)

2. Field & Method


1. Class & Object (OOP)

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.

  1. Basic Grammar:
	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.

  • We call the 3 elements except the constructor as the "Members."
  • Field and Method represents the class' state and function, which are the group of data related to 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.

  • How to make an 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();
    
    }

}
  • Modules1 to 3 are the instances—named as module1, module2, and module3(Those are the reference variable names that navigates the address where the data would be saved)—which are instantiated from the same class Module.
  • Module() is called the Constructor.
  • Every instances derived from the same class share the same methods and field so that we can simply approach to them—which is saved in the class—through (.) operator without restating the them.
    - There is how we use the field and methods after making the instance:

InstanceName.fieldName(VariableName) // calling the field
InstanceName.methodName() // calling the medthod

2. Field & Method

  • There are three types in Variables: Class variable, instance variable, and local variable.
  • Field refers to two types in Variables: Class variable and instance variable.

1. Class variable:

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

2. Instance variable:

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

3. static keyword

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.

  • static keyword makes it possible to share the variables or methods between other Objects(instances); it shares the common values or function between the instances where are using them.
  • static method cannot use instance variables or instance methods; it is because it is not clear that at the moment we use the static method, the instance methods or variables are also already made or not.

4. Methods

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.

5. Method Overloading

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:

1. 생성자
2. 내부 클래스
profile
I'm a musician who wants to be a developer.

0개의 댓글