Home Tutorial Java Certification Abstract class or methods example-1

 
 

Abstract class or methods example-1
Posted on: June 30, 2010 at 12:00 AM
This example shows that a abstract class can hold abstract and non abstract methods.
package roseindia1;

// abstract class can hold non abstract method

abstract class Animal {
  void runAnimal() {
    System.out.println("runAnimal() method called form Animal class")// this
    // statement will override by the subclass method runAnimal
  }

  void printAnimal() {
    System.out.println("printAnimal() method called form Animal class");
  }
}

class BuzzwordAnimal extends Animal {
  void printAnimal() {
    System.out.println("printAnimal() method called form BuzzwordAnimal class");
  }

  void runAnimal() {
    super.runAnimal()
    // super keyword helps to call method of superclass ie Animal
    printAnimal()// this invoke the buzzwordAnimal class method
  }
}

class AbstractExample {
  public static void main(String[] args) {
    BuzzwordAnimal b = new BuzzwordAnimal();
    b.runAnimal()// overriding will take place as it has similar method
    // name exits both is sub and superclass
  }
}

/*
 * --------------------OUTPUT--------------------                       
 * runAnimal() method called form Animal class printAnimal() method called form
 * BuzzwordAnimal class
 */

Related Tags for Abstract class or methods example-1:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.