Home Tutorial Java Certification Abstract class or methods example-1

 
 

Share on Google+Share on Google+
Abstract class or methods example-1
Posted on: June 30, 2010 at 12:00 AM
Advertisement
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
 */
Advertisement

Related Tags for Abstract class or methods example-1:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 30, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com