Results 1 to 3 of 3
- 11-13-2009, 03:58 AM #1
Inhertance in Java, and how does it work compair to C++
I am wondering how inhertance works in Java vs how it works in C++, and I would like to know how it would work in given the following type of stuff from C++, if it works in Java, and if so how it would work, what concepts would carry over from C++ to java, and what will not carry over to Java, and what new concepts exist in Java that might not exist in C++
ClassJava Code:Class A { public: int myNum; virtural void funA() = 0; virtural void funB() { cout << "A::funB();\n" }; void funC() { cout << "A::funC();\n " }; }; Class B : public A { public: virtual void funA() { cout << "B::funA();\n " }; virtual void funB() { cont << "B::funB();\n " }; void funC() { cout << "B::funC();\n " }; }; Class C : public A { public: virtual void funB() { cout << "C::funB();\n "}; void FunC { cout << "C::FunC();\n "}; }; Class D : public C { public: Virtual void FunA { cout << "D::FunA();\n " }; }; Class E : public A { public: void funE() { myNum = 1; cout << “ E::funE(); \n and my Num is “ << myNum << endl;}; }; Class F : public A { public: void funE() { myNum = 2 ;cout << “ F::funF(); \n and my Num is “ << myNum << endl;}; }; Class G : public E, public F { public: void funE() { myNum = 3; cout << “G::funG(); \n and my Num is “ << myNum << endl;}; }; Class H: virtual public A { public: virtual void funH() { myNum = 4; cout << h::funh(); \n and my Num is “ << myNum << endl;}; }; Class I: virtual public A { public: virtual void funH() { myNum = 5; cout << h::funh(); \n and my Num is “ << myNum << endl;}; }; Class J: virtual public H, virtual public I { public: virtual void funH() { myNum = 6; cout << J::funh(); \n and my Num is “ << myNum << endl;}; };
C can not be instanciated as it does not over load the pure virtual function of
funA()
For the following code call the following will be produced
DoesJava Code:ClassA ptrA = new ClassA();
not compile as Class A has a pure virtual function and can not be institated
Will give the following output:Java Code:ClassB ptrB = new ClassB(); ClassA ptrA = ptrB; //ptrA and ptrB now point to the same object ptrB->funA(); ptrB->funB(); ptrB->funC(); ptrA->funA(); ptrA->funB(); ptrA->funC();
B::funA();
B::funB();
B::funC();
B::funA();
B::funB();
A::funC();
Will give me the following out putJava Code:ClassD ptrD = new ClassD(); ClassA ptrA = ptrD; //ptrA and ptrD now point to the same object ptrD->funA(); ptrD->funB(); ptrD->funC(); ptrA->funA(); ptrA->funB(); ptrA->funC();
D::funA();
C::funB();
C::funC();
D::funA();
C::funB();
A::funC();
ClassG is ambiguous as if I need MyNum which myNum would be used, the one inhertaded from ClassE or the one from ClassF, this will cause a compalation errorJava Code:ClassG ptrG = new ClassG();
Will give me the following.Java Code:ClassJ ptrJ = new ClassJ(); ptrJ->funH();
J:funH()
and my Num is 6
So my questions are
1) how would this all be done in Java?
2) what would the expect results be in Java
3) please explain why what happens happens for each type of inhertance.
4) How would I do the same things in JavaMichael P. O'Connor
http://www.mikeoconnor.net
- 11-13-2009, 07:13 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
In Java, a class that cannot be instantiated is declared as abstract. Methods not implemented in the class are also declared as abstract and a class containing such a method must mark itself as abstract.
There is no multiple inheritance in Java so the diamond problem you described later doesn't happen at all.
P.S It may be best to download Sun's Java tutorial and go through it.
- 11-13-2009, 11:38 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Paid java work opportunity
By GGC in forum Jobs OfferedReplies: 1Last Post: 08-11-2009, 02:55 PM -
Trying to get JAVA to work with SQLite
By mark8569 in forum JDBCReplies: 3Last Post: 04-25-2009, 01:42 AM -
Just how do I get Java to actually work?
By MickY G in forum New To JavaReplies: 5Last Post: 11-19-2008, 03:50 AM -
What are applet and how its work in java?
By pawankumarom in forum New To JavaReplies: 7Last Post: 09-05-2008, 05:26 PM -
Ho to work with enumerations in java
By zizou147 in forum Advanced JavaReplies: 0Last Post: 03-22-2008, 12:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks