Java Abstract & Interface (JAI)


JAI:   SOLD       




When we create a superclass, we know that we can inherit all the methods it has into its child classes.

Or if we need the method updated, we just override the method in its subclass.

But there are cases where only one or more methods must be overridden.


inheritance

For example, the method  bersuara() in the Animal class (example from yesterday's meeting)

The sound method should be overridden because it has no form in the animal class (depending on the type of animal)

To make a method sound overridable, the method needs to be made abstract.

abstract = formless

Writing an abstract method is as follows: public abstract String bersuara();

Henceforth, this method is referred to as an abstract method or prototype method.

We can see that the method sounds:

  • do not have  tubuh / implementasi method.
  • using the abstract keyword.

If we force to have a program body like this:

public abstract String bersuara(){
	System.out.println(“weee..”);
}

Then it will happen error abstract methods cannot have a body

If we add the sound abstract method to the Animal class that we have created, an error will appear:

Animal is not abstract and does not override abstract method sound() in animal

The solution is to change the Animal class to an abstract class. If there are abstract methods in a class, then the class must be abstract too.

abstract class Binatang{
	public abstract String bersuara();

	public voide makan(int x){
		System.out.println(“Makan makan”);
	}
}

This abstract class cannot be instantiated (because it has abstract methods)

Questions:

  • Is it permissible for an abstract class to not have any abstract methods at all?
  • What if an abstract class has a constructor?
  • What is the function and usage of the constructor?

Abstract Class Constructor

public abstract class TestEngine{
    private String engineId;
    private String engineName;

    public TestEngine(String engineId, String engineName){
        this.engineId = engineId;
        this.engineName = engineName;
    }

    //getter & settter
    public abstract void scheduleTest();
}


public class JavaTestEngine extends TestEngine{
    private String typeName;

    public JavaTestEngine(String engineId, String engineName, String typeName){
        super(engineId, engineName);
        this.typeName = typeName;
    }

    public void scheduleTest(){
        //todo something
    }
}

Every class in Java must have a constructor by default!

Example of Incorrect Implementation of Abstract Class

public abstract class KelasAbstract{
    public KelasAbstract(){
        tulis("Aku berada di "+this.getClass());
    }

    publicvoid tulis(String teks){
        System.out.println(teks);
    }
} //di kelas ini tidak ada method abstract

public class ImplementasiAbstract extends KelasAbstract{
    public ImplementasiAbstract(){
        super.tulis("Aku ada di kelas ImplementasiAbstract");
    }
}

Correct Example of Abstract Class Implementation

public abstract class KelasAbstract{
    public KelasAbstract(){
        tulis("Aku berada di "+this.getClass());
    }

    publicvoid tulis(String teks){
        System.out.println(teks);
    }

    abstract vlid methodAbstract();
} 
public class ImplementasiAbstract extends KelasAbstract{
    public ImplementasiAbstract(){
        super.tulis("Aku ada di kelas ImplementasiAbstract");
    }

    public void methodAbstract(){
        super.tulis("Method abstract kelas induk sudah didefinisikan ulang");
    }
}

Remember!

  • A class that has methods declared as abstract must also be declared as an Abstract class.
  • A class can be declared abstract without having abstract methods.

Another Example

abstract class AAA{
    //fungsi abstract "callme()" tanpa implementasi
    abstract void callme();

    //fungsi dengan implementasi tetap dibolehkan
    //dalam kelas abstract
    public void callmetoo(){
        System.out.println("ini fungsi test")
    }
}

class BBB extends AAA{
    //implementasi dari fungsi callme() pada kelas abstract AAA
    void callme(){
        System.out.println("implementasi fungsi callme di kelas BBB");
    }
}

class abstractdemo{
    public static void main(String args[]){
        BBB varBBB = new BBB();
        varBBB.callme();
        varBBB.callmetoo();
    }
}

An interface may consist of a collection of functions or constants that do not contain an implementation.

Example:

public interface Mp3Player{
    public static final int STATUS; //deklarasi final dan static
    List TRACKLIST;

    void playTract();
    void stopTract();
    void volumeUp();
    void volumeDown();
}

Why do we need an interface? In other programming languages ​​such as C++, there is a term called multiple-inheritance, which means an object can be derived from two or more different objects. For example, object X has super classes A and B.

In Java, this is not possible, because an object can only extend from 1 object, and this is where Java interfaces become the solution.

As previously mentioned, the Interface class can consist of a collection of function/method declarations to be implemented in other classes, so there is only a method declaration without a method definition, meaning that the method does not have a function body. This means that all methods in the Interface class are abstract. But when declaring, there is no need to use the abstract keyword.

The purpose of abstraction is to make a class implementable to another class without any relationship at all.

Example:

public interface Camera{
    public void setPixel(float pixel);
    public void ambilGambar();
}

Interfaces are used to support multiple inheritance (one class has more than one superclass). In addition, it can also declare abstract methods, in the interface can also be given final attributes (constants). These constants are also inherited to the class that implements the interface.

An interface can also inherit another interface, like normal inheritance, Example.

public interface NamaInterface extends InterfaceA, InterfaceB{
    //todo something
}

A class that implements an interface must declare all the methods in the interface, otherwise the class must be declared as an abstract class.

Interface Rules

Classes that implement an interface only inherit constants in the interface class, unlike derived classes that inherit everything, whether it's variables or methods.

A class that implements an interface does not inherit methods that are not interfaces, namely classes that have implementations in the interface class.

In a derived class, all existing functions will be inherited, both abstract and those implemented in the superclass.

Independent class hierarchy, that is, interface classes and the classes that implement them can be unrelated to each other.

Interface vs Abstract

The use of the keywords extends and implements is one of the differences between the two.

All methods in an Interface are Abstract, but only a few or maybe just one method is Abstract in an Abstract class.

Abstract class can implement Interface, but Interface cannot inherit Abstract class.

Abstract can declare instance variables, Interface can only declare constants.


Post a Comment

Previous Next

نموذج الاتصال