본문 바로가기

Java

(9)
[JAVA] abstract와 반대되는 final의 용도 변수에서의 final 123456789101112131415161718192021222324252627282930313233package a; class Calculator { static final double PI = 3.14; int left, right; public void setOprands(int left, int right) { this.left = left; this.right = right; //Calculator.PI = 6; //이 부분의 주석을 풀게 되면 에러가 발생한다. final 이 붙으면 해당 변수의 값을 변경할 수 없다. } public void sum() { System.out.println(this.left + this.right); } public void avg() ..
[JAVA] 추상클래스의 상속 12345678910111213141516package org.opentutorials.javatutorials.abstractclass.example2;abstract class A{ public abstract int b(); public void d(){ System.out.println("world"); }}class B extends A{ public int b(){return 1;} //이와같이 재정의 하지 않으면 사용할 수 없다.}public class AbstractDemo { public static void main(String[] args) { B obj = new B(); System.out.println(obj.b()); }}Colored by Color Scriptercs abs..
JAVA의 접근제어자 표 정리 public protected default private 같은패키지,같은 클래스 허용 허용 허용 허용 같은패키지,상속관계 허용 허용 허용 불용 같은패키지,상속관계아님 허용 허용 허용 불용 다른패키지,상속관계 허용 허용 불용 불용 다른패키지,상속관계아님 허용 불용 불용 불용
[JAVA] 오버로딩(overloading) 예제 12345678910111213141516171819202122232425262728293031323334353637383940414243package a; class Calculator{ int left, right; int third = 0; public void setOprands(int left, int right){ System.out.println("setOprands(int left, int right)"); this.left = left; this.right = right; } public void setOprands(int left, int right, int third){ System.out.println("setOprands(int left, int right, int third)")..
[JAVA] 오버라이딩(overriding) 예제 123456789101112131415161718192021222324252627282930313233343536373839package a class Calculator { int left, right; public void setOprands(int left, int right) { this.left = left; this.right = right; } public void sum() { System.out.println(this.left + this.right); } public void avg() { System.out.println((this.left + this.right) / 2); }} class SubstractionableCalculator extends Calculator { publ..
[JAVA] 생성자와 상속 1234567package a; public class ConstructorDemo { public static void main(String[] args) { ConstructorDemo c = new ConstructorDemo(); }}Colored by Color Scriptercs 위의 예제는 에러를 발생시키지 않는다. ConstructorDemo 객체를 생성할 때 자동으로 생성자를 만들어주기 때문이다 12345678package a; public class ConstructorDemo { public ConstructorDemo(int param1) {} public static void main(String[] args) { ConstructorDemo c = new ConstructorDe..
[JAVA] 상속에 대한 기본 예제 1234567891011121314151617181920212223242526272829303132333435363738package a; class Calculator { int left, right; public void setOprands(int left, int right) { this.left = left; this.right = right; } public void sum() { System.out.println(this.left + this.right); } public void avg() { System.out.println((this.left + this.right) / 2); }} class SubstractionableCalculator extends Calculator {//exte..
[JAVA]변수의 유효범위 예제 1234567891011121314151617181920 package org.opentutorials.javatutorials.scope; public class ScopeDemo6 { static int i = 5; static void a() { int i = 10; b(); } static void b() { System.out.println(i); } public static void main(String[] args) { a(); } }Colored by Color Scriptercs 변수의 유효범위에 대한 예제입니다. 해당 소스를 실행 했을때 출력되는 숫자는 5일까요 10일까요?출처:https://opentutorials.org