Our teacher gave us an assignment.Can anyone help or give me some idea on how to solve this java problem? im still learning java. Here's the problem:

"Write some code to demonstrate to yourself the chain of construction. Create an inheritance hierarchy of 4 classes. Give them any names you like. They don't have to have any data or methods, but each one should have a no-args constructor. These constructors should print out a line identifying the current class (something like "This line belongs to Construction 1 "). Your main() method should construct a single instance of your lowest-level subclass. What is the output and explain your work during our laboratory time? Does it matter which class contains the main() method?"

you need to type extends [name here] and then you can retrieve information between the 2 and if you need any more help post what you have so far.

public class Class1{

}
public class Class2 extends Class1{ //class 2 is a subclass of class1


}

That is the basic syntax for inheritance in java.

For this assignment your constructors aren't actually constructing a class, they're demonstrating the order in which constructors are called, and how the code executes.
So there isn't really a problem to solve: you're just going to write four dummy classes, each of which extends the previous one and has a no-argument constructor which simply prints out a line identifying itself, then you're going to write a main method which makes a new instance of the last one.

Your real task is to understand what happens when you do this, and why it happens this way, and nobody here can do that for you.

For this assignment your constructors aren't actually constructing a class, they're demonstrating the order in which constructors are called, and how the code executes.
So there isn't really a problem to solve: you're just going to write four dummy classes, each of which extends the previous one and has a no-argument constructor which simply prints out a line identifying itself, then you're going to write a main method which makes a new instance of the last one.

Your real task is to understand what happens when you do this, and why it happens this way, and nobody here can do that for you.

what do you mean by "each of which extends the previous one and has a no-argument constructor which simply prints out a line identifying itself, then you're going to write a main method which makes a new instance of the last one."

im sorry for being noob. here's the code i tried:

public class dummy1 {

}
public class dummy2 extends dummy1{
}
public class dummy3 extends dummy1 {
}
public class dummy4 extends dummy1{
}
import static java.lang.System.out;

public class TestDummies {

	public static void main(String[]args){
		dummy1 dum1=new dummy1();
		dummy2 dum2=new dummy2();
		dummy3 dum3=new dummy3();
		dummy4 dum4=new dummy4();


		out.println("This line belongs to dummy1" +dum1);
		out.println("This line belongs to dummy2" +dum2);
		out.println("This line belongs to dummy3" +dum3);
		out.println("This line belongs to dummy4" +dum4);

	}
}

its not the output that i expected. please help! :(

//first java file
public class Dummy1{}

//2nd java file
public class Dummy2{}

//3rd
public class Dummy3{}

//MAIN CLASS
public class Main{
    public static void main(String[] args){
        Dummy1 dum1 = new Dummy1();
        Dummy2 dum2 = new Dummy2();
        Dummy3 dum3 = new Dummy3();
        
        String dum1Name = dum1.getClass().getName();
        String dum2Name = dum2.getClass().getName();
        String dum3Name = dum3.getClass().getName();

        System.out.println("This belongs to "+dum1Name);
        System.out.println("This belongs to "+dum2Name);
        System.out.println("This belongs to "+dum3Name);
        
    }
}

Is this maybe what you're looking for?

//first java file
public class Dummy1{}

//2nd java file
public class Dummy2{}

//3rd
public class Dummy3{}

//MAIN CLASS
public class Main{
    public static void main(String[] args){
        Dummy1 dum1 = new Dummy1();
        Dummy2 dum2 = new Dummy2();
        Dummy3 dum3 = new Dummy3();
        
        String dum1Name = dum1.getClass().getName();
        String dum2Name = dum2.getClass().getName();
        String dum3Name = dum3.getClass().getName();

        System.out.println("This belongs to "+dum1Name);
        System.out.println("This belongs to "+dum2Name);
        System.out.println("This belongs to "+dum3Name);
        
    }
}

Is this maybe what you're looking for?

Did you read his original post?

@op your specifications say: "Write some code to demonstrate to yourself the chain of construction. Create an inheritance hierarchy of 4 classes. "

In your last post, all of your classes inherit from 1 class, but you are asked to create a hierarchy of 4 classes.
Class1
Class2 inherits from Class1
Class3 inherits from Class2
Class4 inherits from Class3

//first java file
public class Dummy1{}

//2nd java file
public class Dummy2{}

//3rd
public class Dummy3{}

//MAIN CLASS
public class Main{
    public static void main(String[] args){
        Dummy1 dum1 = new Dummy1();
        Dummy2 dum2 = new Dummy2();
        Dummy3 dum3 = new Dummy3();
        
        String dum1Name = dum1.getClass().getName();
        String dum2Name = dum2.getClass().getName();
        String dum3Name = dum3.getClass().getName();

        System.out.println("This belongs to "+dum1Name);
        System.out.println("This belongs to "+dum2Name);
        System.out.println("This belongs to "+dum3Name);
        
    }
}

Is this maybe what you're looking for?

thanks! i just put "extends" on the class.actually what im looking for is the corect sysntax. i have the idea but i dont know the correct syntax. now the program's displaying the correct output.

the output can't be displayed

thanks! i just put "extends" on the class.actually what im looking for is the corect sysntax. i have the idea but i dont know the correct syntax. now the program's displaying the correct output.

You're welcome :) (First time actually helping someone and succeeding in DaniWeb! Woop!

the output can't be displayed

Make your own thread.
Include what IDE you are using and detail about the problem.

@MPA - that code does not address the problem assigned, although it does produce a simulation of what was asked for.

Take the code that you wrote above, and modify it so that Dummy1 has a constructor. In that constructor, just put a println() statement - "Hello world" will do. Then run the code and see what happens - I think that'll show you what else you need to do.

@MPA - that code does not address the problem assigned, although it does produce a simulation of what was asked for.

Take the code that you wrote above, and modify it so that Dummy1 has a constructor. In that constructor, just put a println() statement - "Hello world" will do. Then run the code and see what happens - I think that'll show you what else you need to do.

Main Class

public class Main{
  public static void main(String[] args){
    Dummy1 dum1 = new Dummy1();
    Dummy2 dum2 = new Dummy2();
    Dummy3 dum3 = new Dummy3();
  }
}

Dummy1 Class

public class Dummy1{
  public static void main(String[] args){
    new Dummy1();
  }
  public Dummy1(){
    System.out.println("This constructor belongs to "+getClass().getName());
  }
}

Dummy2 Class

public class Dummy1{
  public static void main(String[] args){
    new Dummy2();
  }
  public Dummy2(){
    System.out.println("This constructor belongs to "+getClass().getName());
  }
}

Dummy3 Class

public class Dummy3{
  public static void main(String[] args){
    new Dummy3();
  }
  public Dummy3(){
    System.out.println("This constructor belongs to "+getClass().getName());
  }
}

I so hope this works...

No, not quite. You've got the constructors, but you're not using inheritance here.

Let's let the original poster have a go at it, though, instead of just throwing bits of code at him.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.