Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Superclass { //start definition of class Sueprclass
- int age; // every Superclass instance has an int field called age
- Superclass(int age) { // Superclass constructor
- this.age = age; // assign parameter to field age
- }
- public void getAge() { // getter method for age - see next line
- // its incorrect as a getter. A getter method should return the field
- // This returns nothing - it outputs the value
- System.out.println("The value of the variable named age in super class is: " + age);
- }
- } // end definition of Superclass
- public class Test extends Superclass { // start Test class definition
- // Test extends Superclass - so it inherits all members
- Test(int age) { // A constructor
- super(age); // this calls the constructor of the superclass
- }
- public static void main(String argd[]) { // execution starts here
- Test s = new Test(24); // make a Test instance
- s.getAge(); // call the inherited method getAge;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment