Welcome back to OOP!
Today we delved deeper into OOP and learn some interesting things on how they work!!
Classes
there are two ways to form classes!
- creating it in the same file that you will be running your code in
- creating a seperate java file!
The difference is whether or not you'll be using public!
it is a keyword that declares that member's access as public! this means it is visible to all other classes.
This also means that if something is set to public, even different java files from different packages can reach in and call it!! (so cool!!)
class Calc {
// 속성 -> 인스턴스 변수
// 생성자()
// 기능 -> 메소드()
// return을 알아서 처리할땐 void
public int add(/* parameter */ int num1, int num2) {// 매개변수: 메소드가 구동될때 사용할 변수들
// 매개변수는 가상으로 적어준다
System.out.println(num1 + num2);
return num1 + num2;
}
}
public class OOP01 {
public void add(int num1, int num2) {
}
public static void main(String[] args) {
int num;
// add(1, 2);
//
// Calc.add(1, 2);
//인스턴스 생성
//데이터 타입 변수명 : 값;
Calc calc = new Calc() ;
//호출 인스턴스명.변수명 인스턴스명.메소드명().
calc.add(1, 3);
String str = "Hi"; // r-type
}
}
that means there is also private access (oooOOoooH)
Private means it's only accessed in the same class, if ideclared outside it is not reachable!!!
can use getter/setter to create a method that will access the private information when its called and pass it through, like a key!
here's an example of private fields:
private String name;
private int age;
to access this from outside the class it is declared it we use getter/setter that looks like this!
The method names can be changed but java creates a placeholder name for us here which is getName and setName:
public String getName() {//name값 얻어오기
return name;
}
public void setName(String name) { //setname 지정하기
this.name = name;
}
Constructors
Constructors are the actual code that creates an object when a Class gets objectified!
Constructors also can:
1.) create objects from Classes
2.) assign values (initialization) to fields
3.) initiate i nstance initialization method if needed!
Therefore we cannot create an object from class without a contructor!!
Constructors forms are similar to a method, however there are differences in terms of technicality.
1.) The name of the constructor must be the same of the class, if not it will be percieved as a method
2.) Since it is not a method, there are no return types nor returns
3.) If there are no contructors in a class, java will automatically form it for you => default constructor
Default constructors
When compiling, Java will automatically form a default constructor for each class!!
However if there is one already present, then it will forgo its creation. Default constructors are only formed when there are absolutely no constructors present in the class (important!!!)
So what do default constructors look like..?
- no parameters
- instances that are formed through these are called basic objects
- all field values will be reset to its original value
Constructor / Method signatures
Now methods and constructors are cool in a sense that they can have the same names but be differenciated by the data types and paramenters they receive when being called while being overloaded!
The qualities we use to differenciate these is called a method/ constructor signature!
but how do they know if they are different or the same?
When the name is the same, the compiler checks the signature of the member being called and uses the constructor/method that matches!
To define different signatures but using the same name of the method/constructor then calling it is called overloading!!
We do not call it overloading if the name of the parameter is different but the its type, order and number is the same. The example below will therefore raise an error:
int divide(int x, int u) {...}
int divide(int r, int m) {...}
It also doesn't count if the return type is different but the parameter is the same as JVM doesn't check the return type when selecting the methods.
methods or constructors can only be the same if they share the same signature, even if one of them is different that will count as two different constructor/methods!!
.this( ): the method that calls a different constructor that is defined in the same class
We call this by following the paramaters of the constructor we like to call and it automatically gets applied.
super duper useful little guy!
Methods
good old building blocks we can use with our constructors!!
here's a structural overview of a method:
Access, returnType, method name,( parameter ) { method body start
command;
command;
command;
} method body end
return type: voids
If we choose to set the return type of a method as void, there will be no return!
return type: data types (int, char, string, array)
If we choose to set the return type as a data type, it will return the result in that data type
lets look at this example here:
public void add() {
System.out.println("저장하는 메소드");
}
int result = apple4.add();
System.out.println( apple4.add() );
we will not be able to excecute both above, we will have to get rid of void and define the datatype (both primitive and reference) before returning a value in said datatype.
4 Pillars of OOP
We've very briefly touched upon the 4 main parts of Object Oriented Programming. Here are their names and definitions. We will be looking into these in depth as our class progresses.
- Encapsulation (protection and security)
It encapsulates all related data (fields) and functions(methods) into one bundle - Inheritance
Child classes can inherit all fields and methods from its parent class and use them!This leads to code recycling where the child receives classes already formed in the parent (this is called extending). When there is inheritance through class, you can only have one generation of inheritance, there can be no grandchilden. - Abstraction
This means to get rid of superfluous parts and to maintain the ones in need. When creating instances only the important parts are kept and the rest is disposed. - Polymorphism
When the action of an object is changed or when an action is effected and altered based on its parameters or circumstances.
'JAVA' 카테고리의 다른 글
| Interface and Exceptions (0) | 2024.07.02 |
|---|---|
| Recap of Access Modifiers and deep dive into 4 pillars of OOP (0) | 2024.07.01 |
| OOP continued: Static, access modifiers, Singleton pattern (0) | 2024.06.29 |
| JAVA: Deep and Shallow Copying, String Class Methods (0) | 2024.06.26 |
| JAVA: creating a JSP page using Apache, 2D Arrays part. 2, OOP fundamentals (0) | 2024.06.25 |