Java tutorial- package

Java package

A package enables you to structure your files in folders and subfolders. By doing so, you can avoid file names conflict.

-Create a folder called javasource in drive D: (D:\javasource)
-Save the following code as TestInheritance.java in it

import Testpackage.*;//to import the package named Testpackage



class Dog extends Animal

{
   private String dogName;

 
   public void setdogName(String name)
   {
      dogName = name;
   }

 
   public String getdogName()
   {
      return dogName;
   }

 

}

 

public class TestInheritance

{
   public static void main(String[] args)
   {
      Dog d= new Dog();
      d.setnumberOflegs(4);
      d.setdogName("Aluk");
      System.out.println("Dog Name: " + d.getdogName());
      System.out.println("Number of legs: " + d.getnumberOflegs());
   }

}

 

-Create a subfolder called Testpackage in D:\javasource and save the following code as Animal.java in it.

 

package Testpackage; //to create a package named Testpackage

abstract class Animal

{
   private int numberOflegs;

 
   public void setnumberOflegs(int n)
   {
      numberOflegs = n;
   }

 
   public int getnumberOflegs()
   {
      return numberOflegs;
   }

}

 

-Type javac TestInheritance.java in DOS window to compile the program

-Type java TestInheritance in DOS window to run the program

 

Note:

            -all package statement must be the first statement in the file
            -To create a package, use package keyword followed by the name of the package (package packagename)
            -To import the package, use import keyword followed by the name the package, dot, and the star ( import packagename.*).



Comments

Name:
Comment:
Enter the text:
CAPTCHA image

....................................................................................................................Home | Forum | About | Contact
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like