달력

12

« 2018/12 »

  •  
  •  
  •  
  •  
  •  
  •  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  •  
  •  
  •  
  •  
  •  

'추상클래스'에 해당되는 글 2

  1. 2010.12.09 추상 클래스보다는 인터페이스를 사용하자
  2. 2010.12.09 추상 클래스

추상클래스로 정의된 타입을 구현하는 클래스는 반드시 추상 클래스의 서브 클래스가 되어야 한다.

인터페이스를 구현하는 클래스의 경우는 인터페이스에 정의된 모든 메소드를 구현하고 인터페이스 구현 계약을 지키면

되므로 클래스 상속계층과는 무관하다.

각각의 장점(?) 즉 외부에 공개된 중요한 인터페이스와 연관시킨 골격 구현 추상클래스를 제공하여 인터페이스와

추상 클래스의 장점을 결합한다.

골격 구현 클래스의 작성법은 다음과 같다.

우선 대상이 되는 인터페이스를 파악하고, 구현할 메소드와 그대로 둘 메소드를 결정한다

그대로 둘 메소드가 골격 구현 클래스의 추상 메소드가 될 것이다.

그다음 그대로 둘 메소드를 제외한 인터페이스의 나머지 모든 메소드를 구현한다.

다음의 예제를 한번 보면 좀 더 이해가 갈 것 같다.

public abstract class AbstractMapEntry<K,V> implements Map.Entry<K,V> {
public abstract K getKey();
public abstract V getValue();

public V setValue(V value){
throw new UnsupportedOperationException();
}

//기타 Map.Entry.equals 메소드에 약속된 보면적 계약은 생략...
}

음 여기 항목도 결론은 상황에 따라 추상 클래스와 인터페이스 중 잘 선택해서 쓰자 라는 것.

추상 클래스는  진화의 용이성 이 중요할 때

인터페이스는 유연성이 중요할 때  인 것 같다.

Posted by 유쾌한순례자
2010.12.09 16:53

추상 클래스 Study/Java2010.12.09 16:53

한마디로 " 설계서, 공통적인 양식 " 라고 생각하면 될 것 같다.

추상메서드는 선언부만 작성하고 구현부는 남겨둔다.(이러하기 때문에 미완성 메서드)

메서드의 내용이 상속받는 클래스에 따라 달라지기 때문에 실제내용은 상속받는 클래스에서 구현하도록 하는 것이다,

사용방법은 키워드 abstract 해서 쓰면 된다.

ex)

abstract class 클래스 명{
abstract void Test();
}

실제로 예제 정리

import javax.swing.*;
abstract class Work{
abstract void temp(int j);
}
class Mode extends Work{
@Override
void temp(int i) {
if(i<23){
System.out.println("Cold Mode");
}else{
System.out.println("Hot Mode");
}
}
}
public class aircon{
public static void main(String args[]){
String ques = "";
int temp = 26;
Work wk = new Mode();
ques = JOptionPane.showInputDialog("온도를 입력하세요");
temp = Integer.parseInt(ques);
wk.temp(temp);
}
}
}

다음은 JLS 의 abstract class 부분을 보자 (  JLS 8.1.1.1 abstract class 발췌 )


8.1.1.1 abstract Classes
An abstract class is a class that is incomplete, or to be considered incomplete.
Normal classes may have abstract methods (§8.4.3.1, §9.4), that is methods
that are declared but not yet implemented, only if they are abstract classes.
If a normal class that is not abstract contains an abstract method, then a compile-
time error occurs.
Enum types (§8.9) must not be declared abstract; doing so will result in a
compile-time error. It is a compile-time error for an enum type E to have an
abstract method m as a member unless E has one or more enum constants, and all
of E’s enum constants have class bodies that provide concrete implementations of
m. It is a compile-time error for the class body of an enum constant to declare an
abstract method.
A class C has abstract methods if any of the following is true:
• C explicitly contains a declaration of an abstract method (§8.4.3).
• Any of C’s superclasses has an abstract method and C neither declares nor
inherits a method that implements (§8.4.8.1) it.
• A direct superinterface (§8.1.5) of C declares or inherits a method (which is
therefore necessarily abstract) and C neither declares nor inherits a method
that implements it.
In the example:
abstract class Point {
int x = 1, y = 1;
void move(int dx, int dy) {
x += dx;
y += dy;
alert();
}
abstract void alert();
}
abstract class ColoredPoint extends Point {
int color;
}

class SimplePoint extends Point {
void alert() { }
}
a class Point is declared that must be declared abstract, because it contains a
declaration of an abstract method named alert. The subclass of Point named
ColoredPoint inherits the abstract method alert, so it must also be declared
abstract. On the other hand, the subclass of Point named SimplePoint provides
an implementation of alert, so it need not be abstract.
A compile-time error occurs if an attempt is made to create an instance of an
abstract class using a class instance creation expression (§15.9).
Thus, continuing the example just shown, the statement:
Point p = new Point();
would result in a compile-time error; the class Point cannot be instantiated
because it is abstract. However, a Point variable could correctly be initialized
with a reference to any subclass of Point, and the class SimplePoint is not
abstract, so the statement:
Point p = new SimplePoint();
would be correct.
A subclass of an abstract class that is not itself abstract may be instantiated,
resulting in the execution of a constructor for the abstract class and, therefore,
the execution of the field initializers for instance variables of that class. Thus,
in the example just given, instantiation of a SimplePoint causes the default constructor
and field initializers for x and y of Point to be executed.
It is a compile-time error to declare an abstract class type such that it is not
possible to create a subclass that implements all of its abstract methods. This
situation can occur if the class would have as members two abstract methods
that have the same method signature (§8.4.2) but incompatible return types.
As an example, the declarations:
interface Colorable { void setColor(int color); }
abstract class Colored implements Colorable {
abstract int setColor(int color);
}
result in a compile-time error: it would be impossible for any subclass of class
Colored to provide an implementation of a method named setColor, taking one
argument of type int, that can satisfy both abstract method specifications,
because the one in interface Colorable requires the same method to return no
value, while the one in class Colored requires the same method to return a value
of type int (§8.4).
A class type should be declared abstract only if the intent is that subclasses
can be created to complete the implementation. If the intent is simply to prevent
instantiation of a class, the proper way to express this is to declare a constructor
(§8.8.10) of no arguments, make it private, never invoke it, and declare no other
constructors. A class of this form usually contains class methods and variables.
The class Math is an example of a class that cannot be instantiated; its declaration
looks like this:
public final class Math {
private Math() { } // never instantiate this class
. . . declarations of class variables and methods . . .
}
Posted by 유쾌한순례자