Results 1 to 3 of 3
  1. #1
    Drun's Avatar
    Drun is offline Member
    Join Date
    Nov 2009
    Location
    Indian Head, MD (Missing Pittsburgh PA)
    Posts
    44
    Rep Power
    0

    Default 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++
    Java 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;};
    };
    Class
    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

    Java Code:
    ClassA
    ptrA = new ClassA();
    Does
    not compile as Class A has a pure virtual function and can not be institated

    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();
    Will give the following output:
    B::funA();
    B::funB();
    B::funC();
    B::funA();
    B::funB();
    A::funC();

    Java 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();
    Will give me the following out put
    D::funA();
    C::funB();
    C::funC();
    D::funA();
    C::funB();
    A::funC();

    Java Code:
    ClassG ptrG = new ClassG();
    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 error
    Java Code:
    ClassJ ptrJ = new ClassJ();
    ptrJ->funH();
    Will give me the following.
    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 Java
    Michael P. O'Connor
    http://www.mikeoconnor.net

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    In addition all non-static methods of a class are the equivalent of virtual, so in your example with ClassA and ClassB the call ptrA->funC(); would execute ClassB::funC(), and not ClassA::funC().

Similar Threads

  1. Paid java work opportunity
    By GGC in forum Jobs Offered
    Replies: 1
    Last Post: 08-11-2009, 02:55 PM
  2. Trying to get JAVA to work with SQLite
    By mark8569 in forum JDBC
    Replies: 3
    Last Post: 04-25-2009, 01:42 AM
  3. Just how do I get Java to actually work?
    By MickY G in forum New To Java
    Replies: 5
    Last Post: 11-19-2008, 03:50 AM
  4. What are applet and how its work in java?
    By pawankumarom in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 05:26 PM
  5. Ho to work with enumerations in java
    By zizou147 in forum Advanced Java
    Replies: 0
    Last Post: 03-22-2008, 12:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •