oop - Java Inheritance: Superclass being "overriden" by Subclass function with different signature -


i quite rusty on java , i'm having hard time following:

public abstract class animal implements comparable<animal> {      //enum stored elsewhere     animaltype type = null;      private specificcompare(animal a){         return 0;     }      @override     public int compareto(animal a){         int comp = type.compareto(a.type);         if (comp > 0) {             return 1;         } else if (comp < 0) {             return -1;         } else {             return specificcompare(a);         }     } }  public class mammal extends animal {     private mammalstuff;      public mammal(){         ...     }      public specificcompare(mammal m){        //do specific comparison     } } 

so, understand why mammal's specificcompare isn't being called (incorrect type) i'm trying figure out clean way make work. want able extend animal , have correctly call specific comparison when needed.

in addition actual answer, if has suggested documentation me read on happy have link it.

override parent function , cast animal parameter mammal or whatever specific subclass.

public class mammal extends animal {     private mammalstuff;      public mammal(){         ...     }      public int specificcompare(animal a){        mammal m = (mammal)a;        //do specific comparison     } 

you're calling specificcompare (see: animal.compareto) when know both objects same type, won't fail.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -