@Override In Inheritance With the Characteristic of Polymolphism : Annotation is just annotation? or Does it have a role?

#Foreword

  • Before i begin, I want to clarify that I am not a seasoned programmer; rather I am here to learn. If somebody encounters this document, consider is as someone's personal log.

I've often heard about Override and Overload. These are concepts I learned when studying Java. As you guys know, Spring is a Java-based framework. When I studided Spring, I only skimed over the overall flows, so specific concepts that I didn't fully understand still troubled me. This document will outline Override, along with annotations related to them.(Later, I will handle Overload too.) A solid foundation should precede applicable knowledge. Still, I can't apply my knowledge practically as i wish, and I don't find it enjoyable. I think I should be consistent until I find enjoyment in mastering something difficult. Thanksfully, I am glad I am hanging in this programming world. I hope this will be helpful for my improvement.

#Override

Simply put, "Override" refers to having the same method name but executing different actions for each class.

When a subclass has the same method as its superclass but performs a different action, this is an example of inheritance, and it exhibits the characteristic of polymorphism.

#On inheritance, a subclass can access and use the method from its superclass

In other words, subclass also use(access, call) upper class's method.

See below.

package com.example.demo.Practice;

public class Mammal {

    public void livingArea() {
        System.out.println("mammal.livingArea() = "
            + "Where Do Animals Live?");
    }
    public static class Tiger extends Mammal {

        // new method altogether
        public void WhereTigerLives() {
            System.out.println("tiger.WhereTigerLives() = "
            + "Asia, Habitat Diversity, Territorial, Solitary");
        }
    }
    public static class Lion extends Mammal {

        public void WhereLionLives() {
                System.out.println("lion.WhereLionLives() = "
    + "Africa, Gir Forest in Indea, Open Habitats, Social Structure");
        }
    }
}
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);

        Mammal mammal = new Mammal();
        mammal.livingArea();

        Tiger tiger = new Tiger();
        tiger.livingArea();

        Lion lion = new Lion();
        lion.livingArea();
    }
}

: Like you see, Even though I used method with different instance : mammal, tiger, lion, but same outcome is printed.

#Override Outcome

: same method name, but performs different action

(example of Inheritance, exibits the characteristic of polymorphism)

public class Mammal {

    public void livingArea() {
        System.out.println("mammal.livingArea() = "
            + "Where Do Animals Live?");
    }
    public static class Tiger extends Mammal {

        // Override - same name but different action
        public void livingArea() {
            System.out.println("tiger.livingArea() = "
                + "Where Does Tiger Live?");
        }

        // new method altogether
        public void WhereTigerLives() {
            System.out.println("tiger.WhereTigerLives() = "
                + "Asia, Habitat Diversity, Territorial, Solitary");
        }
    }

    public static class Lion extends Mammal {

        // Override - same name but different action
        public void livingArea() {
            System.out.println("lion.livingArea() = "
                + "Where Does Lion Live?");
        }

        // new method altogether
        public void WhereLionLives() {
                System.out.println("lion.WhereLionLives() = "
    + "Africa, Gir Forest in Indea, Open Habitats, Social Structure");
        }
    }
}

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
       SpringApplication.run(DemoApplication.class, args);

       Mammal mammal = new Mammal();
       mammal.livingArea();

       Tiger tiger = new Tiger();
       tiger.livingArea();

       Lion lion = new Lion();
       lion.livingArea();
    }
}

#Override Method + New Method

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);

        Mammal mammal = new Mammal();

        System.out.println();

        mammal.livingArea();

        System.out.println();

        Tiger tiger = new Tiger();
        tiger.livingArea();
        tiger.WhereTigerLives();

        System.out.println();

        Lion lion = new Lion();
        lion.livingArea();
        lion.WhereLionLives();

    }
}

#With Annotation : @Override

  • Like below, @Override can detect typos when it's used below annotation, we are intuitively notified.

  • Typos Detection

#Annotation is a just Annotation? , Crucial role of Annotation

  • Annotations in Spring (or in Java in general) serve as metadata to provide information to the Spring framework about how to configure and manage components, as well as to detect errors.

#Erorr Detection

  • Simply put, when we use annotations, we are telling Spring, "You should do the task below this annotation," and Spring can detect typos made by human error. While we can oversee this role of annotations, they play a crucial role in error detection for developers.

#Help Build More Robust And Reliable Applications

  • Annotations help make your code more readable, maintainable, and less error-prone. They provide a clear way to communicate your intentions to both other developers and the framework itself. Additionally, they allow for compile-time and runtime checks to catch mistakes like typos or misconfigurations early in the development process, which ultimately helps in building more robust and reliable applications.