how to make a package in java and import it

how to make a package in java and imort it in a next program

how to make and use a package

how to declare a package in java

The syntax to declare a package is package package-name;
This is how we declare a package .To make a package there must be the directory just like as we have declared the our package .In this above figure what we have done is make a two directory project and demo inside D: drive. We have used our project name as “project.demo”. So now write a java program and save the class file into these location to make it a package. Do not forget to use the first line to declare package. Now any class that are inside project/demo can be easily imported and used by any other class.
The class to import this package must be at path D: drive because remaining paths are for the package which means you can’t compile the class importing the package from the project or demo directory.
See the example code below:

Package project.demo;
Public class Tara
   {
       Public void show()
      {
      System.out.println(“hi my name is Tara”);
     }
}

Compile these program from these location
D:/project/demo >
Or wherever you compile it the class file must be inside D:/project/demo because these is how we have declare our package.

Now we will import these package from the next class.

We will make this class in D: drive and compile and execute from the same location. WE can’t use the previous location where we have our package class.

Import project.demo.*;    or we can write import project.demo.Tara;
Class Demo
  {
    public static void main(String[] args)
     {
        Tara obj=new Tara();
         Obj.show();
        }
}

Now compile and run the program from location D:>

what is encapsulation in java

Encapsulation is one of the fundamental concept of Object Oriented Programming language. It is also known as data hiding.It is the process of hiding the data by making it private and allowing access only through the public method or interface. Let me better start with one good example to make it more simpler.
You have a money which belongs to you and you don’t want anyone else to use it except you. what will you do to make it safe from others. I guess you are using the bank to save your money.You will feel secure that no one can use your money. One thing to note here is even you can’t directly use that money what do I mean is to use money you need to perform some actions right like make a check or use ATM. This is how the encapsulation implemented in java.
Lets take another simple example all of us have a private cell phone number which everyone can access with the call.

encapsulation in java

This is how encapsulation works

how is it employed in java

lets use this simple example code that will be more better and easier.
-we will make a class with the instance variable name and phone-no;
-we will use one method to access that variable
-Remember the instance variable being created must be private access specifier type

  class AccountHolder
   {
     private String name;
     private int phone-no;

     public void customer()
      {
        name="tara";
        phone-no=431530;

        System.out.println("the name="+name);
        System.out.println("the phone-no="+phone-no);
      }
    }

Can you see we have made this class encapsulated. Lets discuss how
-Let me ask you a basic qustion first. Any other class which one to use the methods or variables of these class how do they do it? yea ofcourse by making an instance of this class.
-With the instance of this class being created ok we can access the method by simply calling the method but do we have a right to make change in the variables, its absolutely no. It is because the variables are private to the class and only the same class can use the variable. How is this class making use of it using the public methods. These methods are accessible to other class since its public method.
-This is what we call as Encapsulation in java.

why is it called data hiding

As we see any class which one one to use such class has access to the methods only but not the variables so it looks like as if the variables are hidden. So this is why encapsulation is also called as data hiding.

points to consider to make encapsulation

-the variable must be private otherwise there won’t be any encapsulation. If the variable is public than any other class will have access to it and can easily make a change to its value.
-The method must be public. There is no use of making a method that is private to a class.This should be the only one means for other class to access the variable of the class.

Advantage of Encapsulation

-The main purpose of encapsulation is to make data secure from outer world.

what is inheritance in java

Inheritance:

When you were at your school you must have come up with the term inheritance while reading biology. What was the meaning of inheritance in biology. Heredity is the passing of traits to offspring (from its parent or ancestors). This is the process by which an offspring cell or organism acquires or becomes predisposed to the characteristics of its parent cell or organism. So it’s the same concept that is being employed in java programming.

Inheritance is the process of creating a new class from the existing class rather than creating a new class from the scratch. Also inheritance can be defined as extending already defined class.The new class being made is called a derived class or child class or subclass.
And the existing class from which the new class is made is called base class or super class or parents class.

After inheriting the base class the derived class can be able to use all the functionality available in the base class just using one line of code. The derived class can use the instance variables and the methods in the base class. Also the derived class can add the functionality of its own. Imagine what it be without inheritance ,the child class would have to write every method available in the base class again.

