Page 142 - CTS - CSA TP - Volume 2
P. 142

COMPUTER SOFTWARE APPLICATION - CITS





           EXERCISE 99 : Override methods in JAVA


            Objectives

           At the end of this exercise you shall be able to
           •  know override  methods in JAVA
           •  develop Java programs using override methods.

           Requirements

           Tools/Materials

           •  PC / laptop with windows OS
           •   SDK software
           •  Test editor (Visual studio/ subline/ notepad)


           Method overriding in Java allows a subclass to provide a specific implementation of a method that is
           already provided by its superclass. Here’s an example demonstrating method overriding along with its
           explanation:

           Procedure


           TASK 1: Shape and its Subclasses
           // Shape and its Subclasses using override method
           class Shape {
           void draw() {

           System.out.println(“Drawing a shape”);
           }
           }
           class Circle extends Shape {

           // Overriding the draw method of the superclass
           @Override
           void draw() {
           System.out.println(“Drawing a circle”);
           }
           }

           class Rectangle extends Shape {
           // Overriding the draw method of the superclass
           @Override
           void draw() {

           System.out.println(“Drawing a rectangle”);
           }
           }





                                                           127
   137   138   139   140   141   142   143   144   145   146   147