[go: up one dir, main page]

0% found this document useful (0 votes)
35 views54 pages

Unit 4 Part 2 Package and Interfaces

Uploaded by

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

Unit 4 Part 2 Package and Interfaces

Uploaded by

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

SNJB’s Late Sau. K. B. J.

College of Engineering

SE Computer- Division A
Course Name :Principle of Programming Language
Course Code: 210256
Course InCharge: Kainjan M. Sanghavi

Thanks to Khyati R. Nirmal ..to prepare the slides

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Unit 4
Inheritance, Packages and Exception Handling using Java

● Packages and Interfaces: defining a package, finding packages and


CLASSPATH,
● access protection,
● importing packages,
● interfaces (defining, implementation, nesting, applying),
● variables in interfaces,
● extending interfaces,
● instance of operator.

Departmentof
Department ofComputer
ComputerEngineering
Engineering
SNJB’s Late Sau. K. B. J. College of Engineering

What is an interface?

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Interface in Java

● An interface in Java is a blueprint of a class.


● It has static constants and abstract methods.
● The interface in Java is a mechanism to achieve abstraction. There
can be only abstract methods in the Java interface, not method body.
It is used to achieve abstraction and multiple inheritance in Java.
● In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body.
● Java Interface also represents the IS-A relationship.
● It cannot be instantiated just like the abstract class.

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Why Interface?

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Interface in Java

● All variables declared inside interface are implicitly public, static


and final.
● All methods declared inside interfaces are implicitly public and
abstract, even if you don't use public or abstract keyword.
● Interface can extend one or more other interface.
● Interface cannot implement a class.
● Interface can be nested inside another interface.

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
New features added in interfaces

● JDK -8 - allows the interfaces to have default and


static methods.
● JDK 9 onwards has some changes, interfaces can
contain the following.
● Static methods
● Private methods
● Private Static methods

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Syntax for Java Interface

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Java Interface Example

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Java Interface Example

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Interface implementation : A. java
interface printable{

void print();

}
Output:
class A implements printable{
Hello
public void print(){System.out.println("Hello");}

public static void main(String args[]){

A obj = new A();

obj.print();

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Interface implementation : TestInterface1. java
//Interface declaration class TestInterface1{

interface Drawable{ public static void main(String args[]){

void draw();

} Drawable d=new Circle();

//Implementation d.draw();

class Rectangle implements Drawable{ }}

public void draw()

{System.out.println("drawing rectangle");}

}
Output:
class Circle implements Drawable{ drawing circle
public void draw(){System.out.println("drawing circle");}

} Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Multiple inheritance in Java by interface

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Interfaces supports Multiple Inheritance : Tyre.java

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Interface extends other Interface

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
The relationship between classes and interfaces

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Default Methods In Java 8

● Before Java 8, interfaces could have only abstract methods.

● The implementation of these methods has to be provided in a separate

class. So, if a new method is to be added in an interface, then its

implementation code has to be provided in the class implementing the same

interface.

● To overcome this issue, Java 8 has introduced the concept of default

methods which allow the interfaces to have methods with implementation

without affecting the classes that implement the interface.

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Default Methods In Java 8 : TestClass.java


// A program to Test Interface default methods public static void main(String args[])
interface TestInterface {
{ TestClass d = new
// abstract method TestClass();
public void square(int a); d.square(4);

// default method // default method


default void show() executed
{ d.show();
System.out.println("Default Method Executed"); }
} }
}

class TestClass implements TestInterface


