Post: is this a proper way to use polymorphism in java?
01-22-2018, 07:53 PM #1
(adsbygoogle = window.adsbygoogle || []).push({});
    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.
02-17-2018, 04:04 AM #2
No, in your case create an interface or an abstract class if you want specific classes having a set instruction for methods.
03-25-2018, 11:11 PM #3
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();

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo