I have 2 examples for inheritance in Java The first works but the second not. I wonder why.
Does the extended class must implement constructor with super()

class Counter {
	  int i = 0;
	public Counter()
	{
		i=1;
	}
	  Counter increment() {
	    i++;
	    return this;
	  }

	  void print() {
	    System.out.println("i = " + i);
	  }
  }

	  public class CounterDemo extends Counter{
		  public static void main(String[] args) {
			  Counter x = new Counter();
			  x.increment().increment().increment().print();
		  }  
	  }
  
import java.io.*;
class Base
{
	int i;
	public Base(int i)
	{
		this.i=i;
	}
	public void print()
	{
		System.out.println("Value of i "+i);
	}
} 

public class MainClass extends Base
{
	
	int i;
	public static void main(String[] args)
	{
		MainClass m=new MainClass();
		 
		
	}
}

in the second example tells that "Cant find symbol constructor Base()"

This is just a guess, but I'm somewhat certain that if you don't explicitly call the constructor, the compiler will put in a super() call for you, which calls the default constructor (the constructor with no parameters). Since your Base class does not have a constructor w/ no parameters, that is why you are getting the error. Either add a constructor with no parameters, or in the class where you get the error, add a call to an existing constructor (like Base(int i) constructor)

A superclass must always be constructed before its children. There are two things that are implicit in your MainClass class.

1) Since you did not specify any constructors, you have an implicit default constructor:

public MainClass() {
}

2) Since you did not specify which constructor for your superclass to construct from, so it tries to use the default superclass constructor, which, in this case, doesn't exist.

public MainClass() {
    super(); // error!
}

^^ Isn't that exactly what I just said? Lol

this works OK

class Base
{
int i;
public Base(int i)
{
this.i=i;
}
public void print()
{
System.out.println("Value of i "+i);
}
}


public class MainClass extends Base
{
int i,k;
public MainClass()
{
super(0);
i=0;
}


public static void main(String[] args)
{
MainClass m=new MainClass();
m.print();



}
}


but this not


class Base
{
int i;
public Base(int i)
{
this.i=i;
}
public void print()
{
System.out.println("Value of i "+i);
}
}


public class MainClass extends Base
{
int i,k;
public MainClass()
{
//  super(0);
i=0;
}


public static void main(String[] args)
{
MainClass m=new MainClass();
m.print();



}
}

and the meaning of this is that in this case we must call the base constructor with super() but my question is how in th first class CounterDemo extends Counteexample we do not need super();

in th first example means
lass CounterDemo extends Counte

^^ Isn't that exactly what I just said? Lol

We posted at the same time.

and the meaning of this is that in this case we must call the base constructor with super() but my question is how in th first class CounterDemo extends Counteexample we do not need super();

You do need it. But it's there implicitly. It works because that superclass has a constructor that takes no arguments.

> and the meaning of this is that in this case we must call the base constructor with super()
> but my question is how in th first class CounterDemo extends Counteexample we do not
> need super();

In case you don't provide a constructor for your class, the compiler automatically inserts one with the same access modifier as that of the class for you. It is called a *default* constructor. A default constructor is a no-arg constructor but an explicitly provided no-arg constructor isn't a default one. A few examples:

// original code
public class A {}

// generated code
public class A {

  // default constructor
  public A() {
    super();
  }

}
// original code
class B {}

// generated code
class B {
  
  // default constructor
  public B() {
    super();
  }

}
// Original code
public class C {
  
  // An explicitly created no-arg constructor
  public C() {
  }

  public C(int i) {  
  }

}

// Generated code
public class C {
  
  public C() {
    super();
  }

  public C(int i) {
    super();
  }

}

Of course, things start getting hairy when Serialization is involved, but that's a story for some other day...

You can read more about it here.

thnx a lot Destin this Was my problem

But it's there implicitly. It works because that superclass has a constructor that takes no arguments.

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.