0% found this document useful (0 votes)
19 views20 pages

Chapter - 8 - Interface and Package

Uploaded by

dileepdilee8747
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views20 pages

Chapter - 8 - Interface and Package

Uploaded by

dileepdilee8747
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Chapter -8

Interfaces, Packages &


Generic Programming
Interfaces
• An interface in Java is a blueprint of a class.
• An interface is a programming construct which contains
only public abstract methods and public static final
variables.
• Syntax
interface interfacename
{
public static final variables;
public abstract methods;
}
Example:
interface Animal
{
public static final int age=10;
public abstract void eat();
public abstract void travel();
}
All variables in an interface are by default public
static and final
All methods in an interface are by default public and
abstract
Hence no need to mention it explicitly
interface Animal
{
int age=10;
void eat();
void travel();
}
Extending Interfaces
• The process of an interface extending another interface is
known as Extending Interfaces.
• When an interface wants to extend another interface, it
uses the keyword extends.
Eg:
interface i1{
void method1();
}
interface2 i2{
void method2();
}
Interface i3 extends i1,i2{
void method3();
}
Implementing Interfaces
• The implements keyword is used to implement
an interface.
• To implement an interface, a class provide bodies
for the methods described by the interface is
used.
Syntax:
Class ClassName implements intefaceName
{
<body of the class>
}
Example:
interface i1{
void method1();
}
interface2 i2{
void method2();
}
Class MyClass implements i1, i2
{
void method1()
{
}
void method2()
{
}
Packages in java
• A Package can be defined as a grouping of
related types (classes, interfaces) etc.
• It provides a mechanism by which related pieces of a
program can be organized as a unit.
• Packages are used in Java in order to prevent naming
conflicts, to control access, to make
searching/locating and usage of classes, interfaces.
• Some of the existing packages in Java are −
• java.lang − bundles the fundamental classes
• java.io − classes for input , output functions
are bundled in this package
• Java packages are classified into two types
– Java API packages
– User defined packages
Java API packages
• It contain the built in class provided by the
java. Packages are organised in a hierarchical
structure. A packages name should be unique.
Defining user defined packages
– To create our own package, put a package command
at the top of the java source file. The classes declared
within that file will then belong to the specified
package.
package pkg;
– Java uses file system to manage packages, with each
package stored in its own directory.
– We can create hierarchy of packages by simply
separate each package name with a period.
package pack1.pack2.pack3;
the classes in that java file will be stored in
…/pack1/pack2/pack3
Create a package ‘student.Fulltime.BCA’ in your current working
directory. Create a default class in the above package wit the
following a. Attributes : name, age, sex b. Methods for storing
as well as displaying
• Compile the package

• Observe that the folders student , inside that Fulltime, inside


that BCA is generated and Student.class file generated inside
BCA folder
• Using Java API package
– If we want to use a class in a particular package say in
java.util, we need to import the package in our program
using import keyword and after that we can use all the
classes from that package.
import java.util.*;
– If we want to import only one class called Vector from
that package, we can import only that class.
import java.util.Vector;
– If we don’t want to import the class, then we can create
an object of Vector class with its full package name
java.util.Vector v = new java.util.Vector();
– If the package contain sub packages, we have to import
the sub package as well if we have to use the classes in
that sub package
import java.awt.event.*;
Access Modifiers or Visibility Modifiers
• Public
• Private
• Default
• Protected
refer previous notes
Generic programming
• Generic programming is a style of computer
programming in which algorithms are written in
terms of types to-be-specified-later state.
• Generics means parameterized types. The
classes, which accept one or more
parameters, ​are known as parameterized classes
or parameterized types. (Integer, String, … etc.,
and user-defined types).
• We can write generic methods and generic
classes in java.
• It allows programmer to use generic types and
functions and thus ensure type safety.
Advantages
• Type casting is not required: There is no need
to typecast the object.
• Compile-Time Checking: It is checked at
compile time so problem will not occur at
runtime.
• Code reusability
Java Generics Class

• We can create a class that can be used with


any type of data. Such a class is known as
Generics Class.
• We can define our own classes with generics
type.
• Syntax:
• // To create an instance of generic class
BaseType <Type> obj = new BaseType
<Type>()
Ex:
Generic Methods
• We can create a method that can be used
with any type of data. Such a method is
known as Generic method.
• We can also use generic methods when we do
not need the entire class to be generic.
Eg:

You might also like