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)