Sunday, January 18, 2009

Simple java program to understand class imports

/*---------------------------------------------------------------------------------------
Simple java program to understand class imports
---------------------------------------------------------------------------------------*/
import javaprogs.basic.*;
import javaprogs.packagexamples.*;
import javaprogs.packagexamples.car;

class carexample{

public static void main(String args[]){
int j =2;

car fiat = new car();
fiat.setnumberoftyres(j);
fiat.displaynumberoftyres();

}
}

/*
---------------------------------------------------------------------------------------
Output of program to understand class imports
---------------------------------------------------------------------------------------
ambiguous reference
---------------------------------------------------------------------------------------
The output before adding the line "import javaprogs.packagexamples.car;"
---------------------------------------------------------------------------------------
C:\Users\flower\Documents>javac carexample.java
carexample.java:12: reference to car is ambiguous, both class javaprogs.packagex
amples.car in javaprogs.packagexamples and class javaprogs.basic.car in javaprog
s.basic match
car fiat = new car();
^
carexample.java:12: reference to car is ambiguous, both class javaprogs.packagex
amples.car in javaprogs.packagexamples and class javaprogs.basic.car in javaprog
s.basic match
car fiat = new car();
^
2 errors
---------------------------------------------------------------------------------------
The output after adding the line "import javaprogs.packagexamples.car;"
---------------------------------------------------------------------------------------
C:\Users\flower\Documents>javac carexample.java

C:\Users\flower\Documents>java carexample
Number of tyres is:3
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
The output if the line "import javaprogs.basic.car;" was added instead of the line "import javaprogs.packagexamples.car;"
---------------------------------------------------------------------------------------
C:\Users\flower\Documents>javac carexample.java

C:\Users\flower\Documents>java carexample
Number of tyres is:2
---------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
The car class inside "javaprogs.packagexamples" package
---------------------------------------------------------------------------------------
package javaprogs.packagexamples;

public class car{

private int numberoftyres;

public void setnumberoftyres(int i){

numberoftyres=i+1;
}

public void displaynumberoftyres(){
System.out.println("Number of tyres is:"+numberoftyres);
}

}
---------------------------------------------------------------------------------------
The car class inside "javaprogs.basic" package
---------------------------------------------------------------------------------------
package javaprogs.basic;

public class car{

private int numberoftyres;

public void setnumberoftyres(int i){

numberoftyres=i;
}

public void displaynumberoftyres(){
System.out.println("Number of tyres is:"+numberoftyres);
}

}
---------------------------------------------------------------------------------------
*/

No comments: