logo

Java의 추상 메소드

객체 지향 프로그래밍에서 추상화는 사용자에게 불필요한 세부 사항(구현)을 숨기고 필수 세부 사항(기능)에 초점을 맞추는 것으로 정의됩니다. 효율성을 높이고 복잡성을 줄입니다.

Java에서는 다음을 사용하여 추상화를 달성할 수 있습니다. 추상 수업 및 방법. 이번 튜토리얼에서는 Java에서의 추상 메소드와 그 사용법에 대해 배웁니다.

추상 수업

클래스는 다음을 사용하여 추상으로 선언됩니다. 추상적인 예어. 0개 이상의 추상 및 비추상 메서드를 가질 수 있습니다. 추상 클래스를 확장하고 해당 메서드를 구현해야 합니다. 인스턴스화할 수 없습니다.

자바 연결 mysql

추상 클래스 구문:

 abstract class class_name { //abstract or non-abstract methods } 

참고: 추상 클래스에는 추상 메서드가 포함될 수도 있고 포함되지 않을 수도 있습니다.

추상 메소드

메소드를 사용하여 선언된 메소드 추상적인 추상 클래스 내에 키워드가 있고 정의(구현)가 없는 것을 추상 메서드라고 합니다.

슈퍼 클래스에서 메소드 선언만 필요한 경우 메소드를 추상으로 선언하여 이를 달성할 수 있습니다.

추상 메서드는 슈퍼 클래스에 구현이 없기 때문에 하위 클래스 책임이라고도 합니다. 따라서 서브클래스는 이를 재정의하여 메서드 정의를 제공해야 합니다.

추상 메소드 구문:

 abstract return_type method_name( [ argument-list ] ); 

여기서 추상 메서드에는 메서드 본문이 없습니다. 0개 이상의 인수가 있을 수 있습니다.

기억해야 할 점

다음 사항은 Java의 추상 메소드에 대한 중요한 규칙입니다.

  • 추상 메서드에는 본문(구현)이 없고 메서드 서명(선언)만 있습니다. 추상 클래스를 확장하는 클래스는 추상 메서드를 구현합니다.
  • 비추상(구체) 클래스가 추상 클래스를 확장하는 경우 클래스는 해당 추상 클래스의 모든 추상 메서드를 구현해야 합니다. 그렇지 않은 경우 구체적인 클래스도 추상 클래스로 선언해야 합니다.
  • 추상 메서드에는 시그니처만 있으므로 끝에 세미콜론(;)이 있어야 합니다.
  • 다음은 다양한 불법적인 조합 에 관한 메소드에 대한 기타 수식어 추상적인 수정자:
    • 결정적인
    • 추상 네이티브
    • 추상 동기화
    • 추상 정적
    • 추상 개인
    • 추상 엄격한
  • 클래스에 추상 메소드가 포함되어 있으면 추상이어야 하며 그 반대의 경우는 그렇지 않습니다.

Java의 추상 메소드 예

예시 1:

다음 예제에서는 추상 클래스와 추상 메서드를 사용하여 추상화를 수행하는 방법을 알아봅니다.

AbstractMethodEx1.java

우선순위 큐 자바
 // abstract class abstract class Multiply { // abstract methods // sub class must implement these methods public abstract int MultiplyTwo (int n1, int n2); public abstract int MultiplyThree (int n1, int n2, int n3); // regular method with body public void show() { System.out.println ('Method of abstract class Multiply'); } } // Regular class extends abstract class class AbstractMethodEx1 extends Multiply { // if the abstract methods are not implemented, compiler will give an error public int MultiplyTwo (int num1, int num2) { return num1 * num2; } public int MultiplyThree (int num1, int num2, int num3) { return num1 * num2 * num3; } // main method public static void main (String args[]) { Multiply obj = new AbstractMethodEx1(); System.out.println ('Multiplication of 2 numbers: ' + obj.MultiplyTwo (10, 50)); System.out.println ('Multiplication of 3 numbers: ' + obj.MultiplyThree (5, 8, 10)); obj.show(); } } 

산출:

Java의 추상 메소드

예 2:

기본적으로 인터페이스의 모든 메소드는 공개 및 추상입니다. 인터페이스는 구체적인 메소드, 즉 본문이 있는 일반 메소드를 포함할 수 없습니다.

AbstractMethodEx2.java

 // interface interface SquareCube { // abstract methods public abstract int squareNum (int n); // it not necessary to add public and abstract keywords // as the methods in interface are public abstract by default int cubeNum (int n); // regular methods are not allowed in an interface // if we uncomment this method, compiler will give an error /*public void disp() { System.out.println ('I will give error if u uncomment me'); } */ } public class AbstractMethodEx2 implements SquareCube { // defining the abstract methods of interface public int squareNum (int num) { return num * num; } public int cubeNum (int num) { return num * num * num; } // main method public static void main(String args[]){ SquareCube obj = new AbstractMethodEx2(); System.out.println('Square of number is: ' + obj.squareNum (7) ); System.out.println('Cube of number is: ' + obj.cubeNum (7)); } } 

산출:

Java의 추상 메소드

이상으로 우리는 추상 메소드와 Java에서의 구현에 대해 배웠습니다.

리눅스 명령의 디렉토리