Page 362 - CITS - Computer Software Application -TT
P. 362

COMPUTER SOFTWARE APPLICATION - CITS



           Hierarchical Inheritance Example

           When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given
           below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance.
           File: TestInheritance3.java
              class Animal{

              void eat(){System.out.println(“eating...”);}
              }
              class Dog extends Animal{
              void bark(){System.out.println(“barking...”);}
              }

              class Cat extends Animal{
              void meow(){System.out.println(“meowing...”);}
              }
              class TestInheritance3{

              public static void main(String args[]){
              Cat c=new Cat();
              c.meow();
              c.eat();
              //c.bark();//C.T.Error
              }}

           Output:
           meowing...
           eating...


            Method Overriding in JAVA

           In Java, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a
           method that is already provided by one of its super-classes or parent classes. When a method in a subclass has
           the same name, the same parameters or signature, and the same return type(or sub-type) as a method in its
           super-class, then the method in the subclass is said to override the method in the super-class.




























                                                           349

                             CITS : IT&ITES - Computer Software Application - Lesson 94 - 100
   357   358   359   360   361   362   363   364   365   366   367