Java Programming Lab
Session 4
By : Dr. Amar Arora
USAR, GGSIPU-EDC
Package Statement in Java
• The package statement is used to group related classes, interfaces, and
sub-packages together.
• It provides a way to organize classes into namespaces, which helps in
avoiding naming conflicts and improves code readability and
maintainability.
E.g.
1. package javalab;
2. package [Link];
3. package [Link].lab1;
Import Statements
The import statements is used to make classes and interfaces from other packages available to your program.
It can be done in two ways:
1. Including a specific ClassName
import [Link];
1. Including all Classes and Interfaces within a package using wildcard (*)
import [Link].*;
Note: It's important to note that the import statement is not required for classes that are in the same package as your current
class. You can simply refer to those classes by their name.
Inheritance in Java
• Inheritance in Java is a mechanism in which one object acquires all the properties
and behaviors of a parent object [javapoint].
• It is the mechanism in java by which one class is allowed to inherit the features(fields
and methods) of another class [geeksforgeeks].
• Inheritance is one of the key features of OOP that allows us to create a new class from
an existing class [programiz].
Types of Inheritance [[Link]]
Types of Inheritance In Java
● Multiple and Hybrid Inheritance in Java is supported via interfaces
but not through classes.
Single Inheritance
Syntax:
class HttpClient {
// data members and functions for HttpClient functionality
}
class HttpsClient extends HttpClient {
// additional data members and functions for Https protocol within the existing HttpClient functionality
}
Multilevel Inheritance
Syntax:
class HttpClient {
// data members and functions for HttpClient functionality
}
class HttpsClient extends HttpClient {
// additional data members and functions for Https protocol within the existing HttpClient functionality
}
class SecureClient extends HttpsClient {
// additional data members and functions for login based security embedded within the existing HttpsClient functionality
}
Abstract Class
● Abstract class is a class that is declared with the abstract keyword.
● It may have both abstract and non-abstract methods (methods with bodies).
● An abstract class is a Java modifier applicable for classes and methods in Java but not for
variables.
● An instance of an abstract class cannot be created.
● Constructors are allowed.
● We can have an abstract class without any abstract method.
● There can be a final method in an abstract class but any abstract method in a class must be
implemented in its subclass.
Abstract Class (Example)
abstract class SecureLogin {
public String randomizedhashing(String password){
//Definition of the method
return null;
}
public abstract String generateCSRFToken();
}
class LoginPage extends SecureLogin
{
@Override
public String generateCSRFToken(){
//Definition of the method
return null;
}
}
Interface
An interface is a collection of abstract methods. A class that implements an interface must implement all of the
methods defined in that interface. An interface cannot have instance variables or non-abstract methods.
Differences between abstract classes and interfaces:
● Abstract classes can have instance variables and non-abstract methods, while interfaces cannot.
● A class can only extend one abstract class, but it can implement multiple interfaces.
● Abstract classes can have protected and package-private access, while interfaces can only have public
access.
● Abstract classes can provide a default implementation for some methods, while interfaces cannot.
Interface (Example)
public interface SecureLogin {
public String randomizedhashing(String password);
public String generateCSRFToken();
}
class LoginPage implements SecureLogin
{
@Override
public String randomizedhashing(String password){
//Definition of the method
}
@Override
public String generateCSRFToken(){
//Definition of the method
}
}
Program to Execute
7. Make a class Calculator to provide basic functions
like addition, subtraction, etc. as the member
functions and upgrade it to the ScientificCalcualtor by
inheritance by adding log and exponent functionality.
8. Working on the similar lines, further upgrade
ScientificCalcualtor to SeriesEnabledCalculator by
adding Fibonnacci series functionality.