473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C Implementation Of Java Interface Class

I try to write a c implementation of java abstract class which serve
the same purpose. however, I feel that there might be some potential
holes in the future especially when I plan to scale up this software
piece.

Please consider the following Java code.

=== Start Java ===

interface animal {
public void speak();
}

class dog implements animal {
long dummy; /* please not that cat is having short type */

public void speak() {
System.out.println("I am a happy dog. Wow! Wow!");
}
}

class cat implements animal {
short dummy; /* please not that dog is having long type */

public void speak() {
System.out.println("I am a sad cat. Meow! Meow!");
}
}

public class driver {
public static void main(String[] args) {
animal animal1 = new dog();
animal animal2 = new cat();

animal1.speak();
animal2.speak();
}
}

=== End Java ===

=== Start C ===

#include <stdio.h>

typedef struct tagAnimal{
void (*speak)(void) __attribute__((packed));
void (*delete)(struct tagAnimal *) __attribute__((packed));
} animal;

typedef struct {
void (*speak)(void) __attribute__((packed));
void (*delete)(animal *) __attribute__((packed));
/* Cannt just place dummy before speak function pointer.
* This will cause problem while trying to cast dog
* from animal
*/
long dummy __attribute__((packed));
} dog;

typedef struct {
void (*speak)(void) __attribute__((packed));
void (*delete)(animal *) __attribute__((packed));
/* Cannt just place dummy before speak function pointer.
* This will cause problem while trying to cast cat
* from animal
*/
short dummy __attribute__((packed));
} cat;

animal* createDog(void);
animal* createCat(void);
void deleteDog(animal *myAnimal);
void deleteCat(animal *myAnimal);
void dogSpeak(void);
void catSpeak(void);

animal *createDog() {
dog *myDog = (dog *)calloc(1, sizeof(dog));
myDog->speak = dogSpeak;
myDog->delete = deleteDog;

return((animal *)myDog);
}

void deleteDog(animal *myAnimal) {
free((void*)myAnimal);
}

animal *createCat() {
cat *myCat = (cat *)calloc(1, sizeof(cat));
myCat->speak = catSpeak;
myCat->delete = deleteCat;

return((animal *)myCat);
}

void deleteCat(animal *myAnimal) {
free((void*)myAnimal);
}

void dogSpeak() {
printf("I am a happy dog. Wow! Wow!\n");
}

void catSpeak() {
printf("I am a sad cat. Meow! Meow!\n");
}

int main() {
animal* animal1 = createDog();
animal* animal2 = createCat();

animal1->speak();
animal2->speak();

animal1->delete(animal1);
animal2->delete(animal2);
}

=== End C ===

It work. But here are some potential problem in the future.

1. The size, sequence of the data members inside the struct, have to be
same across the abstract data type (animal) and the concrete data type
(cat, dog).

Say, the following will cause problem

typedef struct tagAnimal{
void (*speak)(void) __attribute__((packed));
void (*delete)(struct tagAnimal *) __attribute__((packed));
} animal;

typedef struct {
long dummy;
void (*speak)(void) __attribute__((packed));
void (*delete)(animal *) __attribute__((packed));
} dog;

Please note that the dummy member has been moved from the bottom to
top. This will screw up everything while we try to cast from dog to
animal, and access function through animal.

Hence, everytime the programmers try to add a new member elements to
the concrete data type (dog, cat), they have to be **VERY VERY**
caution on the size and the sequence of the members with respect to the
abstract data type.

Proper documentation on the struct may help to reduce this from happen.
But are not gurantee. If there any better way we can prevent this from
happen?

Or should I re-design the whole architecture? If yes, how can I
re-implement the above Java class?

Thank you very much!

-cheok

Nov 14 '05 #1
0 1067

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

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
6
by: Paul Fame | last post by:
Hello World, This is not a flame, but a question about the fundamentals of the language. Unlike some languages, C++ requires class member functions to be declared twice: once in the class...
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
9
by: Steven T. Hatton | last post by:
This was written for the gnu.g++.help list. It rather clearly spells out the most important feature of Java that I believe C++ lacks. I really don't believe the C++ Standard sepcifies enough for a...
4
by: gabriel | last post by:
Hi Firstly i'd like to thank you for reading this and offer my appreciation for replies in advance. I've recently been writing a program which implements a user-defined API (Robocode to be...
20
by: Luc Kumps | last post by:
(Sorry about the previous post, it got transmitted before it was complete) We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this,...
7
by: p_shakil | last post by:
Hi, I would like to know the interface concept in Python.How the Interface is defined and implemented in Python?. How to access the interface fromn Client? Thanks PSB
0
by: JosAH | last post by:
Greetings, Introduction At this moment we have a TextProcessor, a LibraryBuilder as well as the Library itself. As you read last week a Library is capable of producing pieces of text in a...
3
by: Armin Zingler | last post by:
Hi, sorry for this C++ noob question... I'm coming from VB.Net actually and I am trying to do the same in C++ (/CLR) as I do here (pseudo code partial): class C implements I1 implements I2...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.