A subclass method overrides its superclass method if it has the same name a parameter types as its superclass.
public class Test { public static void main(String[] args) { Square mysquare = new Square(); mysquare.getInformation(); } } class Shape // the SUPERCLASS { int color = 4; public void getInformation() // getInformation() in the superclass { System.out.println(color); } } class Square extends Shape // the SUBCLASS { String sides="4 sides"; public void getInformation() // getInformation() in the subclass { System.out.println(sides); } }
Output:
4 sides
You can notice the subclass and the superclass both have exactly the same method, with the same parameters, when we create an object from Shape, the getInformation() method in the subclass OVERRIDES the getInformation() in the superclass.
We can make the superclass getInformation() method private and keep the subclass getInformation() method public, but
we cannot make the superclass getInformation() method public and keep the subclass getInformation() method private.
A mistake some coders make is this. If you mean to override a method but introduce a different parameter TYPE, you are accidentally overloading, not overriding.