Java - Packages & Interface

EXPLAIN : PACKAGE
- Package is java's way of grouping the classes, interfaces & sub packages together.
- It can also be known as a container for classes & interfaces.
- Package in Java can be categorized in two form, Built-in package & User-defined package.
- The Java library provides a large number of classes grouped into different packages according to the functionality.
- For example, While using "println()" method, We have to use the "java.lang.*;" default package.

IMPORTANCE : PACKAGE

- By using packages, Programmers can define their own packages to bundle a group of classes / interfaces and etc.
- Programmers can determine the Classes, Interfaces, Enumeration & Annotations easily by using the feature "Packages"


EXPLAIN : PACKAGES IN JAVA

- java.lang : "Language" supports classes such as the System, Thread, Exception and etc.
- java.util : "Utility" supports classes such as Vector, Array, Linked List, Stack and etc.
- java.io : "Input output" supports classes such as BufferedReader, InputStream.
- java.awt : It supports the Classes which includes GUI such as Window, Frame, Panel & etc.
- java.applet : It is used for creating & implementing the Applets in java.

EXPLAIN : HOW TO CREATE A PACKAGE

- Declare a package at the beginning of the gile using following syntax:
    - package packagename;
- Define a class that is to be put in the package and declare it in the public.
- Create the sub directory with the same name as the package name.
- Store the ".java" file in that sub directory.
- Compile the ".java" file, So that it creates the ".class" file in the sub directory.

SYNTAX : PACKAGE

package myPackage;
public class MyClass
{
    // Body Of The Class
}

EXAMPLE : PACKAGE

package p1;
public class Abc
{
    void show()
    {
        System.out.println("This Is The package 'p1' Which Contains The Class 'Abc'");
    }
}

EXPLAIN : HOW TO ACCESS THE JAVA PACKAGE

- There are 3 ways of accessing a Java package
    - Importing the package.*;
    - Import package.classname;
    - By fully qualified name

LIST : WAYS TO ACCESS PACKAGES IN JAVA

- Importing the "package.*;"
- Importing the "package.classname;"
- Fully qualified name

EXPLAIN : WAYS TO ACCESS PACKAGES IN JAVA

- Importing the "package.*;"
    - If you use "package.*;", Then all the classes and interfaces are available to the package and it will be accesible to the class.
    - The IMPORT keyword is sued to make the classes of another package accessible to the another classes.
- Importing the "package.classname;"
    - If you import the "package.classname;", Then only declared class of this package is accesible.
- Fully qualified name
    - If you use a fully qualified name, Then only the declared class of this package is accessible.
    - In this, There is no need to implement the IMPORT statement, But you need to use the fully qualified name to access the class.

EXPLAIN : HOW TO ADD A "CLASS" TO THE PACKAGE

- It is simple to add a class to an existing package consider the following package:
    package p1;
    public class A
    {
        // Body Of "A"
    }
- The package contains one public class by the name "A". Another class can be added by doing as follows:
    - Define the class & make it public:
        package p1;
        public class B
        {
            // Body of "B"
        }
    - Store this as "B.java" file under the directory "p1".
    - Compile "B.java". Thus, it will create a "B.class" file
    - Place the class file in the directory "p1".
    - Now the package "p1" contaims both the classes "A" & "B".
    - Use the statement "import p1.*;". Thus, It will import both of the classes "A" & "B"

EXPLAIN : WHAT ARE ACCESS SPECIFIERS IN JAVA

- Java provides a number of access modifiers to set access leveels for the classes and it's variables, methods & constructors.

LIST & DEFINE : ACCESS SPECIFIERS IN JAVA

- Private : It is visible to the class only.
- Public : It is visible to the world.
- Protected : It is visible to the Packages & All the Subclasses
- Default : It is visible to the package, If no access specifier is used. Then a default specifier is used.

EXPLAIN : MULTIPLE INHERITANCE IN JAVA

- A class which is derived with several base classes is known as multiple inheritance. A class can inherit the attributes of two or more classes.
- To reduce the complexity, Multiple inheritance is not supported in Java. And thus, Interface is used to achieve Multiple Inheritance.
- The Classes in Java cannot have more than pne super class.

EXAMPLE : MULTIPLE INHERITANCE IN JAVA

import java.lang.*;
class A
{
    void msg()
    {
        System.out.println("Hello!");
    }
}
class B
{
    void msg()
    {
        System.out.println("Welcome!");
    }
}
class C extends A, B
{
    public static void main (String args[])
    {
        C obj = new C();
        obj.Msg();
    }
}

EXPLAIN : INTERFACE

- Interface is mainly used for achieving Multiple Inheritance in Java.
- Interface is simple a blueprint of class.
- Once a interface is defined, Any number of classes can implement an interface.

FEATURES : INTERFACE

- Interface cannot be instantiated like the abstract class.
- Interfaces are similar to classes, But they do not have instance variables and their methods are declared without having any body.
- Interface contains abstract methods & final, static type of variables.
- Variables cannot be declared in private & protected modifiers.
- And, Methods cannot be declared in final, static, synchronized & native.

IMPORTANCE : INTERFACE

- Interface is used to reduce the complexity of the Java language. Thus, To achieve Multiple Inheritance, The concept of Inheritance is used.

SYNTAX : INTERFACE

acessspecifier interface interfacename
{
    final static datatype varname1 = value;
    final static datatype varname2 = value;
    ...
    final static datatype varnamen = value;

    returntype methodname1(parameters);
    returntype methodname2(parameters);
    ...
    returntype methodnamen(parameters);
}

EXPLAIN : HOW TO IMPLEMENT INTERFACE

- Once an Interface has been defined, One or more Classes can be implemented in that Interface.
- To implement an interface, We have to include the implement clause in a class definition.

EXAMPLE : INTERFACE

public interface Sport
{
    final static int sport_marks = 100;
    void calculate();
    void show();
}

EXPLAIN : EXTENDING THE INTERFACE

- Class EXTENDS another class, An Interface EXTENDS another Interface. But a Class IMPLEMENTS an Interface.

EXAMPLE : EXTENDING THE INTERFACE

interface A
{
    final static int num = 100;
    void show();
}
interface B extends A
{
    void display();
}

COMPARE : CLASS V/S INTERFACE

- Class are extended by another Class. / Interface are implemented by another Class.
- Methods of Class can be called using the object of the Class. / Method of Interface should be defined in the Class which implements the Interface.
- Variable of Class can be final or not. / Variable of Interface is always final.
- Class can extend only one Class. / Class can implement more than one Interface.
- A Class can define Constructors. / A Interface does not have a Constructor.
- Class can be in public, private or protected specifiers. / Interface is only in the public specifier.
- Objects of the Class can be created. / Object of an Interface cannot be created.

PROGRAMS!

Popular posts from this blog

Software Engineering & Project Mangement

Software Engineering - Software Quality Assurance & Security

Data Structures & Algorithms