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

COMPUTER SOFTWARE APPLICATION - CITS



           EXERCISE 100 : Create and use Super class, Sub class in

                                        JAVA


            Objectives

           At the end of this exercise you shall be able to
           •  know the use of Super class and  Sub class in JAVA
           •  develop Java programs using  Super class and  Sub class.

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


           Procedure

           •  In object-oriented programming, the concept of inheritance allows a subclass to inherit properties and behaviors
              from a superclass. Here’s a simple Java program demonstrating the usage of a superclass and its subclass
              along with explanations
           TASK 1. Superclass and Subclass: Vehicle and Car Classes

           // Superclass
           class Vehicle {
               String brand;


               // Constructor

               Vehicle(String brand) {
                   this.brand = brand;
               }



               void displayBrand() {
                   System.out.println(“Brand: “ + brand);
               }
           }



           // Subclass
           class Car extends Vehicle {
               int year;


               // Constructor

               Car(String brand, int year) {
                   super(brand); // Call superclass constructor
                   this.year = year;



                                                           133
   143   144   145   146   147   148   149   150   151   152   153