Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 6th, 2012  |  syntax: None  |  size: 2.05 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Variable Inheritance in Java
  2. public abstract class Big {
  3.  
  4.     public String tellMe = "BIG";
  5.  
  6.     public Big() {}
  7.  
  8.     public void theMethod() {
  9.         System.out.println ("Big was here: " + tellMe() + ", " + tellMe);
  10.     }
  11.     public String tellMe() {
  12.         return tellMe;
  13.     }
  14. }
  15.  
  16. public class Little extends Big{
  17.  
  18.     public String tellMe = "little";
  19.     public Little(){}
  20.  
  21.     public String tellMe() {
  22.         return "told you";
  23.     }
  24.     public static void main(String [] args) {
  25.         Little l = new Little();
  26.         l.theMethod();
  27.     }
  28. }
  29.        
  30. public String tellMe = "little";
  31.        
  32. public Little(){
  33.     tellMe = "little";
  34. }
  35.        
  36. public String tellMe() {
  37.   return "Whatever";
  38. }
  39.        
  40. public abstract String tellMeString();
  41.        
  42. public String tellMeString()
  43. {
  44.     return "Little";
  45. }
  46.        
  47. System.out.println ("Big was here: " + tellMe() + ", " + tellMeString());
  48.        
  49. class Big {
  50.     protected String tellMe() {
  51.         return "BIG";
  52.     }
  53. }
  54. class Little {
  55.     @Override
  56.     protected String tellMe() {
  57.         return "Little";
  58.     }
  59. }
  60.        
  61. class Big {
  62.     private String tellMe;
  63.     public Big() {
  64.         this("BIG");
  65.     }
  66.     protected Big(String tellMe) {
  67.         this.tellMe = tellMe;
  68.     }
  69.     protected String tellMe() {
  70.         return "BIG";
  71.     }
  72. }
  73. class Little {
  74.     public Little() {
  75.          super("Little");
  76.     }
  77. }
  78.        
  79. static class Big {
  80.     String field = "BIG";
  81.     String bark()   { return "(big bark)"; }
  82.  
  83.     void doIt() {
  84.         System.out.format("field(%s) bark(%s)n", field,bark());
  85.     }
  86.     void doIt2()    {
  87.         System.out.format("2:field(%s) bark(%s)n", field,bark());
  88.     }
  89. }
  90.  
  91. static class Small extends Big {
  92.     String field = "small";
  93.     String bark()   { return "(small bark)"; }
  94.     void doIt2()    {
  95.         System.out.format("2:field(%s) bark(%s)n", field,bark());
  96.     }
  97. }
  98.  
  99. public static void main(String... args) {
  100.  
  101.     Big b = new Big();
  102.     b.doIt();
  103.     b.doIt2();
  104.  
  105.     Small s = new Small();
  106.     s.doIt();
  107.     s.doIt2();
  108. }
  109.        
  110. field(BIG) bark((big bark))
  111. 2:field(BIG) bark((big bark))
  112. field(BIG) bark((small bark))
  113. 2:field(small) bark((small bark))