0% found this document useful (0 votes)
9 views6 pages

Abhayraj Singh Exp-5 OOPL (1)

The document outlines an experiment for a course on Object Oriented Programming at K. J. Somaiya College of Engineering, focusing on the use of interfaces in Java. It includes code examples for implementing a stack interface and a college class that utilizes student and teacher interfaces. The conclusion emphasizes the role of interfaces in achieving abstraction and multiple inheritance in Java.

Uploaded by

Abhay raj Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Abhayraj Singh Exp-5 OOPL (1)

The document outlines an experiment for a course on Object Oriented Programming at K. J. Somaiya College of Engineering, focusing on the use of interfaces in Java. It includes code examples for implementing a stack interface and a college class that utilizes student and teacher interfaces. The conclusion emphasizes the role of interfaces in achieving abstraction and multiple inheritance in Java.

Uploaded by

Abhay raj Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

K. J.

Somaiya College of Engineering, Mumbai-77


(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics and Telecommunication
Engineering

Course Name: Object Oriented Programming Language Semester: IV


Date of Performance: 05 / 02 / 2025 Batch No: Batch A1
Computer and Communication
Programme Name: Roll No: 16014123005
engineering

Experiment No: 5
Title: Use of Interface

Aim and Objective of the Experiment:


Learn the how to use Interfaces

COs to be achieved:
CO2: Understand concepts of Object Oriented Programming and basic characteristics of Java.

Tools used: Visual Studio Code Editor, Codeblock Vlabs


Theory:
1. (About Interfaces)

Code:
1. Write a program to design interface named stack with the following methods
a. push and pop elements from the stack
interface Stack {
void push(int item);
int pop();
boolean isEmpty();
boolean isFull();
}

class ArrayStack implements Stack {


private int[] stack;
private int top;
private int size;

public ArrayStack(int size) {


this.size = size;
stack = new int[size];
top = -1;
}

Object Oriented Programming Semester: IV Academic Year: 2024-25


Roll No:_____________
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics and Telecommunication
Engineering

public void push(int item) {


if (isFull()) {
System.out.println("Stack is full");
return;
}
stack[++top] = item;
}

public int pop() {


if (isEmpty()) {
System.out.println("Stack is empty");
return -1;
}
return stack[top--];
}

public boolean isEmpty() {


return top == -1;
}

public boolean isFull() {


return top == size - 1;
}
}

b. check for stack empty and full using method


interface Stack {
void push(int item);
int pop();
boolean isEmpty();
boolean isFull();
}

class ArrayStack implements Stack {


private int[] stack;
private int top;
private int size;

public ArrayStack(int size) {


this.size = size;

Object Oriented Programming Semester: IV Academic Year: 2024-25


Roll No:_____________
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics and Telecommunication
Engineering

stack = new int[size];


top = -1;
}

public void push(int item) {


if (isFull()) {
System.out.println("Stack is full");
return;
}
stack[++top] = item;
System.out.println("Pushed: " + item);
}

public int pop() {


if (isEmpty()) {
System.out.println("Stack is empty");
return -1;
}
System.out.println("Popped: " + stack[top]);
return stack[top--];
}

public boolean isEmpty() {


return top == -1;
}

public boolean isFull() {


return top == size - 1;
}
}

2. Design class of college which uses two interfaces named student and teacher. Class college
will use methods declared in interface to display student and teacher personal information.
interface Student {
void showStudentInfo();
}

interface Teacher {
void showTeacherInfo();
}

Object Oriented Programming Semester: IV Academic Year: 2024-25


Roll No:_____________
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics and Telecommunication
Engineering

class College implements Student, Teacher {


private String studentName;
private int studentID;
private String teacherName;
private String subject;

public College(String studentName, int studentID, String teacherName, String subject) {


this.studentName = studentName;
this.studentID = studentID;
this.teacherName = teacherName;
this.subject = subject;
}

public void showStudentInfo() {


System.out.println("Student Name: " + studentName);
System.out.println("Student ID: " + studentID);
}

public void showTeacherInfo() {


System.out.println("Teacher Name: " + teacherName);
System.out.println("Subject: " + subject);
}

public static void main(String[] args) {


College obj = new College("Alice", 101, "Mr. John", "Computer Science");
obj.showStudentInfo();
obj.showTeacherInfo();
}
}

Output:
For Stack Implementation

Stack is empty

Pushed: 10

Pushed: 20

Popped: 20

Object Oriented Programming Semester: IV Academic Year: 2024-25


Roll No:_____________
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics and Telecommunication
Engineering

Popped: 10

Stack is empty

For College Implementation

Student Name: Alice

Student ID: 101

Teacher Name: Mr. John

Subject: Computer Science

Post Lab Subjective/Objective type Questions:


1. What is interface? How is it different from an abstract class?
Answer : An interface is a collection of abstract methods that allows multiple inheritance.
It differs from an abstract class because it cannot have instance variables or constructors,
and all methods are by default abstract.

2. Explain how multiple inheritance can be implemented using interfaces.


Answer : Java does not support multiple inheritance in classes but allows it through
interfaces. A class can implement multiple interfaces, thus inheriting multiple
functionalities without conflicts.

3. What will be function of the following code ? will this code work ? if not, why ?

Object Oriented Programming Semester: IV Academic Year: 2024-25


Roll No:_____________
K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Electronics and Telecommunication
Engineering

Conclusion:
 Interfaces help achieve abstraction and multiple inheritance in Java.
 They define a contract for implementing classes to follow.
 The experiment demonstrated the use of interfaces in stack operations and college data
representation.

Signature of faculty in-charge with Date:

Object Oriented Programming Semester: IV Academic Year: 2024-25


Roll No:_____________

You might also like