티스토리 뷰

Overview

Abstract classes are classes that contain abstract methods. Abstract methods are methods that contain no implementation. A class that contains all abstract methods is called an interface. It is not possible to create an instance of an abstract class or an interface, due to the fact that some or all of their methods are not complete.

Reasoning

Abstract classes and interfaces exist for the purpose of organization. If you want to model an abstract concept without being able to instantiate it, an abstract class or interface may be used. For example, think about food: “Food represents the abstract concept of what we can eat. It doesn't make sense for an instance of food to exist.”* This idea of thinking about concepts abstractly allows for much more freedom when writing and sharing code. Also, abstraction increases the power of polymorphism. This is the basis behind abstraction.

Implementation: Abstract Classes

An abstract class contains one or more abstract methods. Generally you want to create an abstract class when you are creating several objects that will share some method functionality, and differ in some method functionality. The methods that function the same way for every object will be implemented in the abstract superclass, whereas the methods that differ in functionality for each object will be declared abstract in the abstract superclass, and implemented in each of the subclasses.

Take for example, an abstract class that models electronics.

public abstract class Electronic
{
      boolean powerOn = false;

      public boolean isOn()
      {
            return powerOn;
      }

      public void turnOn()
      {
            powerOn = true;
            System.out.println("Turned on");
      }

      public void turnOff()
      {
            powerOn = false;
            System.out.println("Turned off");
      }

      public abstract void operate();
}

Notice the abstract keyword in the declaration of class Electronic and method operate(). Also notice the semicolon after the declaration of operate().This is the proper syntax for defining abstract classes and methods.

Every subclass of Electronic will inherit the methods isOn(), turnOn(), and turnOff(), and each of those methods will function in the same way. However, the method operate() will function differently in each subclass of Electronic.

Take now a subclass of Electronic, class Toaster.

public class Toaster extends Electronic
{
      boolean hasBread = false;

      public Toaster(boolean bread)
      {
            hasBread = bread;
      }

      public boolean checkForBread()
      {
            return hasBread;
      }

      public void addBread()
      {
            hasBread = true;
      }

      public void operate()
      {
            if (isOn() && checkForBread())
            {
                  System.out.println("Bread toasted");
            }

            else if (!isOn())
            {
                  System.out.println("Turn power on");
            }

            else if (!checkForBread())
            {
                  System.out.println("Must insert bread before toasting");
            }
      }
}

Notice that the operate() method is implemented in a way specific to this class.

Now look at class iPod, another subclass of Electronic.

public class iPod extends Electronic
{
      String currentSong = null;

      public iPod(String song)
      {
            currentSong = song;
      }

      public String getCurrentSong()
      {
            return currentSong;
      }

      public void changeSong(String newSong)
      {
            currentSong = newSong;
      }

      public void operate()
      {
            if (currentSong != null && isOn())
            {
                  System.out.println("Now playing: " + currentSong);
            }

            else if (!isOn())
            {
                  System.out.println("Turn power on");
            }

            else if (currentSong == null)
            {
                  System.out.println("Please choose a song to play");
            }
      }
}

Both class Toaster and class iPod have different operate() methods, but use the methods implemented in class Electronic in the same way. By grouping these methods in the abstract class Electronic, and extending it to Toaster and iPod, we have saved ourselves from reproducing the same code in each class. Thus far reducing redundancy and increasing efficiency within the code.

Implementation: Interfaces

An interface is basically an abstract class that contains all abstract methods. That is to say, there is no implementation in the class whatsoever. All the class contains are several method headers. Its purpose is similar to that of an abstract class, only in this case, none of the classes that ‘implement’ the interface share the same code. They only share the same method names.

To create an interface, use the interface keyword. None of the methods in the interface need to be declared abstract, because it is obvious that they are abstract if they are within an interface.

public interface Animal
{
      String talk();
      int nuberOflegs();
      int nuberOfarms();
      boolean hasFur();
}

Interfaces are not extended, they are implemented by other classes. This is done through the implements keyword. Take a look at the following four classes that implement the Animal interface.

public class Cat implements Animal
{
      public Cat(){}

      public String talk()
      {
            return "Meow";
      }

      public int numberOfLegs()
      {
            return 4;
      }

      public int numberOfArms()
      {
            return 0;
      }

      public boolean hasFur()
      {
            return true;
      }
}

public class Dog implements Animal
{
      public Dog(){}

      public String talk()
            {
      return "Woof";
      }

      public int numberOfLegs()
      {
            return 4;
      }

      public int numberOfArms()
      {
            return 0;
      }

      public boolean hasFur()
      {
            return true;
      }
}

public class Human implements Animal
{
      public Human(){}

      public String talk()
      {
            return "Hello";
      }

      public int numberOfLegs()
      {
            return 2;
      }

      public int numberOfArms()
      {
            return 2;
      }

      public boolean hasFur()
      {
            return false;
      }
}

public class TRex implements Animal
{
      public TRex(){}

      public String talk()
      {
            return "Roar!";
      }

      public int numberOfLegs()
      {
            return 2;
      }
      public int numberOfArms()
      {
            return 2;
      }
      public boolean hasFur()
      {
            return false;
      }
}

Notice that each of these classes implements the methods specified in the Animal interface in a different way. That is to say, there are no methods with common functionality among all of the classes that implement Animal. Thus is why Animal is an interface, and not an abstract class.




*quoted from http://java.sun.com/docs/books/tutorial/java/javaOO/abstract.html

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
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
글 보관함