인터페이스를 구현하는 추상 클래스가 왜 인터페이스 메소드 중 하나의 선언 / 구현을 놓칠 수 있습니까?
추상 클래스를 사용하여 인터페이스를 구현할 때 Java에서 흥미로운 일이 발생합니다. 인터페이스의 일부 메소드가 완전히 누락 될 수 있지만 (예 : 추상 선언이나 실제 구현이 존재하지 않음) 컴파일러는 불평하지 않습니다.
예를 들어 인터페이스가 다음과 같습니다.
public interface IAnything {
void m1();
void m2();
void m3();
}
다음 추상 클래스는 경고 나 오류없이 컴파일됩니다.
public abstract class AbstractThing implements IAnything {
public void m1() {}
public void m3() {}
}
이유를 설명 할 수 있습니까?
클래스가 추상적이라면 정의에 따라 인스턴스화하기 위해 서브 클래스를 만들어야하기 때문입니다. 서브 클래스는 추상 클래스가 생략 한 인터페이스 메소드를 구현하기 위해 (컴파일러에 의해) 필요합니다.
예제 코드에 AbstractThing
따라 m2
메소드 를 구현하지 않고 서브 클래스를 작성 하고 컴파일러가 제공하는 오류를 확인하십시오. 이 메소드를 구현해야합니다.
완벽하게 괜찮습니다.
추상 클래스를 인스턴스화 할 수는 없지만 추상 클래스를 사용하여 m1 () 및 m3 ()에 대한 공통 구현을 수용 할 수 있습니다.
그래서 만약 m2 () 구현은 각 구현 다르지만 M1 및 m3는 없다. 다른 m2 구현으로 다른 구체적인 IAnything 구현을 생성하고 DRY 원칙을 준수하는 AbstractThing에서 파생 할 수 있습니다. 인터페이스가 추상 클래스에 대해 완전히 구현되었는지 검증하는 것은 쓸모가 없습니다.
업데이트 : 흥미롭게도 C #은 이것을 컴파일 오류로 시행합니다. 이 시나리오에서는 메소드 서명을 복사하여 추상 기본 클래스에서 'abstract public'접두사를 붙여야합니다.
괜찮아. 위의 내용을 이해하려면 먼저 추상 클래스의 특성을 이해해야합니다. 그것들은 그런 점에서 인터페이스와 유사합니다. 이것이 오라클이 여기 에 대해 말한 것 입니다.
추상 클래스는 인터페이스와 유사합니다. 인스턴스화 할 수 없으며 구현 여부에 관계없이 선언 된 여러 가지 메소드를 포함 할 수 있습니다.
따라서 인터페이스가 다른 인터페이스를 확장 할 때 어떤 일이 발생하는지 고려해야합니다. 예를 들어 ...
//Filename: Sports.java
public interface Sports
{
public void setHomeTeam(String name);
public void setVisitingTeam(String name);
}
//Filename: Football.java
public interface Football extends Sports
{
public void homeTeamScored(int points);
public void visitingTeamScored(int points);
public void endOfQuarter(int quarter);
}
... as you can see, this also compiles perfectly fine. Simply because, just like an abstract class, an interface can NOT be instantiated. So, it is not required to explicitly mention the methods from its "parent". However, ALL the parent method signatures DO implicitly become a part of the extending interface or implementing abstract class. So, once a proper class (one that can be instantiated) extends the above, it WILL be required to ensure that every single abstract method is implemented.
Hope that helps... and Allahu 'alam !
Interface means a class that has no implementation of its method, but with just declaration.
Other hand, abstract class is a class that can have implementation of some method along with some method with just declaration, no implementation.
When we implement an interface to an abstract class, its means that the abstract class inherited all the methods of the interface. As, it is not important to implement all the method in abstract class however it comes to abstract class (by inheritance too), so the abstract class can left some of the method in interface without implementation here. But, when this abstract class will inherited by some concrete class, they must have to implements all those unimplemented method there in abstract class.
Given the interface:
public interface IAnything {
int i;
void m1();
void m2();
void m3();
}
This is how Java actually sees it:
public interface IAnything {
public static final int i;
public abstract void m1();
public abstract void m2();
public abstract void m3();
}
So you can leave some (or all) of these abstract
methods unimplemented, just as you would do in the case of abstract
classes extending another abstract
class.
When you implement
an interface
, the rule that all interface
methods must be implemented in the derived class
, applies only to concrete class
implementation (i.e., which isn't abstract
itself).
If you indeed plan on creating an abstract class
out of it, then there is no rule that says you've to implement
all the interface
methods (note that in such a case it is mandatory to declare the derived class
as abstract
)
When an Abstract Class Implements an Interface
In the section on Interfaces, it was noted that a class that implements an interface must implement all of the interface's methods. It is possible, however, to define a class that does not implement all of the interface's methods, provided that the class is declared to be abstract. For example,
abstract class X implements Y {
// implements all but one method of Y
}
class XX extends X {
// implements the remaining method in Y
}
In this case, class X must be abstract because it does not fully implement Y, but class XX does, in fact, implement Y.
Reference: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
Abstract classes are not required to implement the methods. So even though it implements an interface, the abstract methods of the interface can remain abstract. If you try to implement an interface in a concrete class (i.e. not abstract) and you do not implement the abstract methods the compiler will tell you: Either implement the abstract methods or declare the class as abstract.
'Programing' 카테고리의 다른 글
PostgreSQL DB에서 현재 연결 수를 가져 오는 올바른 쿼리 (0) | 2020.07.14 |
---|---|
파이썬의 목적 __repr__ (0) | 2020.07.14 |
R에서 키 누르기를 기다리는 방법? (0) | 2020.07.14 |
AngularJs 앱을 작성할 때 Jade 또는 Handlebars를 사용하는 것은 무엇입니까 (0) | 2020.07.14 |
파이썬에서 문자열로 유니 코드를 선언하는 이유는 무엇입니까? (0) | 2020.07.14 |