Abstract Class – Java Tutorial

Abstract Class – Java Tutorial

An abstract is a class that you cannot create any concrete objects, but you can extend from. You use the keyword abstract when you declare the class. You can also create methods that are abstracts that can be inherited. Below is an example of an abstract class.

public abstract class Animal
{
private String name;
public Animal(String nm)
{
name = nm;
}
public String getName()
{
return(name);
}
public abstract void speak();
}
Now that you have an abstract class created, you can create other classes that are extended form this class like the one below:

public class Cow extends Animal
{
  public Cow(String nm)
  {
    super(nm);
  }
  public void speak()
  {
    System.out.println("Moo");
  }
}

You must either provide a subclass method to override a superclass abstract method if you want to instantiate any subclass objects, or declare the subclass as an abstract so you can extend the subclass into sub-subclasses.

When a superclass is abstract, objects of that superclass cannot be instantiated, but they can be referenced. You do this by typing ref.objectName(). This is called dynamic method binding. The following programs show how a reference works.

The AbstractClass


abstract class AbstractClass1 {
protected AbstractClass1() {
System.out.println("AbstractClass1 constructor called");
}

public abstract void distinct_method();

public void common_method() {
System.out.println("common_method called");
distinct_method();
}
}

class ConcreteClass extends AbstractClass1 {
public void distinct_method() {
System.out.println("distinct_method called");
}
}

public class AbstractDemo1 {
public static void main(String args[]) {
AbstractClass1 ref;

//ref = new AbstractClass1();

ref = new ConcreteClass();
ref.common_method();
}
}

Note: you cannot create a new instance of an abstract class. The comment is intended to notify that the class has been referenced. If you got rid of the comment lines // and compiled the program, you’d get an error.

A good idea to keep in mind is to create a superclass reference and treat subclass objects as superclass objects so you can create an array of different objects that share the same ancestry. For example, say you set up three classes, Account (abstract), and two child classes, Checking and Savings. You have fields in Account for account number and balance, a constructor that requires the account number and sets the balance to 0.0, a set() method for the balance, and two abstract get() methods for each field. In Checking, the get() method prints “Checking Account Information”, the account number, and the balance. Savings has a field for the interest rate, requires the Savings constructor to accept an argument for the interest rate, then the get() method prints “Savings Account Information”, the account number, the balance and the interest rate. Now, if you wanted to show a mix of ten accounts, you might use an array like this:

public class AccountArray
{
  public static void main(String[] args)
  {
    Account[] ref = new Account[10];
    Checking checkOne = new Checking(7564);
    Checking checkTwo = new Checking(25661);
    Checking checkThree = new Checking(3104);
    Checking checkFour = new Checking(29942);
    Checking checkFive = new Checking(8306);
    Savings saveOne = new Savings(7564, 3.1);
    Savings saveTwo = new Savings(25661, 3.3);
    Savings saveThree = new Savings(3104, 3.5);
    Savings saveFour = new Savings(29942, 3.7);
    Savings saveFive = new Savings(8306, 3.9);
    ref[0] = checkOne;
    ref[1] = checkTwo;
    ref[2] = checkThree;
    ref[3] = checkFour;
    ref[4] = checkFive;
    ref[5] = saveOne;
    ref[6] = saveTwo;
    ref[7] = saveThree;
    ref[8] = saveFour;
    ref[9] = saveFive;
    for(int x = 0; x < 5; ++x)
    {
      ref[x].getAcctNum();
      ref[x].getAcctBal();
    }
    for(int x = 5; x < 10; ++x)
    {
      ref[x].getAcctNum();
      ref[x].getAcctBal();
    }
  }
}

Note the two for loops. One is specifically for the Checking class and the other is specifically for the Savings class.

Question: After all this learning, I should be able to create any software program I want. Is there anything more I need to know?

Answer: Like you said, you should be able to create any software program you want if you knew a little bit more about the Abstract Windows Toolkit.