Inheritance is like making a new model of cell phone adding some extra functionality on the existing one .See every cell phone available in the market they all have a common functionality only what we find is some extra features and graphics being added.This is the good example of inheritance the company wont make a new model of cell phone bringing change in every hardware and software that previous model had.They will reuse at least something from previous model. Inheritance is also about the re-usability of code.Why to write a code again and again if verified and tested code is available.So inheritance saves the time and money.


Syntax to make inherit the class


class childclassname extends baseclassname
eg:class Dog extends Animal
{
—-
——
}

lets see one example code:

class Father
 {
    String color="red";
    public void myCar()
     {
       System.out.println("i have a "+color+"car");
     }
 }

class Son extends Father
{
   public static void main(String[] args)
    {
      Son obj=new Son();
      obj.myCar();
    }
}

So can you understand the code .Here the son needs the method to display car. It can find that the method is already available in its
Father class so it extends the Father class rather than writing the code of its own.
So from now onward whenever you find some functionality that some class can fulfill than extend that class.

how to know if a class can be inherited

To know if the class you are going to make can be inherited from the existing one we can do one test called IS A .
Which means suppose I have a class called Mobile to describe what a mobile should be like. Bow I am going to make a class called Nokia than we will do the test like this. Nokia is a Mobile .It sounds so clear yes Nokia is a mobile phone means Nokia class can be made to inherit Mobile class.

Types of Inheritance in java

There are three type of inheritance in java.
1-Single inheritance
2-Multilevel inheritance
3-Hierarchial inheritance

Multiple inheritance is not supported in java.

Single inheritance

It is the simplest type of inheritance available in java where a class will inherit the properties from a single base class. It means we will have a single base class and a single derived class.
Following figure gives the schematic representation of single inheritance in java.

single inheritance

Single Inheritance

example code of single inheritance in java:

  class MobilePhone
  {
    public void call()
     {
        System.out.println("basic feature of mobile");
     }
  }

  class NokiaLumia extends MobilePhone
  {
     public void lumia()
      {
        System.out.println("features of Lumia");
       }
    public static void main(String[] args)
     {
       NokiaLumia cell=new NokiaLumia();
       cell.call();
       cell.lumia();
      }
   }
single inheritance example output

sample output of single inheritance

Multilevel inheritance

The multilevel inheritance can be well defined only from a simple diagram so please see the figure below which shows the schematic representation of single inheritance in java.

multilevel inheritance in java

Multi-Level inheritance

These type of inheritance follows the one to one ladder as shown in figure.The father will inherit the property of the Grandfather so
the grandfather is the base class of the father but the son will inherit the properties of Father but will also have the properties from the
grandfather class.The multilevel inheritance is the indirect way of implementing multiple inheritance like we can see in this example
the son class has a multiple super class Grandfather and Father.

example code of multiLevel inheritance in java

  class Grandfather
  {
    public void one()
     {
        System.out.println("grandfather")
      }
   }

 class Father extends Grandfather
  {
    public void two()
  {
    System.out.println("Father");
   }
  }

class Son extends Father
  {
    public void three()
    {
      System.out.println("son");
     }
   public static void main(String args[])
    {
       Son obj=new Son();
        obj.one();
        obj.two();
        obj.three();
      }
   }
multilevel inheritance sample program output

sample output of above examle code

3.Hierarchical inheritance

It is the type of inheritance in which multiple classes are derived from the same single base class. Or we can say it as one class is being inherited by more than one class such type of inheritance is called hierarchical inheritance.Figure below depicts the hierarchical inheritance.
hierarchical inheritance schematic diagram

example code of hierarchical inheritance in java

class HumanBeing
 {
   public void parts()
    {
      System.out.println("I am human ");
    }
  }

 class Male extends HumanBeing
  {
    public void boys()
    {
     System.out.println("Male");
    }
  }

  class Female extends HumanBeing
   {
     public void girls()
      {
        System.out.println("Female");
      }
   }

  class MainStart
 {
     public static void main(String[] args)
   {
      Male m=new Male();
      m.parts();
      m.boys();

      Female f=new Female();
      f.parts();
      f.girls();
    }
}
hierarchical inheritance sample output

sample output of above examle code