Java Interface, inner & abstract class

Introduction

Java Interface & abstract class

Contents

  1. Interface in Java
  2. Inner class in Java
  3. Abstract class in Java

1. Interface in Java

Interface itself that you are creating is abstract, and that means that there is no actual code for any of the methods, you only supply the actual method names and the parameters. The idea is to use interface to provide common behavior that can be used several classes by having them all implement the same interface.

It is really a way to standardize the way a particular set of classes is used. 

It is actually a commitment, a contract if you will, that the method’s signatures that those variables in the interface constants if you defined any will not change.

The interface exists to define the methods that must be implemented. So private or public really useless.

How would we go about implementing this interface?

  • Our class is going to be using the interface.
  • To use implements keyword
package anote;

public class Main {
    public static void main(String[] args) {

        ITelephone janePhone;
        janePhone = new DeskPhone(987654321);
        janePhone.powerOn();
        janePhone.callPhone(987654321);
        janePhone.answer();

        System.out.println("----------------------------");
        janePhone = new MobilePhone(123456789);
        janePhone.powerOn();
        janePhone.callPhone(123456789);
        janePhone.answer();
    }
}

interface ITelephone {
    void powerOn();
    void dial(int phoneNumber);
    void answer();
    boolean callPhone(int phoneNumber);
    boolean isRinging();
}

class DeskPhone implements ITelephone{

    private int myNumber;
    private boolean isRinging;

    public DeskPhone(int myNumber) {
        this.myNumber = myNumber;
    }

    @Override
    public void powerOn() {
        System.out.println("power on");
    }

    @Override
    public void dial(int phoneNumber) {
        System.out.println("Now ringing " + phoneNumber + "on deskphone");
    }
    @Override
    public void answer() {
        if(isRinging){
            System.out.println("Answering the desk phone");
            isRinging = false;
        }
    }

    @Override
    public boolean callPhone(int phoneNumber) {
        if (phoneNumber == myNumber){
            isRinging = true;
            System.out.println("Ring ring");
        }
        else{
            isRinging = false;
        }
        return isRinging;
    }

    @Override
    public boolean isRinging() {
        return false;
    }
    
}

class MobilePhone implements ITelephone{

    private int myNumber;
    private boolean isRinging;
    private boolean isOn = false;

    public MobilePhone(int myNumber) {
        this.myNumber = myNumber;
    }

    @Override
    public void powerOn() {
        isOn = true;
        System.out.println("Mobile phone powered up");
    }

    @Override
    public void dial(int phoneNumber) {
        if (isOn){
        System.out.println("Now ringing " + phoneNumber + "on mobile phone");
        }
        else{
            System.out.println("Phone is switched off");
        }
    }
    @Override
    public void answer() {
        if(isRinging){
            System.out.println("Answering the mobile phone");
            isRinging = false;
        }
    }

    @Override
    public boolean callPhone(int phoneNumber) {
        if (phoneNumber == myNumber && isOn){
            isRinging = true;
            System.out.println("Melody ring");
        }
        else{
            isRinging = false;
            System.out.println("Mobile phone not on or number different");
        }
        return isRinging;
    }

    @Override
    public boolean isRinging() {
        return false;
    }
}
power on
Ring ring
Answering the desk phone
----------------------------
Mobile phone powered up
Melody ring
Answering the mobile phone

2. Inner class in Java

In Java, it is also possible to nest classes (a class within a class). The purpose of nested classes is to group classes that belong together, which makes your code more readable and maintainable.

package anote;

import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {

        Gearbox mcLaren = new Gearbox(6);
        Gearbox.Gear first = mcLaren.new Gear(1, 12.3);
        System.out.println(first.driveSpeed(1000));
    }
}

class Gearbox {
    private ArrayList<Gear> gears;
    private int maxGears;
    private int gearNumber = 0;

    public Gearbox(int maxGears) {
        this.maxGears = maxGears;
        this.gears = new ArrayList<>();
        Gear neutral = new Gear(0, 0.0);
        this.gears.add(neutral);
    }

    public class Gear {
        private int gearNumber;
        private double ratio;
    
        public Gear (int gearNumber, double ratio){
            this.gearNumber = gearNumber;
            this.ratio = ratio;
        }
        public double driveSpeed(int revs){
            return revs * ( this.ratio);
        }
    }
}
12300.0

3. Abstract class in Java

An abstract class is a class that is declared abstract  it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.

package anote;

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog("Doge");
        dog.breathe();
        dog.eat();

        System.out.println("------------");
        Swift swift = new Swift("swift");
        swift.breathe();
        swift.eat();
        swift.fly();
        System.out.println("------------");
        Penguin penguin = new Penguin("Penguin");
        penguin.breathe();
        penguin.eat();
        penguin.fly();
    }
}

abstract class Animal {
    private String name;
    public Animal(String name) {
        this.name = name;
    }

    public abstract void eat();
    public abstract void breathe();
     String getName(){
        return name;
    }
}

class Dog extends Animal {

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

    @Override
    public void eat() {
        System.out.println(getName() + "is eating");
    }

    @Override
    public void breathe() {
        System.out.println("Breathe in and out, repeat");

    }
}

abstract class Bird extends Animal {
    public Bird(String name) {
        super(name);
    }

    @Override
    public void eat() {
        System.out.println(getName() + "is peaking");

    }

    @Override
    public void breathe() {
        System.out.println("Breathe in and out, repeat");
    }

    public abstract void fly();
}

class Swift extends Bird { 
    public Swift(String name) {
        super(name);
    }

    @Override
    public void fly() {
        System.out.println("Flitting from branch to branch");
    }
}
class Penguin extends Bird {
    public Penguin(String name){
        super(name);
    }
    @Override
    public void fly() {
        System.out.println( "Is it possible?");
    }
}
Breathe in and out, repeat
Dogeis eating
------------
Breathe in and out, repeat
swiftis peaking
Flitting from branch to branch
------------
Breathe in and out, repeat
Penguinis peaking
Is it possible?

Abstract class

  1.  Abstract Classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation.
  2. However, with Abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.
  3. An Abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.
  4. However, if it does not, then the subclass must also be declared abstract.

Interface

  1. An interface is just the declaration of methods of a Class, it is not the implementation.
  2. In an Interface, we define what kind of operation an object can perform. These operations are defined by the classes that implement the interface.
  3. Interfaces form a contract between the class and the outside world, and this contract is enforce at build time by the compiler.
  4. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. All methods in interfaces are automatically public and abstract.
  5. An interface can extend another interface.
  6. Interfaces are more flexible and can deal with a lot more stress on the design of your program than the implementation.
  7. By introducing interfaces into your program, you are really introduce points of variation at which you can plug in different implementations for that interface. An Interfaces primary purpose is abstraction. Decoupling the “what” from the “How”.

Leave a Reply

Your email address will not be published.

ANOTE.DEV