Menu
Topics Index
...
`


Interfaces, Packages and Access Control >
Siva Nookala - 24 Feb 2019
As discussed in Packages Packages are used to group related classes. Now we will see how to compile classes in packages.

Now i have a two program BedRoomLight.java and HallLight.java at the locations E:\src\house\bedroom and E:\src\house\bedroom
INSERT SHOWING PACKAGES IMAGE HERE INSERT SHOWING BEDROOMLIGHT PROGRAM INSERT SHOWING HALLLIGHT PROGRAM Here I considered BedRoomLight as super class and HallLight is extending it.
/* BedRoomLight.java */
package house.bedroom;
public class BedRoomLight
{
    public void print()
    {
        System.out.println("I am Bedroom light");
    }
}
/* HallLight.java */

package house.hall;
import house.bedroom.*;
public class HallLight extends house.bedroom.BedRoomLight
{
    public static void main(String[] args)
    {
        BedRoomLight l = new BedRoomLight(); // LINE A
        l.print();
        HallLight h = new HallLight(); // LINE B
        h.print();
    }
    public void print()
    {
        System.out.println("I am Hall light");
    }
}
Here in HallLight program at LINE A we are creating an object for BedRoomLight and calling the print method in BedRoomLight. At LINE B we are creating an object for HallLight and calling HallLight print method.

Now we want to compile these program and execute them we should do as follows in the command prompt.
INSERT IMAGE OF COMPILING BEDROOMLIGHT INSERT IMAGE OF COMPILING HALLLIGHT
E:\src>javac house/bedroom/BedroomLight.java
E:\src>javac house/hall/HallLight.java
E:\src>java house/hall/HallLight
This will produce the below output.
I am Bedroom light
I am Hall light
Note : To compile the programs we should be in the parent directory. So here i started from src

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App