Overlading vs Overriding in Java

Overloading: Same Method Name but different parameters in the same class

Overriding: Same Method Signature in both superclass and child class

Parent Class

package sample;

public class People {
    private String name;
    private int age;

    public People(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String move() {
        return this.name + " is moving";
    }

    /**
     * @return String return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return int return the age
     */
    public int getAge() {
        return age;
    }
}

Child Class

package sample;

public class American extends People {

    private String language;
    private String conutry;

    public American(String name, int age, String language, String conutry) {
        super(name, age);
        this.language = language;
        this.conutry = conutry;
    }

    public String speak() {
        return this.getName() + " can speak " + this.language;
    }

    @Override
    public String move() {
        return this.getName() + " is moving in "+ this.conutry;
    }

    /**
     * @return String return the language
     */
    public String getLanguage() {
        return language;
    }

    /**
     * @return String return the conutry
     */
    public String getConutry() {
        return conutry;
    }
}

Leave a Reply

Your email address will not be published.

ANOTE.DEV