/* * [Recipe.java] * * Summary: Demonstrate override and shadow with and without casting. * * Copyright: (c) 1998-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 1996-07-08 initial version * 1.1 1998-11-10 add name and address */ package com.minpdrod.recipe; /** * Demonstrate override and shadow with and without casting. * * @author Roedy Green, Canadian Mind Products * @version 1.1 1998-11-10 add name and address * @since 1996-07-08 */ public class Recipe { private static final String EmbeddedCopyright = "Copyright: (c) 1996-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; public static void main( String[] args ) { // check behaviour of static functions and variables. System.out.println( Grandma.name() ); /* Bessie */ System.out.println( Mom.name() ); /* Rhonda */ System.out.println( Grandma.age ); /* 70 */ System.out.println( Mom.age ); /* 30 */ Grandma grandma = new Grandma(); Mom mom = new Mom(); // check out instance behaviour of instance functions and variables. System.out.println( grandma.recipe() ); /* light a fire */ System.out.println( mom.recipe() ); /* open a can */ System.out.println( grandma.cups ); /* 20 */ System.out.println( mom.cups ); /* 1 */ // check out instance behaviour of casted instance functions and variables. System.out.println( ( ( Grandma ) mom ).recipe() ); /* open a can !!! */ System.out.println( ( ( Grandma ) mom ).cups ); /* 20 !!! */ // check out instance behaviour of variables accessed internally System.out.println( grandma.getCups() ); /* 20 */ System.out.println( mom.getCups() ); /* 20 !!! */ System.out.println( ( ( Grandma ) mom ).getCups() ); /* 20 !!! */ } // end main } // end class Recipe class Grandma { public static int age = 70; public int cups = 20; String recipe() { return ( "light a fire,..." ); } public static String name() { return ( "Bessie" ); } public int getCups() { return cups; } } // end class Grandma class Mom extends Grandma { public static int age = 30; public int cups = 1; public static String name() { return ( "Rhonda" ); } public String recipe() { return ( "Open a can ..." ); } } // end class Mom