2025/05/08 6

제어자

제어자란 클래스나 멤버의 사용을 제어하기 위해서 사용됩니다. 제어자의 종류에는 외부에서 접근하는 것을 막는 접근제어자와 클래스 멤버를 의미하는 static, 상수를 만들거나 상속을 종결시키는 final 등이 있습니다. 1. 접근 제어자 접근 제어자는 외부에서 접근할수 있는 정도와 범위를 정해줍니다. 접근 제어자로는 허용범위가 넓은 순서대로 public, protected, default, private가 있습니다. 여기서 dafeult는 아무런 접근 제어자를 작성하지 않았을 경우를 말합니다. package chapter07;public class exam46 { public static void main(String[] args) { exam46_User user1 = new exam46_User("철..

상속과 생성자

자손 클래스의 생성자 작업을 할 때 부모 클래스의 생성자도 반드시 호출해주어야 합니다. package chapter07;public class exam43 { public static void main(String[] args) { exam43_SportsCar sportsCar1 = new exam43_SportsCar("red",330); System.out.println(sportsCar1.color); System.out.println(sportsCar1.speedLimit); }}package chapter07;public class exam43_Car2 { int wheel; int speed; String color; exam43_Car2() {}; exam43_Car2 (Str..

오버라이딩

오버라이딩은 자손 클래스에서 부모 클래스로부터 물려받은 메서드를 다시 작성하는 것을 말합니다. package chapter07;public class exam41 { public static void main(String[] args) { Leader leader1 = new Leader(); leader1.eat(); leader1.say(); }}package chapter07;public class exam41_Student { void learn() { System.out.println("배우기"); } void eat () { System.out.println("밥먹기"); } void say() { System.out.println("선생님 안녕하세요!"); }}package ch..

상속이란?

상속이란? 새로운 클래스를 작성할 때 기존에 존재하는 클래스를 물려받아 이용합니다. 기존의 클래스가 가진 멤버를 물려받기 때문에 새롭게 작성해야 할 코드의 양이 줄어드는 효과가 있습니다. 이때 자신의 멤버를 물려주는 클래스를 부모 클래스 또는 조상 클래스라고 하고 상속받는 클래스를 자식 클래스 또는 자손 클래스라고 합니다.package chapter07;public class Person { void breath() { System.out.println("숨쉬기"); } void eat() { System.out.println("밥먹기"); } void say() { System.out.println("말하기"); } } class Student extends Person { ..

생성자

생성할때 1. 인스턴스 변수 초기화2. 주소반환 기본생성자클래스 이름 () {}** 기본적으로 주소 return **package chapter05;public class Cellphone { String model = "Galaxy 8"; String color; int capacity; Cellphone (String color, int capacity) { // 매개변수가 있는 생성자 this.color = color; this.capacity = capacity; }}package chapter05;public class exam39 { public static void main(String[] args) { Cellphone myphone = new Cellphone("Silver", ..

JAVA/클래스 2025.05.08

오버로딩

오버로딩은 매개변수의 개수와 타입은 다르지만 이름이 같은 메서드를 여러 개 정의하는 것을 말한다 ** 같은 이름의 함수를 여러개 쓰는것 ** 매개변수개수와 데이터 타입에 따라 자동으로 알맞은 메서드가 호출 됩니다. package chapter05;public class exam38 { static int sum (int a, int b) { System.out.println("인자가 둘일 경우 호출됨"); return a+b; } static int sum (int a, int b, int c) { System.out.println("인자가 셋일 경우 호출됨"); return a+b+c; } static double sum (double a, double b, double c) { Syst..

JAVA/클래스 2025.05.08