programming challenge in java-1

Morningdot Hablu

Morningdot Hablu

@morningdot-6Xuj4M Oct 17, 2024
Post the correct code you have permission to take change only in public class.....

class a
{
a()
{
System.out.println("mohit kumar singh a");
}
}
class b extends a
{
b()
{
System.out.println("mohit kumar singh b");
}
}
class c extends b
{
int x;
c()
{
x=y;
System.out.println("mohit kumar singh c");
System.out.println("x= "+x);
}
}
public class f extends c
{
public static void main(String args[])
{
c t=new c();
}
}

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • sushant005

    sushant005

    @sushant005-tyt4WK Aug 22, 2010

    class a
    {
    int y;//here we have declare y.
    a()
    {
    System.out.println("mohit kumar singh a");
    }
    }
    class b extends a
    {
    b()
    {
    System.out.println("mohit kumar singh b");
    }
    }
    class c extends b
    {
    int x;//you do not the permission to eliminate this
    c()
    {
    x=y;//you do not the permission to eliminate this
    System.out.println("mohit kumar singh c");
    System.out.println("x= "+x);//you do not have permission to eliminate this.
    }
    }
    public class f
    {
    public static void main(String args[])
    {
    c t=new c();
    }
    }
    Mohit am i right.
    Just we have to initialize y inside class a.
  • sushant005

    sushant005

    @sushant005-tyt4WK Aug 22, 2010

    class a
    {
    int y;//here we have declare y.
    a()
    {
    System.out.println("mohit kumar singh a");
    }
    }
    class b extends a
    {
    b()
    {
    System.out.println("mohit kumar singh b");
    }
    }
    class c extends b
    {
    int x;//you do not the permission to eliminate this
    c()
    {
    x=y;//you do not the permission to eliminate this
    System.out.println("mohit kumar singh c");
    System.out.println("x= "+x);//you do not have permission to eliminate this.
    }
    }
    public class f
    {
    public static void main(String args[])
    {
    c t=new c();
    }
    }
    Mohit am i right.
    Just we have to initialize y inside class a.
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Aug 23, 2010

    Oh sorry sus i have done a mistake in my question.
    Check out once again.
  • Kaustubh Katdare

    Kaustubh Katdare

    @thebigk Aug 23, 2010

    Please always wrap your code inside
    ... [ /code]  tags.
  • anandkumarjha

    anandkumarjha

    @anandkumarjha-jzdDMA Aug 23, 2010

    Hello Mohit
    as far as i know that you only created the object for the class c but didn't use it.so it might be the error.and what sushant told is also right that we should also declare y veriable inside the class A
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Aug 24, 2010

    since c has a constructor: c(int y) you must satisfy that constructor when you extend it. In other words r extends c but c has a constructor that requires an integer therefore r must satisfy this. You could add c() {} in class c to allow for an empty constructor or a better solution is to use super to set the constructor to a default value such as zero when constructing r: r() { super(0); } or if you wanted r(int y) { super(y); } See the Example:
    class a
    {
    a()
    {
    System.out.println("mohit kumar singh a");
    }
    }
    class b extends a
    {
    b()
    {
    System.out.println("mohit kumar singh b");
    }
    }
    class c extends b
    {
    int x;
    c(int y)
    {
    x=y;
    System.out.println("mohit kumar singh c");
    System.out.println("x= "+x);
    }
    }
    class r extends c
    {
    r() { super(0); }
    public static void main(String args[])
    {
    c t=new c(4);
    }
    }
  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Aug 24, 2010

    hey mohit you had mentioned that we can make changes in only public class ie f
    but here you are making changes in class c also
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Aug 24, 2010

    @goyal ya you are right.

    Oh!! sorry guy's actually i missed int y in constructor.
    Well i try to avoid this type of mistakes in my next java programming challenge.
    So wait for my next challenge.