Java-Creating Subclasses

Creating Subclasses

It is also possible to subclass a class that has been imported from another package. This is illustrated by following program. Note that the Data class inside the mypack package.

Program

503final

Package Source

package mypack;

public class Data {
    
    public int data1 = 20;
    
    public void printData() {
        
        System.out.println("data1 = "+data1);
    }
}

Program Source

import mypack.*;

class NewData extends Data {
    
    int data2 = 40;
    
    void newData(){
        
        System.out.println("data1 = "+data1);
        System.out.println("data2 = "+data2);
    }
}

public class Javaapp {
    
    public static void main(String[] args) {
        
        NewData nd = new NewData();
        nd.newData();
    }
}

Leave a Comment