same old question: interface Vs. Abstract class



Hi all,


I m a C++ programmer basically & recently got into JAVA tech.

I have 2 doubts:

-----------------
FIRST :
-----------------
Interface keyword adv: Multiple inheritance like C++. but avoid all
ambguity dirt of MI.

ABC adv: base code can be given to child class.

Suppose I m damn sure that my kid concrete class objects will have
different behaviour for same request issued and all methods will be
clubed within one dady ABC. Then I think I should use ABC only over
interface for speed optimization.

Here is some sample code that I thought will help u guys understand me
better.

X can be considered as the Factory class.


//BEGIN.
[root@MaujDev documents]# cat C.java
abstract class Character{
abstract public void method();
}

class B extends Character{
public void method() {System.out.println("This is B class");};
}

class X{
public static Character newCharacter(){
return new B();
}
}
class C
{
public static void main(String args[]){
Character x = X.newCharacter();
x.method();
}
};
[root@MaujDev documents]# cat J.java
interface Character{
public void method();
}

class B implements Character{
public void method() {System.out.println("This is B class");};
}

class X{
public static Character newCharacter(){
return new B();
}
}

class D
{
public static void main(String args[]){
Character x = X.newCharacter();
x.method();
}
};
[root@MaujDev documents]#
//END.


-----------
SECOND
-----------

I read this line some where on internet.

/*************
A bird can fly(). So can a aircraft, but aircraft inheriting from bird
doesnt make sense. I fully agree.
**************/

But What if it can be phrased as:

Bird inherit from FlyingThing() and LivingThing().
Aircraft inherit from FlyingThing() and NonLivingThing().
but both FlyingThing() and LivingThing() will inherit from Thing()
which will provide all the base code to register as a valid object in
this world.

In C++ its MI provided programmers dont do stupidty.

How will one phrase the same in JAVA. Its obvious I define interfaces
in C++. I write ABC with all the stuff virtual.

.



Relevant Pages

  • Re: Interface question
    ... This is the beauty of interfaces: you don't inherit from something ... interface, and make all of the classes implement it. ... ListViewModel, which is the brains behind managing item selection, ... to make another control that could interact with any control that was ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Interface question
    ... The interface it uses is IAnimal. ... in C# a class can only directly inherit one other class. ... Not all animals do make sounds, and so by doing it this way, all animals would have a common shared base class "Animal" that really is universal to all animals, while those that can intentionally make sounds would _implement_ the "IVocalize" interface themselves in whatever way was appropriate to that particular animal. ... it essentially enters into a contract with any code that might use an instance of a Student that it will implement the methods in those interfaces: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Space characters is CEdit.
    ... interface, the interface is wrong. ... empty edit control, then the correct approach is to NOT use an edit control for this ... I can't think of what to do except use a different character or maybe. ...
    (microsoft.public.vc.mfc)
  • Re: Pulling my hair out, I need some help
    ... The reason I said not Inherited was that the OP said "an Interface ... that all DLL need to inherit to use". ... is that the plug-in must meet. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Why Java interface?
    ... In C++, there are multiple inheritances; but in Java, a class can ... inherit only one super class. ... interface is ...
    (comp.lang.java.programmer)