자바의 클래스 유형
콘크리트 수업
추상 메서드가 없는 일반 클래스나 부모 클래스 또는 인터페이스의 모든 메서드 구현 및 자체 메서드가 있는 클래스는 구체 클래스입니다.
예
public class Concrete { // Concrete Class
static int product(int a, int b) {
return a * b;
}
public static void main(String args[]) {
int p = product(2, 3);
System.out.println("Product: " + p);
}
} 출력
Product: 6
추상 클래스
abstract 키워드로 선언되고 0개 이상의 추상 메소드가 있는 클래스를 추상 클래스라고 합니다. 추상 클래스는 불완전한 클래스이므로 사용하려면 추상 클래스를 구체적인 클래스로 확장해야 합니다.
예
abstract class Animal { //abstract parent class
public abstract void sound(); //abstract method
}
public class Dog extends Animal { //Dog class extends Animal class
public void sound() {
System.out.println("Woof");
}
public static void main(String args[]) {
Animal a = new Dog();
a.sound();
}
} 출력
Woof
최종 수업
final 키워드로 선언된 클래스는 final 클래스이며 다른 클래스(예:java.lang.System 클래스)에서 확장할 수 없습니다.
예
final class BaseClass {
void Display() {
System.out.print("This is Display() method of BaseClass.");
}
}
class DerivedClass extends BaseClass { //Compile-time error - can't inherit final class
void Display() {
System.out.print("This is Display() method of DerivedClass.");
}
}
public class FinalClassDemo {
public static void main(String[] arg) {
DerivedClass d = new DerivedClass();
d.Display();
}
} 위의 예에서 DerivedClass는 BaseClass(final)를 확장합니다. , 최종 클래스를 확장할 수 없으므로 컴파일러에서 오류가 발생합니다. . 위의 프로그램은 그렇지 않습니다. 실행 .
출력
cannot inherit from final BaseClass Compile-time error - can't inherit final class
POJO 클래스
개인 변수와 이러한 변수를 사용하기 위한 setter 및 getter 메서드만 포함하는 클래스를 POJO(Plain Old Java Object) 클래스라고 합니다. 완전히 캡슐화된 클래스입니다.
예
class POJO {
private int value=100;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
public class Test {
public static void main(String args[]){
POJO p = new POJO();
System.out.println(p.getValue());
}
} 출력
100
정적 클래스
정적 클래스는 중첩된 클래스라는 의미는 다른 클래스 내에서 정적 멤버로 선언된 클래스를 정적 클래스라고 합니다.
예
import java.util.Scanner;
class staticclasses {
static int s; // static variable
static void met(int a, int b) { // static method
System.out.println("static method to calculate sum");
s = a + b;
System.out.println(a + "+" + b); // print two numbers
}
static class MyNestedClass { // static class
static { // static block
System.out.println("static block inside a static class");
}
public void disp() {
int c, d;
Scanner sc = new Scanner(System.in);
System.out.println("Enter two values");
c = sc.nextInt();
d = sc.nextInt();
met(c, d); // calling static method
System.out.println("Sum of two numbers-" + s); // print the result in static variable
}
}
}
public class Test {
public static void main(String args[]) {
staticclasses.MyNestedClass mnc = new staticclasses.MyNestedClass(); // object for static class
mnc.disp(); // accessing methods inside a static class
}
} 출력
static block inside a static class Enter two values 10 20 static method to calculate sum 10+20 Sum of two numbers-30
이너 클래스
다른 클래스나 메서드 내에서 선언된 클래스를 내부 클래스라고 합니다.
예
public class OuterClass {
public static void main(String[] args) {
System.out.println("Outer");
}
class InnerClass {
public void inner_print() {
System.out.println("Inner");
}
}
} 출력
Outer