RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit III
instanceof :
The java instanceof operator is used to test whether the object is an instance of the specified type
(class or subclass or interface).
The instanceof in java is also known as type comparison operator because it compares the instance
with type. It returns either true or false. If we apply the instanceof operator with any variable that
has null value, it returns false.
Simple example of java instanceof-
class Simple{
public static void main(String args[]){
Simple1 s=new Simple();
System.out.println(s instanceof Simple); //true
}
}
Annotation :
Annotations are used to provide supplemental information about a program.
1. Annotations start with ‘@’.
2. Annotations do not change the action of a compiled program.
3. Annotations help to associate metadata (information) to the program elements i.e. instance
variables, constructors, methods, classes, etc.
4. Annotations are not pure comments as they can change the way a program is treated by the
compiler. See below code for example.
5. Annotations basically are used to provide additional information, so could be an alternative
to XML and Java marker interfaces.
Example :
@Override
@SuppressWarnings
@Deprecated
1|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit III
Method Overriding :
If subclass (child class) has the same method as declared in the parent class, it is known as method
overriding in Java.
In other words, If a subclass provides the specific implementation of the method that has been
declared by one of its parent class, it is known as method overriding.
Usage of Java Method Overriding
• Method overriding is used to provide the specific implementation of a method which is
already provided by its superclass.
• Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
• The method must have the same name as in the parent class
• The method must have the same parameter as in the parent class.
• There must be an IS-A relationship (inheritance).
class Animal {
public void displayInfo() {
System.out.println("I am an animal.");
}
}
class Dog extends Animal {
@Override
public void displayInfo() {
System.out.println("I am a dog.");
}
}
class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.displayInfo();
}
}
2|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit III
Abstract Class and Abstract Method:
Data abstraction is the process of hiding certain details and showing only essential information to
the user.
Abstraction can be achieved with either abstract classes or interfaces (which you will learn more
about in the next chapter).
The abstract keyword is a non-access modifier, used for classes and methods:
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be
inherited from another class).
Abstract method: can only be used in an abstract class, and it does not have a body. The body is
provided by the subclass (inherited from).
An abstract class can have both abstract and regular methods:
abstract class Animal
{
public abstract void animalSound();
public void sleep()
{
System.out.println("Zzz");
}
}
Interface:
An Interface in Java programming language is defined as an abstract type used to specify the
behavior of a class. An interface in Java is a blueprint of a behavior. A Java interface contains 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 the method body. It is used to achieve abstraction and multiple inheritances
in Java using Interface. 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.
3|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit III
When we decide on a type of entity by its behavior and not via attribute we should define it as an
interface.Syntax for Java Interfaces
interface {
// declare constant fields
// declare methods that abstract
// by default.
}
To declare an interface, use the interface keyword. It is used to provide total abstraction. That
means all the methods in an interface are declared with an empty body and are public and all fields
are public, static, and final by default. A class that implements an interface must implement all the
methods declared in the interface. To implement the interface, use the implements keyword.
// A simple interface
interface Player
{
final int id = 10;
int move();
}
Interface fields are public, static and final by default, and the methods are public and abstract.
4|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit III
The relationship between classes and interfaces:
A class extends another class, an interface extends another interface, but a class implements an
interface.
Functional Interface:
A functional interface is an interface that contains only one abstract method. They can have only
one functionality to exhibit.
Functional Interface is additionally recognized as Single Abstract Method Interfaces. Functional
interfaces are included in Java SE 8 with Lambda expressions and Method references in order to
make code more readable, clean, and straightforward. Functional interfaces are interfaces that
ensure that they include precisely only one abstract method. Functional interfaces are used and
executed by representing the interface with an annotation called @FunctionalInterface.
An Interface that contains exactly one abstract method is known as functional interface. It can have
any number of default, static methods but can contain only one abstract method. It can also declare
methods of object class.
Functional Interface is also known as Single Abstract Method Interfaces or SAM Interfaces. It is a
new feature in Java, which helps to achieve functional programming approach.
@FunctionalInterface
interface Sample
{ void show();
}
5|Page
Study Notes (For Private Circular Only)
RENAISSANCE UNIVERSITY, INDORE
BCA IV SEM
Subject: Java Programming
Unit III
JAVA Package:
A java package is a group of similar types of classes, interfaces and sub-packages. A package in
Java is used to group related classes. Think of it as a folder in a file directory. We use packages to
avoid name conflicts, and to write a better maintainable code.
Package in java can be categorized in two form-
a) built-in package
b) user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
Advantage of Java Package:
1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
6|Page
Study Notes (For Private Circular Only)