B08 OOP Writeup2
B08 OOP Writeup2
1)What is Interface in Java. Write Java Syntax for Interface. Write Java
Program example to declare and implement Interface in Class.
Definition: An interface in Java is a reference type, similar to a class, that can
contain only constants, method signatures, default methods, static methods,
and nested types. Interfaces cannot contain instance fields or constructors.
They are used to specify a set of methods that a class must implement,
promoting a form of abstraction and multiple inheritance.
Syntax of an Interface:
interface InterfaceName {
// abstract methods
void method1();
void method2();
// default method
default void defaultMethod() {
// method body
}
// static method
static void staticMethod() {
// method body
}
}
Example Program:
// Defining the interface
interface Animal {
void eat();
void sleep();
}
// Implementing the interface in a class
class Dog implements Animal {
public void eat() {
System.out.println("Dog is eating.");
}
public void sleep() {
System.out.println("Dog is sleeping.");
}
}
1. What is relation between Interface and Class. Can single class implement
two Interface? If yes write Java Syntax to how single class implements two
Interfaces. Also write Java Program to show single class implements two
interface and add logic to all methods.
Relationship Between an Interface and a Class: In Java, an interface defines a
contract of abstract methods that a class can implement. While a class
represents a blueprint of objects with attributes and behaviors, an interface
specifies a set of methods that the class agrees to implement. This allows for a
separation of the "what" (interface) from the "how" (class implementation),
promoting flexibility and scalability in code design.
Implementing Multiple Interfaces: Yes, a single class in Java can implement
multiple interfaces. This is a way to achieve multiple inheritance, as Java does
not support multiple inheritance through classes.
Java Syntax for a Class Implementing Two Interfaces:
interface Interface1 {
void method1();
}
interface Interface2 {
void method2();
}
class MyClass implements Interface1, Interface2 {
public void method1() {
// Implementation of method1
}
public void method2() {
// Implementation of method2
}
}
Example Program:
// Defining the first interface
interface Animal {
void eat();
}
// Defining the second interface
interface Pet {
void play();
}
// Implementing both interfaces in a single class
class Dog implements Animal, Pet {
public void eat() {
System.out.println("Dog is eating.");
}
public void play() {
System.out.println("Dog is playing.");
}
}
// Main class to test the implementation
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat();
myDog.play();
}
}
Diagram:
+ + + +
| Animal | | Pet |
| | | |
| + eat() | | + play() |
+ + + +
^ ^
| |
| |
+ + +
|
+ +
| Dog |
| |
| + eat() |
| + play() |
+ +
2. List Down One Real-Time Example Where: a. Java Class Uses a Single
Interface b. Java Class Uses Two Interfaces
a. Java Class Uses a Single Interface:
Real-Time Example: Consider a payment system where different payment
methods are implemented. Each payment method can be represented by a
class implementing a common interface.
Interface Definition:
interface Payment {
void processPayment(double amount);
}
+ +
| Payment | <--- Interface
| |
| + processPayment() |
+ +
^
|
|
+ +
| CreditCardPayment | <--- Implements Payment
| |
| + processPayment() |
+ +
interface Scannable {
void scanDocument(String document);
}
Diagram:
+ +
| Superclass |
+ +
|
v
+ +
| Subclass |
+ +
Example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
Example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
interface Pet {
void play();
}
class Dog implements Canine, Pet {
public void bark() {
System.out.println("The dog barks.");
}
Java Syntax:
class Superclass {
// fields and methods
}
| | |
Teacher Student AdministrativeStaff
| |
| AccountDepartment
|
Evaluatable (Interface)
void getDetails() {
System.out.println("Name: " + name + ", Address: " + address);
}
}
// Subclass: Teacher
class Teacher extends Person implements Evaluatable {
String employeeID;
String subjects;
void prepareLesson() {
System.out.println("Lesson prepared.");
}
}
// Subclass: Student
class Student extends Person {
String studentID;
String courses;
void viewGrades() {
System.out.println("Viewing grades...");
}
}
// Subclass: AdministrativeStaff
class AdministrativeStaff extends Person {
String staffID, department;
void manageRecords() {
System.out.println("Managing records...");
}
}
void manageBudget() {
System.out.println("Managing budget...");
}
}
// Main Class
public class EducationSystem {
public static void main(String[] args) {
Teacher t1 = new Teacher();
t1.name = "John Doe";
Student s1 = new Student();
s1.name = "Alice";
t1.evaluateStudent(s1);
t1.assignGrades(s1);
}
}
7. What is a Java Package? What is the Use of Java Packages? What Directory
Structure is Followed When We Create a Package? What Naming Conventions
are Used When We Write Java Packages?
Java Package: A package in Java is a namespace that organizes a set of related
classes and interfaces. Think of it as a folder in a file directory that contains
related files. Packages help in grouping related classes, preventing naming
conflicts, and controlling access.
Uses of Java Packages:
1. Organization: Packages organize classes and interfaces into a structured
hierarchy, making the codebase more manageable.
2. Avoiding Naming Conflicts: By grouping classes into packages, classes
with the same name can exist in different packages without conflict.
3. Access Control: Packages provide access protection; classes within the
same package can access each other's package-private members.
4. Reusability: Packages allow developers to reuse classes and interfaces
across different projects.
Directory Structure When Creating a Package: The directory structure of a Java
package corresponds to its namespace. For example, a package named
com.example.project would have the following directory structure:
com/
└── example/
└── project/
Each dot (.) in the package name represents a new directory level.
Naming Conventions for Java Packages:
11. Proposed Package Structure for the Library Management System Mini
Project
Designing an organized package structure is crucial for the maintainability and
scalability of a Java project. For the Library Management System (LMS) mini
project, a feature-based package organization is recommended, as it groups
related classes by functionality, enhancing modularity and readability.
Proposed Package Structure:
1. com.librarymanagement
o **model**
▪ Contains classes representing the core entities of the
system.
▪ Classes:
▪ Book
▪ User
▪ Member
▪ Librarian
▪ Transaction
o **service**
▪ Contains classes that implement the business logic and
operations.
▪ Classes:
▪ BookService
▪ UserService
▪ TransactionService
o dao (Data Access Object)
▪ Manages data persistence and retrieval from the database.
▪ Classes:
▪ BookDAO
▪ UserDAO
▪ TransactionDAO
o **ui**
▪ Handles the user interface components of the application.
▪ Classes:
▪ MainMenu
▪ BookUI
▪ UserUI
▪ TransactionUI
o **util**
▪ Contains utility classes and helper functions.
▪ Classes:
▪ DatabaseConnection
▪ DateUtil
Explanation of Packages:
• model Package:
o Defines the data structures and entities used within the LMS.
o Example: The Book class would include attributes like bookID, title,
author, and methods such as getDetails() and checkAvailability().
• service Package:
o Implements the core functionalities and business logic.
o Example: The BookService class might include methods like
addBook(), removeBook(), and searchBooks().
• dao Package:
o Manages interactions with the database, ensuring data is correctly
stored and retrieved.
o Example: The BookDAO class would handle SQL operations related
to the Book entity.
• ui Package:
o Manages the user interface, facilitating user interactions with the
system.
o Example: The MainMenu class could present options like "Add
Book", "Issue Book", and "Return Book" to the user.
• util Package:
o Provides utility functions and common tools used across the
application.
o Example: The DatabaseConnection class would manage the
connection to the database, while DateUtil might offer date
formatting utilities.