DIGIT-ARC read-only archive · snapshot: July 23, 2026

JAVA Query

7 posts · originally from https://geek.digit.in/community/threads/java-query.94100/

Plasma_Snake
Plasma_Snake Original post #1
Jul 26, 2008 at 10:04 PM
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
Jul 26, 2008 at 10:30 PM
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
Plasma_Snake
Jul 27, 2008 at 3:28 AM
Besides Netbeans, can we use Visual Studio to Create,Compile and Run simple JAVA programs?
QwertyManiac
Jul 27, 2008 at 6:59 AM
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
Garbage #5
Jul 27, 2008 at 7:08 AM
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
nitish_mythology
Jul 27, 2008 at 8:32 AM
Another leight weight IDE -- Geany . Found it quite impressive.
Plasma_Snake
Aug 1, 2008 at 3:28 PM
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?