• CONTEST ALERT - Experience the power of DDR5 memory with Kingston Click for details
Status
Not open for further replies.

Plasma_Snake

Indidiot
Consider a scenario;
class2 extends class1
{
--some code--
}

class3 extends class2
{
--some code--
}
Now since its a text book example of Hierarchical Inheritance, is it possible:

  1. for class3 to access a method of class1?
  2. If Super is used whose Constructor is called, class1's or class2's ?
  3. Is there a provision of "Friend" type access specifier that can enable the usage of Private members of Super class in a sub class?
 

QwertyManiac

Commander in Chief
1. Yes it can access it as normally as class 2 would access it.

2. super() would call the class2's constructor, but it'd also trigger the constructor-chain which will call class 1 and so on. You need to be careful with that.

3. Not exactly, but you can use something called as a "Package" to work around this issue.

Here's an example regarding the first two questions:

Code:
class Foo
{
    Foo()
    {
        System.out.println("Foo-Constructor Called.");
    }
    
    // Below one won't show up cause its not explicitly called.
    // But the Foo() is called nevertheless, after Bar(someInt)
    
    Foo(int someInt)
    {
        System.out.println("Foo-Constructor Called with " + someInt);
    }
    
    void doFoo()
    {
        System.out.println("Foo did something.");
    }
}

class Bar extends Foo
{
    Bar()
    {
        System.out.println("Bar-Constructor Called.");
    }
    
    Bar(int someInt)
    {
        /* super(someInt); */
        System.out.println("Bar-Constructor Called with " + someInt);
    }
    
    void doBar()
    {
        System.out.println("Bar did something.");
    }
}

class Spam extends Bar
{
    Spam()
    {
        super(16);
        System.out.println("Spam-Constructor Called.");
    }
    
    void doSpam()
    {
        System.out.println("Spam did something.");
    }
}

class Eggs
{
    public static void main(String args[])
    {
        Spam objSpam = new Spam();
        objSpam.doFoo();
        objSpam.doBar();
        objSpam.doSpam();
    }
}

// Eggs.java
 

QwertyManiac

Commander in Chief
I'm afraid not, but there's Microsoft's J# that comes close enough to Java and is available in the full-fledged versions of Visual Studio.

You can try Eclipse / JCreator / JBuilder instead as alternatives?
 

Garbage

God of Mistakes...
I'm afraid not, but there's Microsoft's J# that comes close enough to Java and is available in the full-fledged versions of Visual Studio.

You can try Eclipse / JCreator / JBuilder instead as alternatives?
Or u can use Notepad++ / Vim / Kete if u love to type n then manually compile programs... :D
 
OP
Plasma_Snake

Plasma_Snake

Indidiot
This is a simple program of implementing "Super" and "Inheritance". I made it using Netbeans 5.5.1 and when compiled it, gave some errors. I, then copied the code in Notepad and when Compiled and executed the program in Command prompt, it worked fine and produced the output as expected. What went wrong in case of Netbeans? I used the same JDK for compiling and executing the program, one used by Netbeans(present in Netbeans folder). below are both, the Netbeans project file and the Notepad one. Please check them and tell me what was wrong?
 
Status
Not open for further replies.
Top Bottom