0% found this document useful (0 votes)
7 views

Java Imp

Uploaded by

Ayaan Nehal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java Imp

Uploaded by

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

UNIT-2

Q:Write a Java program to implement multiple inheritance using interfaces.


Ans:
interface Animal {
void sound();
}

interface Mammal {
void eat();
}

class Dog implements Animal , Mammal {


public void sound() {
System.out.println("The dog barks");
}

public void eat() {


System.out.println("The dog eats");
}
}

public class Main {


public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
dog.eat();
}
}
output:
The dog barks
The dog eats

Q:Explain how to create a package with a help of program.


ans:
package com.example.mypackage;

public class MyClass {


public void sayHello() {
System.out.println("Hello from MyClass!");
}
}

// Main.java
import com.example.mypackage.MyClass;

public class Main {


public static void main(String[] args) {
MyClass myObject = new MyClass();
myObject.sayHello();
}
}
Output: Hello from MyClass!

You might also like