SUB-PACKAGE IN JAVA

WHAT IS SUB-PACKAGE IN JAVA:

Package inside the package is called the sub package

The packages that come lower in the naming hierarchy are called “sub package.

Constructor In Java:

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created.

 Unlike Java methods, a constructor has the same name as that of the class and does not have any return type.

CHARACTERISTIC OF JAVA CONSTRUCTORS:

  • Constructors are automatically called when an object is created,
  • Constructors do not have a return type,
  • It is useful for initializing object Specific Values.
  • Constructors name must be similar to that of the class name inside which it resides.

SAMPLE PROGRAM 1:

package tamilnadu1.thanjavur1;
public class SuperMarket1
{
int price;
int discount;
public SuperMarket1(int p,int d)
{
price=p;
discount=d;

}

public static void main(String[]args)
{
SuperMarket1 rice = new SuperMarket1(60,6);
SuperMarket1 bread = new SuperMarket1(25,5);
SuperMarket1 soap = new SuperMarket1(20,5);
//rice . price = 60;
//bread .price = 25;
//soap . price = 20;
//rice .discount =6;
//bread . discount = 5;
//soap . discount = 5;
System.out.println(rice.price);
System.out.println(bread.price);
System.out.println(soap.price);
System.out.println(rice.discount);
System.out.println(bread.discount);
System.out.println(soap.discount);

}

}

To Compile like this,

javac -d . SuperMarket1.java

To Run like this,

java tamilnadu1.thanjavur1.SuperMarket1

OUTPUT:

Sample Program 2:

package marriage.kanyakumari;
public class Photography
{
int price;
int discount;
public Photography(int p,int d)
{
price = p;
discount = d;

}
public static void main(String[]args)
{
Photography engagement =new Photography(15000,2);
Photography reception = new Photography(18000,5);
Photography marriage = new Photography(20000,7);
Photography marriage_reception=new Photography(35000,4);
marriage_reception.compliment();
//engagement.price=15000;
//reception.price=18000;
//marriage.price=20000;
//engagement.discount=2;
//reception.discount=5;
//marriage.discount=7;
System.out.println(engagement.price);
System.out.println(reception.price);
System.out.println(marriage.price);
System.out.println(marriage_reception.price);
System.out.println(engagement.discount);
System.out.println(reception.discount);
System.out.println(marriage.discount);
System.out.println(marriage_reception.discount);

}
public void compliment()
{
System.out.println(“PreWedding_Photoshoot”);
}

}

To Compile like this,

javac -d . Photography.java

To Run like this,

java marriage.kanyakumari.Photography

OUTPUT:

.

Leave a comment

Design a site like this with WordPress.com
Get started