Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Java Lecture-37
Topic: Chapter-5 Interface And Inner Classes

Interface
 An interface is just same as abstract class.

 The main difference between an interface and abstract class is, interface
is fully unimplemented, whereas abstract class is partially
unimplemented.

 It means by default all the methods in an interface are abstract.

 Syntax to declare an interface:

interface interface_name
{
void method1 ();
void method2 ();
……….
}

 Properties of an interface
1. By default all the methods in an interface are abstract.
2. The default access specifier for an interface is public.
3. Object of an interface cannot be created.
4. Reference of an interface can be created.
5. Inside interface the field is static by default.
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Implementing an Interface :
 Implementing on interface is similar to deriving from class except that
you are required to implement all methods defined in the interface.

 To implement on interface Java provide "implements" keyword.

 A class can implement multiple interfaces.

 Syntax to implement an interface

class <classname> implements <interface 1>, <interface 2>, ….


<interface n>
{
….
….
…. //body of class
}
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Difference between Abstract class and Interface

Abstract Class Interface


i) Abstract class can have i) Interface can have only abstract
abstract and non-abstract methods.
methods.
ii) It is partially unimplemented ii) It is fully un-implemented.
iii) It may contain instance field. iii) It may contain only static field.
iv) It contains constructor. iv) It does not contain constructor.
v) Default access specifier is v) Default access specifier is
default public.
vi) Abstract class can be extended vi) Interface is always implemented
to subclass. by the class.

Extending An Interface
 Like classes interfaces can also be extended.
 That is an interface can be sub interfaced from other interfaces.
 The new interface will inherit all the member of the super interface in
the manner similar to subclass. This is achieved using the keyword
extends.
 Syntax

interface subinterface extends superinterface


{
// body of subinterface
}

Points to Remember:
 One class can extends another class.
 One interface can extends another interface.
 Class can implements other interfaces.
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Java Lecture-27
Topic: Packages

Package
 In small projects, all the java files have unique names. So, it is not difficult
to put them in a single folder.

 But, in the case of huge projects where the number of java files is large, it is
very difficult to put files in a single folder because the manner of storing
files would be disorganized.

 Moreover, if different java files in various modules of the project have the
same name, it is not possible to store two java files with the same name in
the same folder because it may occur naming conflict.

 This problem of naming conflict can be overcome by using the concept of


packages.

 In Java, APIs consist of one or more packages where packages consist of


many classes, classes contain several methods and fields.

Package in Java
 A package is nothing but a physical folder structure (directories) that
contains a group of related classes, interfaces, and sub-packages according
to their functionality.

 It provides a convenient way to organize your work. The Java language has
various in-built packages.

 For example, java.lang, java.util, java.io, and java.net. All these packages
are defined as a very clear and systematic packaging mechanism for
categorizing and managing.

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Advantage of using packages in Java


1. Maintenance: Java packages are used for proper maintenance. If any
developer newly joined a company, he can easily reach to files needed.

2. Reusability: We can place the common code in a common folder so that


everybody can check that folder and use it whenever needed.

3. Name conflict: Packages help to resolve the naming conflict between


the two classes with the same name. Assume that there are two classes
with the same name Student.java. Each class will be stored in its own
packages such as stdPack1 and stdPack2 without having any conflict of
names.

4. Organized: It also helps in organizing the files within our project.

5. Access Protection: A package provides access protection. It can be used


to provide visibility control. The members of the class can be defined in
such a manner that they will be visible only to elements of that package.

Types of Packages in Java


There are two different types of packages in Java. They are:
1. Predefined Packages in Java (Built-in Packages)
Predefined packages in java are those which are developed by Sun
Microsystem. They are also called built-in packages in java. These
packages consist of a large number of predefined classes, interfaces, and
methods that are used by the programmer to perform any task in his
programs.

2. User-defined Package
The package which is defined by the user is called a User-defined
package. It contains user-defined classes and interfaces.

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Java Lecture-28
Topic: Creating Our Own Package

Creating our own package :


Following are the steps to create our own package-
i) Declare the package at the beginning of file using the following form
package <packagename>;

ii) Define the class that is to be put in the package and declare it public.

iii) Create a subdirectory under the directory where the main source file is
stored.

iv) Store the Java file in the sub-directory created.

v) Compile the file. This create (.class) file in the sub-directory.

vi) Provide the import statement in the main program to import the
classes of our package.

Program to create our own package


In the following program we will create “info” package with two classes
“Emp” and “Student” and one nested package “planet” with one class
“Teacher”. Finally we import these classes in application class called “Test”.
The directory structure of this program is as follows.
C:\JavaPrograms (folder)
Test.java (Main class)
info (folder)
Emp.java
Student.java
planet (folder)
Teacher.java

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

import Statements :
There are two methods to create an object of class declared in other
package.
1) By using full qualified Name :
In this method, we have to add the full package name in front of every
classname. For Example
class test
{
public static void main (String [] args)
{
java.util.Date d = new java.util.date();
System.out.println(d);
}
}

2) By Using import statement :


This method is simpler and more common. Once we use import, we no
longer have to give classes with their full qualified name.
We can import a specific class from package on the whole package.
Syntax :
import package.classname; //to import specific class
or
import package.*; //whole package.

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260

You might also like