{ Output:
// implementation of square abstract method
public void square(int a) 16
{ Default Method Executed
System.out.println(a*a);
}

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Private methods – Java 9 : Demo.java

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Packages In Java

● Package is a collection of related classes

● We can assume package as a folder or a directory that is used to store

similar files.

● packages are used to avoid name conflicts and to control access of class,

interface and enumeration etc

● Using package it becomes easier to locate the related classes and it also

provides a good structure for projects with hundreds of classes and

other files.
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

● Package names are dot separated, e.g., java.lang.


● Packages Avoid namespace collision. There can not be
two classes with same name in a same Package But two
packages can have a class with same name.
● Exact Name of the class is identified by its package
structure.

<< Fully Qualified Name>>


● java.lang.String ; java.util.Arrays; java.io.BufferedReader ;
java.util.Date

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

● Package statement must be first statement in the program even before the

import statement.

● A package is always defined as a separate folder having the same name as

the package name.

● Store all the classes in that package folder.

● All classes of the package which we wish to access outside the package

must be declared public.

● All classes within the package must have the package statement as its first

line.

● All classes of the package must be compiled before use


Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering

Creating Your Own Package

● Creating a package in java is quite easy, simply include a package

command followed by name of the package as the first

statement in java source file. Creates a package


package mypack; (directory) mypack
in current directory
public class employee

String empId;

String name;

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Package Program
//save as FirstProgram.java
Creates a package
package learnjava; (directory) learnjava
in current directory
public class FirstProgram{

public static void main(String args[]) {

System.out.println("Welcome to package example");

}
}
Compilation of Package Prog : javac -d . FirstProgram.java
The -d switch specifies the destination where to put the generated class file. You
can use any directory name like d:/abc (in case of windows) etc. If you want to

keep the package within the same directory, you can use . (dot).
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering

Execution of Package Prog : java learnjava.FirstProgram

To run the compiled class that we compiled using above command,

we need to specify package name too

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Finding Packages and CLASSPATH

How does the Java runtime system know


where to look for packages that you create?
The answer has three parts.

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Finding Packages and CLASSPATH

First, by default, the Java run- time system uses the


current working directory as its starting point.
Thus, if your package is in a subdirectory of the
current directory, it will be found.

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Finding Packages and CLASSPATH

Second, you can specify a directory path or


paths by setting the CLASSPATH
environmental variable.

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Finding Packages and CLASSPATH

Third, you can use the -


classpath option with java and javac to
specify the path to your classes.

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

PATH and CLASSPATH

● PATH is an environment variable which is


used to locate JDK binaries like "java" or
"javac" command used to run java program
and compile java source file.
● On the other hand, CLASSPATH, an
environment variable is used by System or
Application ClassLoader to locate and load
compile Java bytecodes stored in the .class
file.
Department of Computer Engineering
SNJB’s Late Sau. K. B. J. College of Engineering

PATH and CLASSPATH


set PATH=%PATH%;C:\Program Files\Java\JDK1.6.20\bin

set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\JDK1.6.20\lib

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Packages and Member Class

● packages, Java addresses four categories of


visibility for class members:
○ Subclasses in the same package
○ Non-subclasses in the same package
○ Subclasses in different packages
○ Classes that are neither in the same package
nor subclasses

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Packages and Member Class

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Install and Run Eclipse

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
When Run Eclipse it asks to Select the workspace directory

After Selecting the workspace directory Click Ok.

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
If any Thing any file Seen on Eclipse Editor previously then select
Close All Perspectives…In Window Drop down menu

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Click on Window Menu and Select Option Open Perspective
Select Java and Ok

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

For creating a Java package, you need to follow the below steps.

go to the File Menu, then

File > New > Package

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Now, a dialog box opens up, where you need to fill the source folder of package and
the package name

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Example , Create TestPack package in PackageTest/src

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

After clicking Finish above, the package gets created successfully as shown below,

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering

Code for Demonstration of Package and Interface

Click Here

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Java instanceof Operator

● to check whether an object is an instance of a particular


class or not.
● It’s Syntax is:

Here, if objectName is an instance of className, the


operator returns true. Otherwise, it returns false.

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Java instanceof Example : Main.java

public class Main {

public static void main(String[] args) {

Main myObj = new Main();

System.out.println(myObj instanceof Main); //


returns //true

} Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Casting Objects and the instanceof Operator

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Casting Objects and the instanceof Operator

Department of Computer Engineering


SNJB’s Late Sau. K. B. J. College of Engineering
Solution downcasting with instanceof : Child.java

Department of Computer Engineering

You might also like