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.");
}
}
^
|
|
|
Dog
|
+ eat()
+ sleep()
|
||
++
++
| <--- Implements Animal
||
|
||
++
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
+ +
| 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
}
1. Class: Book o
Attributes:
▪ bookID: Unique identifier for each book.
▪ title: Title of the book.
▪ author: Author of the book.
▪ publisher: Publisher of the book.
▪ isAvailable: Boolean indicating the availability status. o
Methods:
▪ getDetails(): Returns the details of the book.
▪ checkAvailability(): Checks if the book is available for
borrowing.
2. Class: User o
Attributes:
▪ userID: Unique identifier for each user.
▪ name: Name of the user.
▪ email: Email address of the user.
▪ phone: Contact number of the user. o Methods:
▪ register(): Registers a new user.
▪ updateProfile(): Updates user profile information.
3. Class: Member
(inherits from
User)
o Attributes:
Methods:
▪ borrowBook(Book book): Allows a member to borrow a
book.
▪ returnBook(Book book): Allows a member to return a
borrowed book.
4. Class: Librarian (inherits from User)
o Attributes:
a. Education Domain
The Education Management System consists of multiple entities like teachers,
students, administrative staff, and accounts. Using inheritance and
interfaces, we can design a structured and scalable system.
| | |
Teacher Student AdministrativeStaff
| |
| AccountDepartment
|
Evaluatable (Interface)
Code Representation in Java
// Superclass
class Person {
String personID, name, address, phone;
void getDetails() {
System.out.println("Name: " + name + ", Address: " + address);
}
}
// Subclass: Teacher
class Teacher extends Person implements Evaluatable {
String employeeID;
String subjects;
void viewGrades() {
System.out.println("Viewing grades...");
}
}
void manageRecords() {
System.out.println("Managing records...");
}
}
// Independent Class: AccountDepartment
class AccountDepartment {
double budget;
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:
com/
└── example/
└── project/
Each dot (.) in the package name represents a new directory level.
Naming Conventions for Java Packages:
• Lowercase Letters: Package names are written in all lowercase letters
to avoid conflicts with class or interface names. cite turn0search0
• Reverse Domain Name: Organizations often use their reversed
Internet domain name to begin their package names. For example, a
company with the domain example.com would use com.example as
the base package name. cite turn0search0
• Concatenation Without Underscores: When a package name is
composed of multiple words, they are concatenated without
underscores. For instance, formvalidator instead of form_validator.
cite turn0search2
8. What is a Built-in Package in Java? Explain with Names of Packages.
Built-in Packages in Java: Built-in packages are predefined packages provided
by the Java Development Kit (JDK). They contain a vast collection of classes
and interfaces that facilitate various programming tasks, such as data
structures, networking, and input/output operations.
Examples of Built-in Packages:
3. java.io: Includes classes for input and output operations, like File,
InputStream, OutputStream, Reader, and Writer.
5. java.awt: Contains classes for creating user interfaces and for painting
graphics and images.
7. java.sql: Offers classes and interfaces for accessing and processing data
stored in a data source using the Java programming language.
These built-in packages simplify development by providing ready-to-use
classes and interfaces for common tasks.
Step Command
Create and compile a package javac -d . MyPackageClass.java
Compile the class using the package javac TestPackage.java
Run the program java TestPackage
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**