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()"