What is method hiding in Java? Even the JavaDoc explanation is confusing

public class Animal { public static void foo() { System.out.println(“Animal”); } } public class Cat extends Animal { public static void foo() { // hides Animal.foo() System.out.println(“Cat”); } } Here, Cat.foo() is said to hide Animal.foo(). Hiding does not work like overriding, because static methods are not polymorphic. So the following will happen: Animal.foo(); // … Read more

Why use getters and setters/accessors?

There are actually many good reasons to consider using accessors rather than directly exposing fields of a class – beyond just the argument of encapsulation and making future changes easier. Here are the some of the reasons I am aware of: Encapsulation of behavior associated with getting or setting the property – this allows additional functionality (like validation) … Read more

How can I design a class named allergy

requirement: design a class named allergy that provides information about the allergy of a patient. e.g. who reported the allergy(patient/doctor/relative), different symptoms of the allergy that are detected, severity, method that returns when was that allergy detected in that patient. I am thinking of something like this: [java]public abstract class Allergy{ private String reporter; private … Read more