Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 18th, 2006, 9:24 PM   #21
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,208
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Feb 18th, 2006, 11:27 PM   #22
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Quote:
Originally Posted by grumpy
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.
It is possible to "provide basic functionality" in a Java abstract base class, as the code below demonstrates. As long as methods within an abstract class are not specifically declared to be abstract, they are inherited (a()) and can be overloaded (b()).

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 "); }
	}
}
This code will print "A b c".
titaniumdecoy is offline   Reply With Quote
Old Feb 19th, 2006, 12:03 AM   #23
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,208
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Feb 19th, 2006, 12:43 AM   #24
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Feb 19th, 2006, 1:33 AM   #25
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,208
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:41 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC