0% found this document useful (0 votes)
39 views1 page

Q. A) How Can You Achieve Multiple Inheritance in Java?: Answer

Java allows multiple inheritance through interfaces. Classes can implement multiple interfaces to inherit their abstract methods. This avoids conflicts seen in multiple inheritance with classes in other languages. StringBuffer represents mutable character sequences while String is immutable - its value cannot change after creation like literals.

Uploaded by

Abdulhaq Sadiqi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views1 page

Q. A) How Can You Achieve Multiple Inheritance in Java?: Answer

Java allows multiple inheritance through interfaces. Classes can implement multiple interfaces to inherit their abstract methods. This avoids conflicts seen in multiple inheritance with classes in other languages. StringBuffer represents mutable character sequences while String is immutable - its value cannot change after creation like literals.

Uploaded by

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

Java Programming

Q. a) How can you achieve Multiple Inheritance in Java?


Answer:
Javas interface mechanism can be used to implement multiple inheritance, with one important difference from c+
+ way of doing MI: the inherited interfaces must be abstract. This obviates the need to choose between different
implementations, as with interfaces there are no implementations.

interface CanFight {
void fight();
interface CanSwim {
void swim();
interface CanFly {
void fly();
class ActionCharacter {
public void fight() {}
class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly {
public void swim() {}
public void fly() {}
}
You can even achieve a form of multiple inheritance where you can use the *functionality* of classes rather than
just the interface:

interface A {
void methodA();
}
class AImpl implements A {
void methodA() { //do stuff }
}
interface B {
void methodB();
}
class BImpl implements B {
void methodB() { //do stuff }
}
class Multiple implements A, B {
private A a = new A();
private B b = new B();
void method() { a.methodA(); }
void method() { b.methodB(); }
}

Q. b) What is the difference between String Buffer and String class?

Answer:

A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified.
At any point in time it contains some particular sequence of characters, but the length and content of the
sequence can be changed through certain method calls.

Whereas,

The String class represents character strings. All string literals in Java programs, such as "abc" are constant and
implemented as instances of this class; their values cannot be changed after they are created.

Abdul Haq
A1922715007 (el)

You might also like