You are on page 1of 21

PACKAGES AND

INTERFACES IN
JAVA
Submitted To-: Submitted By-:
Dr Geeta Rani Vinay Sharma (2010981550)
Assistant professor Phalesh (2010981543)
INTERFACE
• A Java interface is a collection of abstract S
methods and
constants
• An abstract method is a method header without a
method body
• An abstract method can be declared using the modifier
abstract, but because all methods in an interface are
abstract, usually it is left off
• An interface is used to establish a set of methods that a
class will implement
Interfaces
interface is a reserved word
None of the methods in
an interface are given
public interface Doable a definition (body)
{
public void doThis();
public int doThat();
public void doThis2 (float value, char ch);
public boolean doTheOther (int num);
}

A semicolon immediately
follows each method header
Interfaces
.An interface cannot be instantiated
.Methods in an interface have public visibility by default
.A class formally implements an interface by:
◦ stating so in the class header
◦ providing implementations for each abstract method in the
interface

.If a class asserts that it implements an interface, it must


define all methods in the interface
Comparison with Inheritance
.Interfaces don’t define any method actions… you need to fill in all the details
.It essentially just gives a basic collection of method names
.Not a strict hierarchy
.Important: can implement several Interfaces
.Can use this to “fake” multiple inheritance
MCQ
1. Package in Java is a mechanism to encapsulate a ______________.

A. Classes
B. Sub Packages
C. Interfaces
D. All of the above

2. Which of these keywords is used to define packages in Java?

A. pkg
B. Pkg
C. package
D. Package
3. Package names and directory structure are closely related.

A. TRUE
B. FALSE
C. can be true or false
D. can not say

4. An _______________ statement can be used to access the classes and interface of a different package
from the current package.

A. instanceOf
B. import
C. extends
D. implement
5. Which of the following packages is used to includes classes to create user interface like Button and
Checkbox?
A. java.lang
B. java.net
C. java.awt
D. java.io

6) An interface in Java is like a 100% ____.


A) abstract class
B) public class
C) inner class
D) anonymous class

7) A Java Interface is not considered a class. State TRUE or FALSE.


A) TRUE
B) FALSE
C) Can be true or false
D) can’t say
8) Choose a correct statement about Java Interfaces?
A) Interface contains only abstract methods by default.
B) A Java class can implement multiple interfaces
C) An Interface can extend or inherit another Interface.
D) All the above

9 – A Java Class inherits Constants and Methods of an Interface using ____ keyword.


A) INTERFACE
B) IMPLEMENTS
C) EXTENDS
D) All the above
1.D
2.C
3.A
4.B
5.C
6.A
7.A
8.D
9.B
THANK YOU!

You might also like