473,651 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheriting only interface

Recently, I have been learning Java (it is one of laboratories on my
department), well I know Java is awfully slow, but it is not the point
that I'm interested.
Java has a keyword "interface. (and if you know what it is skip this
paragraph), that is an abstract class, whose every function is public
and purely virtual, and every field is public and static. Java has its
own reasons for creating that keyword - you can only inherit from one
non-interface class, but from infinite interfaces.
The main question is whether using similar classes in C++ is considered
a good practice. I know that every abstract function has a small
overhead on every time you use it. But are there other cons?
I had an usage for it (in Java program). I had to manage a small
database, small enough to store it in a single file, and load it whole
into memory. I created an interface Product, that and implemented it in
SmipleProduct which is a straightforward , or even blunt,
implementation. And every function in SimpleProduct that would use an
instance of products gets a java-pointer to Product. Also I had a
couple of classes that used the Product interface. And I had created an
interface class for Product data base (that also gets only
java-pointers of Product)

Looks like that:
interface Product{
//(...)
}

class SimpleProduct{
SimpleProduct(P roduct foo){ /*(...)*/};
//(...)
}

interface ProductDataBase {
void add (Product);
Product findById(int id);
//(...)
}

And if I the database had grown a lot I would had just to re-implement
classes representing DataBase using for example mySQL. And whole GUI,
and classes used to compare
products would be left untouched.

The second question is whether it is possible to get similar result -
easiness of re-implementation of parts of program - using other,
better, techniques.

Apr 6 '06 #1
1 1833
jb****@gmail.co m wrote:
Recently, I have been learning Java (it is one of laboratories on my
department), well I know Java is awfully slow, but it is not the point
that I'm interested.
Java has a keyword "interface. (and if you know what it is skip this
paragraph), that is an abstract class, whose every function is public
and purely virtual, and every field is public and static. Java has its
own reasons for creating that keyword - you can only inherit from one
non-interface class, but from infinite interfaces.
The main question is whether using similar classes in C++ is considered
a good practice. I know that every abstract function has a small
overhead on every time you use it.
Well, usually you get an overhead if you call it through a pointer or
reference, but that would be the same for every virtual function, not just
pure virtual ones.
But are there other cons?
Well, a base class in C++ is more flexible. If you want, you can still write
some non-pure functions or add some member variables, but still derive from
as many of those classes as you want. Of course, with freedom comes
responsibility.
I had an usage for it (in Java program). I had to manage a small
database, small enough to store it in a single file, and load it whole
into memory. I created an interface Product, that and implemented it in
SmipleProduct which is a straightforward , or even blunt,
implementation.
Well, in C++, you could also put the implementation directly into Product.
Derived classes can still use that implementation for the parts where it
makes sense. Reducing code duplication is a good thing.
And every function in SimpleProduct that would use an
instance of products gets a java-pointer to Product. Also I had a
couple of classes that used the Product interface. And I had created an
interface class for Product data base (that also gets only
java-pointers of Product)

Looks like that:
interface Product{
//(...)
}

class SimpleProduct{
SimpleProduct(P roduct foo){ /*(...)*/};
//(...)
}

interface ProductDataBase {
void add (Product);
Product findById(int id);
//(...)
}

And if I the database had grown a lot I would had just to re-implement
classes representing DataBase using for example mySQL. And whole GUI,
and classes used to compare products would be left untouched.


Hmm, but wouldn't it be enough to just replace your DataBase class then? I
mean, what is the advantage of that interface now? It seems redundand. I
would use that interface approach if you have several implementations in
your program of which you choose one at run-time.

Apr 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
5875
by: shaun roe | last post by:
I want something which is very like a bitset<64> but with a couple of extra functions: set/get the two 32 bit words, and conversion to unsigned long long. I can do this easily by inheriting from bitset<64>, but I know that STL has no virtual destructor. Can I get around this by calling the baseclass destructor explicitly in my derived class? Is there another way to get all of the bitset<64> functionality without rewriting a lot of...
11
2160
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain methods, such as public void Add(MyClass c). How can I enforce the same behavior (of requiring to implement a member with a new return type in an inherited class) in the master class (similar to the CollectionBase)? I have a class called...
2
1826
by: Simon | last post by:
Hi, Just to get started.... I've created a very simple web form, "Simple", using VS and I dropped a HTML img control on the design view. I referenced a picture and it is displayed in browser view. Very simple. I have created a second form and in the code-behind page I have altered the class definition to inherit from, "Simple".
1
336
by: Matthew Roberts | last post by:
Howdy Everyone, I am having trouble understanding the process of creating a type-safe collection by inheriting from the CollectionBase class. I have done it plenty of times, but now that I sit down and look at it, I'm wondering why it behaves the way it does, and also how to improve its functionality. First, understand the basic format of a type-safe collection:
0
1219
by: damianmatiasmax | last post by:
Helow, Good days. I am creating a class Base in VB.NET, then I use this class base to create to the class son across inherits, and Then generate the object COM this class son to be used in VB6. The problem is that I do not receive the events of the class base from VB6. I show the code ---------------------------------------------------------------------------------------------------------------- Class Base - Name: A
24
2941
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public vector<Corres>{ public: void addCorres(Corres& c); //it do little more than push_back function. }
17
2430
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer and then dereferencing that pointer. Should the dereferenced pointer have the same constness of the pointer as the pointer has the same constness as the class object? Yes, I am aware that C++ does not do this. I just want to know everyones...
5
1551
by: jc | last post by:
RE: Two Classes with the same Data Structure.. saving code? Inheriting a structure? I have two classes. One in Inherits System.Collections.CollectionBase, the other does not, but they both have the identical structure and properties. the only difference between them is there methods and that one is a collection class and the other is not. currently the code starts like this:
4
1690
by: AalaarDB | last post by:
struct base { int x, y, z; base() {x = 0; y = 0; z = 0;}; base(int x1, int y1, int z1) {x = x1; y = y1; z = z1;}; }; struct intermediate1 : public virtual base {}; struct intermediate2 : public virtual base
0
8349
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8795
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8460
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.