Module 2 (Interfaces and Packages)
Module 2 (Interfaces and Packages)
abstract class A {
final class A { public final void
public abstract void methodOne() {}
methodOne(); }
}
• However, an abstract class can
Clearly, this implementation is invalid have a final method. This final
because a final class cannot have an method is treated like a normal
abstract method. As a final class method with a body which cannot
be overridden.
cannot be inherited.
ABSTRACT CLASSES
• Here, access is either public or not used. When no access specifier is included, then default access
results, and the interface is only available to other members of the package in which it is declared.
• When it is declared as public, the interface can be used by any other code. name is the name of the
interface, and can be any valid identifier.
• Notice that the methods which are declared have no bodies. They end with a semicolon after the
parameter list. They are, essentially, abstract methods; there can be no default implementation of any
method specified within an interface. Each class that includes an interface must implement all of the
methods.
• Variables can be declared inside of interface declarations. They are implicitly final and static, meaning
they cannot be changed by the implementing class. They must also be initialized with a constant value.
All methods and variables are implicitly public if the interface, itself, is declared as public.
Implementing Interfaces
• Once an interface has been defined, one or more classes can implement that interface.
• To implement an interface, include the implements clause in a class definition, and then create the
methods defined by the interface. The general form of a class that
• includes the implements clause looks like this:
• access class classname [extends superclass]
• [implements interface [,interface...]] {
• // class-body
• }
• Here, access is either public or not used. If a class implements more than one interface, the interfaces
are separated with a comma. If a class implements two interfaces that declare the same method, then
the same method will be used by clients of either interface. \
• The methods that implement an interface must be declared public. Also, the type signature of the
implementing method must match exactly the type signature specified in the interface definition.
• // Interface
• interface Animal {
• void sound(); // Abstract method
• void eat(); // Another abstract method
• }
• }}
• Notice that variable b is declared to be of the interface type Bank111, yet it
was assigned an instance of SBI class. Although b can be used to access
the rateOfInterest() method, it cannot access any other members of the
SBI class(had it been defined). An interface reference variable only has
knowledge of the methods declared by its interface declaration.
• Thus, b could not be used to access any concrete methods defined
under SBI class(if there are any)
Difference Between Abstract Class and Interface
Difference
Between Abstract
Class and Interface
Multiple inheritance in Java by interface
1.class A{
2.private A(){}//private constructor
If you make any class
3.void msg(){System.out.println("Hello java");}
4.} constructor private, you
5.public class Simple{ cannot create the
6. public static void main(String args[]){instance of that class
from outside the class.
7. A obj=new A();//Compile Time Error
8. }
9.}
PROTECTED ACCESS MODIFIER
• The protected access modifier is accessible within
package and outside the package but through
inheritance only.
• The protected access modifier can be applied on the
data member, method and constructor. It can't be
applied on the class.
PROTECTED ACCESS MODIFIER
/save by A.java
package pack;
package mypack;
public class A{ import pack.*;
protected void msg(){System.out.pr
intln("Hello");}
class B extends A{
} public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
Static Binding and Dynamic Binding
Connecting a method call to the method body is known as binding.
There are two types of binding
1.Static Binding (also known as Early Binding).
2.Dynamic Binding (also known as Late Binding).
static binding
When type of the object is determined at compiled time(by the compiler), it is
known as static binding.
If there is any private, final or static method in a class, there is static binding.
Dynamic binding
When type of the object is determined at run-time, it is
known as dynamic binding.
Java ‘instanceof’ Operator
The instanceof operator in Java is used to check whether an object is an instance of a particular class
or not.
Its syntax is
objectName instanceOf
Here, if objectName className;
is an instance of className, the operator returns true. Otherwise, it returns false.
JAVA PACKAGES
• User-defined Packages
JAVA PACKAGES
• Built-in Packages
• The built-in packages are the packages from java API. The Java API is a library of pre-defined classes, interfaces, and
sub-packages. The built-in packages were included in the JDK.
• There are many built-in packages in java, few of them are as java, lang, io, util, awt, javax, swing, net, sql, etc.
• Thus, the library is divided into packages and classes. Meaning you can
either import a single class (along with its methods and attributes), or a whole
package that contain all the classes that belong to the specified package.
• User-defined Packages
• The user-defined packages are the packages created by the user. User is free to create their own packages.
Defining a Package in java
• We use the package keyword to create or define a package in java programming language.
• Syntax:
• package packageName;
Using a package from library using import
To use a class or a package from the library, you need to use the import keyword:
import java.util.Scanner;
class MyClass
{ public static void main(String[] args)
{
Scanner myObj = new Scanner(System.in);
In the example, java.util is a
System.out.println("Enter username"); package, while Scanner is a class
String userName = myObj.nextLine(); of the java.util package.
System.out.println("Username is: " +
userName);
} }
Importing all the classes
• package myPackage;
• import java.util.*;
• public class ImportingExample
• {
• public static void main(String[] args)
• {
• Scanner read = new Scanner(System.in);
• int i = read.nextInt();
• System.out.println("You have entered a number " + i);
• Random rand = new Random();
• int num = rand.nextInt(100);
• System.out.println("Randomly generated number " + num);
• }
• }
Importing all the classes
• In the above code, the class ImportingExample belongs to myPackage package, and it also
importing all the classes like Scanner, Random, Stack, Vector, ArrayList, HashSet, etc. from
the java.util package.
• 🔔 The import statement imports only classes of the package, but not sub-packages and its classes.
• We may also import sub-packages by using a symbol '.' (dot) to separate parent package and sub-
package.
• Consider the following import statement.
•
import java.util.*;
• The above import statement util is a sub-package of java package. It imports all the classes
of util package only, but not classes of java package.
How to access package from another
package?
• If you use package.* then all the classes and interfaces of this package w
• The import keyword is used to make the classes and interface of anothe
EXAMPLE
//save by A.java
//save by B.java
package pack;
package mypack;
public class A{ import pack.*;
public void msg(){System.out.
println("Hello");} class B{
} public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Using packagename.classname
If you import package.classname then only declared class of this package will
be accessible.
/save by B.java
//save by A.java package mypack;
package pack; import pack.A;
public class A{
public void msg(){System.out.println("Hello");}
class B{
} public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Using fully qualified name
• If you use fully qualified name then only declared class of this
package will be accessible. Now there is no need to import. But
you need to use fully qualified name every time when you are
accessing the class or interface.
Using fully qualified name
//save by A.java
package pack;
//save by B.java
public class A{
package mypack;
public void msg(){System.out
.println("Hello");} class B{
public static void main(String args
} []){
pack.A obj = new pack.A();//using fu
lly
qualified name
obj.msg();
NOTE
• If you import a package, all the classes and interface of that
package will be imported excluding the classes and interfaces
of the subpackages. Hence, you need to import the subpackage
as well.
TASK 2
• Write a Java program to create a class called Shape with a
method called getArea(). Create a subclass called Rectangle
that overrides the getArea() method to calculate the area of a
rectangle.