Let Us Know About Package And Interfaces Creation In Java


This resource contains the information about ‘package and interfaces creation’ in java etc.

Let Us Know About Package And Interfaces Creation In Java



Package:guide
Package is container for classes, interfaces and sub packages. java.lang.Thread, java.lang.Runnable
Here java package contains lang package and Thread class, Runnable interface are in lang package.

Interface:
Java does not support multiple inheritances with classes. Due to this, interfaces are introduced. Interfaces support multiple inheritances. It contains final type data members and declaration of methods. If data member is final, its value is not changed. If member function is final, is not overridden. If class is final, it is not inherited.

interface Area
{
double PI=3.14;
void c_area(double r);
}

class Circle implements Area
{
void c_area(double r)
{
System.out.println("area is : "+PI*r*r);
}
}

class TestArea
{
public static void main(String args[])
{
Circle cobj=new Circle();
cobj.c_area(2);
}
}

Extends keyword:
Extends keyword is used when class is derived from another class, and interface is derived from another interface.

Implements keyword:
Implements keyword is used when class is derived from interface.
Note: All methods of interface should be defined in child class.



STEPS TO CREATE USER DEFINED PACKAGE:

Step 1:
Create following directory structure
c:\nagpur\javapack\mypack

Step 2:
Create Factorial.java, Sum.java, Area.java and store its .class files in above directory structure. Factorial.java, Sum.java contains classes. Area.java contains interface.

Factorial.java

package javapack.mypack;
public class Factorial
{
public static int fact(int n)
{
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
}
}

Sum.java

package javapack.mypack;
public class Sum
{
public void total(int n)
{
int s=0;
for(int i=1;i<=n;i++)
{
s=s+i;
}
System.out.println("sum is : "+s);
}
}

Area.java

package javapack.mypack;
public interface Area
{
double PI=3.14;
public void c_area(double r);
}

Step 3:
Set CLASSPATH =.;c:\nagpur;

SELECT MY COMPUTER AND OPEN PROPERTIES FROM POPUP MENU.
GO TO ADVANCED TAB FROM DISPLAYED DIALOG BOX.
GO TO ENVIRONMENT VARIABLES.
AT SYSTEM VARIABLES COLUMN CREATE CLASS PATH IF NOT AVAILABLE OR EDIT EXISTING CLASS PATH VARIABLE.

Step 4:
Create testing program anywhere in the computer, compile and run that program to display result.

import javapack.mypack.*;
class Circle implements Area
{
public void c_area(double r)
{
System.out.println("area is: "+ PI*r*r);
}
}

class Test
{
public static void main(String args[])
{
Circle obj1=new Circle();
obj1.c_area(2);

int ans=Factorial.fact(4);
System.out.println(ans);

Sum obj2=new Sum();
obj2.total(5);
}
}

Note:
Store this Test.java in c:\nagpur folder to operate user defined package without class path setting. That means store Test.java in parent directory of java pack directory to operate without class path setting.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: