bookmark_borderAbstract 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”.
ANOTE.DEV