can anybody explain the output of the following Java code? [duplicate] - java

This question already has an answer here:
Why does it store or allocate memory for super class variables, in sub class object?
(1 answer)
Closed 7 years ago.
class A{
int i = 1;
A(){play();}
void play(){System.out.print(i);}
}
class B extends A{
int i = 2;
B(){play();}
void play(){System.out.print(i);}
}
public class Test extends B{
public static void main(String args[]){
new Test();
}
}
output:
02
can anybody explain the output of the above Java code?Thx.

Because Test class doesn't constructor and it extends from B class. Therefore, it will call B constructor and output is 2.

Related

A question on Java constructor and class member initialization [duplicate]

This question already has answers here:
Are fields initialized before constructor code is run in Java?
(5 answers)
Closed 2 years ago.
I'm trying to understand why the output of the following Java code is as like this:
class A {
public A() {
System.out.print("A()");
}
public static void main(String[] args) {
}
}
class B {
public B() {
System.out.print("B()");
}
public static void main(String[] args) {}
}
class C extends A {
B b = new B();
}
public class E05SimpleInheritance {
public static void main(String args[]) {
new C();
}
}
Output:
"A()B()"
I would imagine that when the main method of the E05SimpleInheritance public class is called the following things should happen
Non-public class C is loaded and its fields are initialized(before calling the default constructor of class C)
Since its member 'b' is an object of class B, class B is loaded in memory
Since we construct an object of Class B its constructor is called which should print B()
Default constructor of C is called which automatically calls the constructor of the superclass A which print A()
So the final output should be B()A() which is obviously wrong so I do not really understand how the code flows in this case. Can you perhaps show me why is A()B() printed instead of B()A()
Your mistake is in step 1:
Non-public class C is loaded and its fields are initialized(before calling the default constructor of class C)
This is not what’s happening. In reality, non-static fields are initialised at the beginning of the constructor, not before it. And the base class constructor is implicitly (or explicitly) invoked even before that.
In other words, javac generates code for C which is equivalent to the following:
class C extends A {
B b;
C() {
super();
b = new B();
}
}

why this class not compiling? [duplicate]

This question already has answers here:
Why can't I do assignment outside a method?
(7 answers)
compilation error: identifier expected
(4 answers)
Closed 5 years ago.
Why is this error coming? Please see the following code.
class Test{
Hello h=new Hello();
}
class Hello{
int a=10;
System.out.println(a); // error identifier expected
}
Create Class With Same Package
public class Hello {
public void print(){
int a = 10;
System.out.println("Number is :" +a);
}
}
Crate Class to Set the Main Method Within the Same Package of Hello Method
public class Main {
public static void main(String args[]){
Hello h1 = new Hello();
h1.print();
}
}

Does static variable inherited? [duplicate]

This question already has answers here:
Are static variables inherited
(4 answers)
Closed 4 years ago.
We know that in java static variable are not inherited. But in below code I am not getting any error as I want to initialize the static variable in child class.
class s
{
static int x;
}
class aaa extends s
{
void fun()
{
x=2;
System.out.println(x);
}
public static void main(String args[])
{
aaa w=new aaa();
w.fun();
}
}
static members are most definitely accessible from subclasses, as your example shows. You cannot override them, of course, but you could hide them.

Whats wrong with the given Java Code [duplicate]

This question already has answers here:
Cannot make a static reference to the non-static method
(8 answers)
Closed 7 years ago.
Code is
public class ctorsandobjs {
private int a;
public int b;
public ctorsandobjs(String arg)
{
System.out.println("I got " + arg);
}
public void add(int a,int b)
{
System.out.println("Addition is " + String.valueOf(a+b));
}
public static void main(String args[])
{
ctorsandobjs c = new ctorsandobjs("You");
c.a = 12;
c.b = 15;
add(c.a,c.b); //compiler shows error here
}
}
I am using Eclipse Luna IDE and JDK 8 ...
can you tell me why compiler is showing error here.....
"Cannot make a static reference to a non static method add(int,int) from the type ctorsandobjs"
I am new to JAVA...
and if possible suggest a solution
add is a non-static method and so you have to invoke it from the object of a class
You have to do:
c.add(c.a, c.b);
You cannot reference non-static members (private int a; public int b) from within a static function.
The add method is not a static method, so you need to call it on an instance of the class ctorsandobjs, for example like this:
c.add(c.a,c.b);

Java and skipping a level with super [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why is super.super.method(); not allowed in Java?
If you have a class that derives from a class that derives from another is there anyway for me to call the super.super method and not the overridden one?
class A{
public void some(){}
}
class B extends A{
public void some(){}
}
class C extends B{
public void some(){I want to call A.some();}
}
See: Why is super.super.method(); not allowed in Java?
#tgamblin is right but here is a workaround :
class A{
public void some(){ sharedCode() }
public final void someFromSuper(){ sharedCode() }
private void sharedCode() { //code in A.some() }
}
class B extends A{
#Override
public void some(){}
}
class C extends B{
#Override
public void some(){
//I want to call A.some();
someFromSuper();
}
}
Create a second version of your method in A that is final (not overridable) and call it from C.
This is actually a poor design, but sometimes needed and used inside JDK itself.
Regards,
Stéphane

Categories