0% found this document useful (0 votes)
7 views4 pages

Java Object-Oriented Programming Interfaces

This document provides a comprehensive overview of Object Oriented Programming concepts in Java, specifically focusing on interfaces. It covers various aspects such as abstract methods, static methods, private methods, and interface inheritance, along with practical examples. Key topics include the implementation of interfaces, method overriding, and polymorphism in Java.

Uploaded by

manjusince2006
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)
7 views4 pages

Java Object-Oriented Programming Interfaces

This document provides a comprehensive overview of Object Oriented Programming concepts in Java, specifically focusing on interfaces. It covers various aspects such as abstract methods, static methods, private methods, and interface inheritance, along with practical examples. Key topics include the implementation of interfaces, method overriding, and polymorphism in Java.

Uploaded by

manjusince2006
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

MODULE 3 – Object Oriented Programming with Java (BCS306A)

VTU 3rd Semester CSE – Full Answer Notes (6–7 Marks Each)

1. Interface Calculator with abstract add() and static displayInfo()


interface Calculator {
int add(int a, int b);
static void displayInfo() {
[Link]("Calculator Interface");
}
}

class CalcDemo implements Calculator {


public int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
[Link]();
CalcDemo c = new CalcDemo();
[Link]("Sum = " + [Link](10, 20));
}
}

Explanation: The interface contains an abstract method add() which is implemented in the class.
The static method displayInfo() is accessed using the interface name. This demonstrates interface
usage.

2. Interface with private method used inside default method

interface MyInterface {
default void show() {
helper();
[Link]("Default method executed");
}
private void helper() {
[Link]("Private helper method");
}
}

class Test implements MyInterface {


public static void main(String[] args) {
Test t = new Test();
[Link]();
}
}

Explanation: Private methods are used only inside the interface. They help avoid code duplication,
improve readability, and provide better encapsulation. Introduced in Java 9.

3. Static methods in interfaces cannot be overridden

interface Sample {
static void display() {
[Link]("Interface static method");
}
}

class Demo implements Sample {


static void display() {
[Link]("Class static method");
}
public static void main(String[] args) {
[Link]();
[Link]();
}
}

Explanation: Static methods belong to the interface itself and are accessed using the interface
name. They cannot be overridden, only hidden.

4. Interface variables are public, static and final


interface Values {
int X = 50;
}

class Test {
public static void main(String[] args) {
[Link](Values.X);
}
}

Explanation: Interface variables are implicitly public, static and final. Their value cannot be
changed.

5. Necessity of private methods in interfaces

Private methods improve code reusability by allowing common logic to be written once and reused
inside default or static methods. They improve readability, reduce duplication, hide implementation
details and increase maintainability of interfaces.

6. Payment interface with CreditCardPayment and UPIPayment

interface Payment {
void processPayment();
}

class CreditCardPayment implements Payment {


public void processPayment() {
[Link]("Credit Card payment processed");
}
}

class UPIPayment implements Payment {


public void processPayment() {
[Link]("UPI payment processed");
}
}

class PaymentDemo {
public static void main(String[] args) {
Payment p1 = new CreditCardPayment();
Payment p2 = new UPIPayment();
[Link]();
[Link]();
}
}

Explanation: Different classes implement the same interface method, demonstrating polymorphism.

7. Default method and overriding in implementing class

interface Hello {
default void greet() {
[Link]("Hello from Interface");
}
}

class Welcome implements Hello {


public void greet() {
[Link]("Hello from Class");
}
public static void main(String[] args) {
Welcome w = new Welcome();
[Link]();
}
}

Explanation: Default methods provide implementation and can be overridden in implementing


classes.

8. Interface extending multiple interfaces and method resolution

interface A {
default void show() {
[Link]("A");
}
}

interface B {
default void show() {
[Link]("B");
}
}

interface C extends A, B {
default void show() {
[Link]();
}
}

class Test implements C {


public static void main(String[] args) {
new Test().show();
}
}

Explanation: Java resolves ambiguity using [Link]().

9. Resizable interface implemented by Rectangle

interface Resizable {
void resizeWidth(int w);
void resizeHeight(int h);
}

class Rectangle implements Resizable {


int width, height;
public void resizeWidth(int w) {
width = w;
}
public void resizeHeight(int h) {
height = h;
}
public static void main(String[] args) {
Rectangle r = new Rectangle();
[Link](10);
[Link](20);
[Link]("Rectangle resized");
}
}

Explanation: Interface defines methods and class provides implementation.


10. Interface extending another interface and class implementing
it
interface Animal {
void eat();
}

interface Pet extends Animal {


void play();
}

class Dog implements Pet {


public void eat() {
[Link]("Dog eats food");
}
public void play() {
[Link]("Dog plays");
}
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
}
}

Explanation: Interface inheritance uses extends. Class implementing derived interface must
implement all methods.

You might also like