waltermilner

Untitled

Dec 19th, 2016
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. class Superclass { //start definition of class Sueprclass
  2.  
  3.   int age; // every Superclass instance has an int field called age
  4.  
  5.   Superclass(int age) { // Superclass constructor
  6.     this.age = age; // assign parameter to field age
  7.   }
  8.  
  9.   public void getAge() { // getter method for age - see next line
  10.     // its incorrect as a getter. A getter method should return the field
  11.     // This returns nothing - it outputs the value
  12.     System.out.println("The value of the variable named age in super class is: " + age);
  13.   }
  14. } // end definition of Superclass
  15.  
  16. public class Test extends Superclass { // start Test class definition
  17.   // Test extends Superclass - so it inherits all members
  18.  
  19.   Test(int age) { // A constructor
  20.     super(age); // this calls the constructor of the superclass
  21.   }
  22.  
  23.   public static void main(String argd[]) { // execution starts here
  24.     Test s = new Test(24); // make a Test instance
  25.     s.getAge(); // call the inherited method getAge;
  26.   }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment