Declares Two Variables with the Same Name as Declared in Its Superclass - Java Object Oriented Design

Java examples for Object Oriented Design:Inheritance

Description

Declares Two Variables with the Same Name as Declared in Its Superclass

Demo Code

class MySuper {/*from  w ww  .  java2s. co  m*/
  protected int num = 100;
  protected String name = "Edith";
}

class MySub extends MySuper {
  // Hides num field in MySuper class
  private int num = 200;

  // Hides name field in MySuper class
  private String name = "Mary";

  public void print() {
    System.out.println("num: " + num);
    System.out.println("name: " + name);
  }
}

public class Main {
  public static void main(String[] args) {
    MySub a = new MySub();
    a.print();
  }
}

Result


Related Tutorials