![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,208
Rep Power: 5
![]() |
And, one other issue is that Java disallows inheritence from multiple classes (whether abstract or not). That's why Java treats interfaces differently from abstract classes .... they sought to eliminate multiple inheritence of classes from the language (because full blown implementation of MI of classes significantly increases difficulty implementing a compiler and can introduce serious gotchas for simple-minded programmers who misuse it). But then they wanted to allow implementation of multiple interfaces. Hence, they had to treat interfaces differently from abstract classes.
C++, by supporting multiple inheritence, does not need to make the distinction between abstract classes and interfaces (unlike Java, an interface can be implemented as a particular type of abstract class). One effect of this is that, in C++, it is possible for an interface class to provide implementation of its methods. While it is not possible to instantiate an interface class (as for any abstract class), it is possible to provide basic functionality along with the interface that can be used in derived classes. In practice there are better ways to do that in C++ (a discussion for another day), but the fact it is possible in C++ and not in Java is one discriminator between those two languages. |
|
|
|
|
|
#22 | |
|
Expert Programmer
|
Quote:
class Test {
public static void main(String[] args) {
Derived d = new Test().new Derived();
d.a();
d.b();
d.c();
}
abstract class Base {
void a() { System.out.print("A "); }
void b() { System.out.print("B "); }
abstract void c();
}
class Derived extends Base {
void b() { System.out.print("b "); }
void c() { System.out.print("c "); }
}
} |
|
|
|
|
|
|
#23 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,208
Rep Power: 5
![]() |
True but, AFAIK, it is not possible for class Base to provide an implemention of abstract method c(). In C++, it is possible. It is also not possible in Java to extend more than one abstract class.
|
|
|
|
|
|
#24 |
|
Expert Programmer
|
If you wanted the class Base to provide an implementation of c() you would not declare it as abstract. This may be different in C++, which I believe is what you mean to say.
|
|
|
|
|
|
#25 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,208
Rep Power: 5
![]() |
Yep. C++ allows one to provide an implementation for all member functions of a class, even if they are "abstract" or (in C++ speak) "pure virtual". Providing an implementation of a pure virtual function is optional (as long as you don't explicitly call it, as in an example I gave earlier), but it is certainly possible.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|