THREAD: is this a proper way to use polymorphism in java?
  1. 01-22-2018, 07:53 PM
    32
    Posts
    30
    Reputation
    May 2017
    NextGenUpdate
    public class Program
    {
    public static void main(String[] args) {
    healthy a = new healthy();
    healthy b = new unhealthy();
    a.food();
    b.food();
    }
    }
    class healthy{
    public void food(){
    System.out.println("vegetable");
    }
    }
    class unhealthy extends healthy{
    public void food(){
    System.out.println("meat");
    }
    }
    Last edited by mudville209 ; 01-22-2018 at 08:48 PM.

  2. 02-17-2018, 04:04 AM
    irule914's Avatar
    irule914
    Keeper
    irule914's Avatar
    irule914
    Keeper
    16
    Posts
    70
    Reputation
    Oct 2011
    NextGenUpdate
    No, in your case create an interface or an abstract class if you want specific classes having a set instruction for methods.
  3. 03-25-2018, 11:11 PM
    23
    Posts
    206
    Reputation
    Jun 2014
    NextGenUpdate
    I’m pretty sure if you want to create a new class you have to close the other close with Insert class name (here).close();