Java -Abstract keyword – why and how

0
2283

As learning Java, we knew how to deal with Inheritance where all the subclasses will represent the spirit of the superclass. They inherited all the attributes and methods that belong to superclass. 

Abstract on a class

Once we have enough subclasses for covering all different kinds of the superClass. Sure we don’t need to make an instance from the superclass anymore. There is no scene for doing that.

For giving an simple example, let me put it in this silly way:

We have a superClass Finger, and we will have 5 subclasses, no need to say eh? Just in case of birth defects, we have another subclass for the extra fingers...

  • Finger (superClass)
    1. Thumb
    2. Index
    3. Middle
    4. Ring
    5. Little
    6. ExtraFinger

Up to this point, all the subclasses are enough to cover all kinds of a finger.  It seems like we don’t need to initialize any object straight from the Finger class. We make Finger an abstract class to prevent accidentally initialize it.

Another case of Abstract class is when your Inheritance tree has many layers, the top superClass at some point doesn’t need to be used for initializing an object, you also wanna make it abstract.

Note:

  • An abstract class cannot be used to initialize an object (it’s abstract, not a real object).
  • We make a superclass as Abstract when we find that all the subclasses cover all the type we need about that class and there’s no need to have an object that initialized directly from the superclass.
  • An Abstract class is kind of a principle that enforce all the subclass to follow all the abstract methods inside it.

Abstract on methods

If a method is declared as abstract in a superclass, the subclass must implement this method unless it will throw an error. The abstract method just needs to be declared (name, return type, variable type) without having the body.

We declare an abstract method to ensure that all the subclasses will have a kind of behavior in common, but the behavior doesn’t need to be the same between those subclasses.

public static abstract class Finger {
private String name;

public Finger(String name) { this.name = name; }

public abstract void action();
}

public static class Thumb extends Finger{
public Thumb(String name) { super(name); }

@Override
public void action() { System.out.println("Give a thumb up"); }
}

public static class Index extends Finger{

public Index(String name) { super(name); }

@Override
public void action() { System.out.println("Point to something"); }
}

Consider the code above. We want to enforce all the finger subclasses have a method of action but they can implement that method in their own way. We declared the action method in the superclass Finger as abstract without specific the code inside the method. The ability that allows the same method acts differently among all subclasses is known as Polymorphism. The abstract keyword here is just for enforcing all the subclasses implement that method. Keeping everything uniform.

Note:

  • An Abstract method must be declared inside an Abstract class.
  • An Abstract method couldn’t be implemented (have no code in its body) in abstract class since this class couldn’t be initialized.
  • All subclasses of an abstract class MUST implement the abstract methods.

public class AbstractClass {
public static abstract class Finger {
private String name;

public Finger(String name) { this.name = name; }

public abstract void action();
}

public static class Thumb extends Finger{
public Thumb(String name) { super(name); }

@Override
public void action() { System.out.println("Give a thumb up"); }
}

public static class Index extends Finger{

public Index(String name) { super(name); }

@Override
public void action() { System.out.println("Point to something"); }
}

public static class Middle extends Finger{
public Middle(String name) {
super(name);
}

@Override
public void action() { System.out.println("OK, F...ine!"); }
}
public static class Ring extends Finger{
public Ring(String name) {
super(name);
}

@Override
public void action() { System.out.println("Will you marry me?"); }
}

public static class Little extends Finger{
public Little(String name) {
super(name);
}

@Override
public void action() { System.out.println("What am I used for?"); }
}

public static void main(String[] args) {
//initialize all subclasses
}
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here