I was just experimenting with abstract classes and bumped into an error.
I have one class as:
public abstract class C {
String aname;
int aid;
public C(String s, int n) {
aname = s;
aid = n;
}
public static void main(String[] args) {
System.out.println("Hello");
}
}
Now, another class extends it as follows:
public class D extends C {
public static void main(String[] args) {
}
}
However, it gives the following error:
error: constructor C in class C cannot be applied to given types;
public class D extends C {
^
required: String,int
found: no arguments
reason: actual and formal argument lists differ in length
Since class D does not define any constructor, the compiler automatically creates a default no-argument constructor for it. However, this constructor implicitly calls the no-argument constructor of the base class C, which is not defined -- a no-argument constructor exists if you explicitly define one or if you don't define any constructor. To solve it, you can either define a no-arg constructor for C:
public C() {
}
or you can define a constructor in D that has the same parameters as that of the parent class:
public D(String s, int n) {
super(s, n);
}
This has nothing to do with abstract classes and methods, and everything to do with the fact that you don't have a constructor declared in D.
You have only one constructor in C, and it takes 2 arguments. You did not declare a constructor in D, so Java implicitly inserts a no-arg, default constructor. It calls the superclass's default (no-arg) constructor, but that doesn't exist in C.
You must explicitly create a D constructor that calls C's 2-arg constructor or supply a no-arg constructor in C.
You have a parameterized constructor only in the abstract class. Try to have explicit default constructor or override the one in child.
Related
public class A{
public A(String x){
System.out.println("A constructor Called "+x);
}
public static void main(String []args){
System.out.println("Hello World");
A a= new B("b");
}
}
class B extends A{
public B(String x){
System.out.println("B constructor Called "+x);
}
}
What is the problem in this very simple program, I am unable to locate it.
Getting the following error on compile:
A.java:13: error: constructor A in class A cannot be applied to given types;
public B(String x){
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
Since class A does not have a default constructor, you need to tell class B how to construct its parent:
class B extends A{
public B(String x){
super(x); // this constructs the parent class
System.out.println("B constructor Called "+x);
}
}
The error is telling you that the constructor you must call requires a string:
required: String
... however the one you are calling (which is the the default constructor because you are not calling super ) has no arguments:
found: no arguments
You need to invoke the super constructor:
public B(String x){
super(x);
System.out.println("B constructor Called "+x);
}
Usually, there's an implicit call to a default no-arg constructor, but A doesn't have one of those. Writing super(x); will call the public A(String x) ... constructor, which is the only one available in the A class.
Just to add more.
If you will write no-argumnet constructor in hte parent class it will not show an error. Because by default super() call happens inside constructor. So when it's calling the parent class constructors it's not getting the no-argumnet constructor in the parent class.
class A {
A(int x) { System.out.println("constructor A"); } }
class B extends A {
B() { System.out.println(" constructor B"); } }
public class C {
public static void main(String args[]) { B b = new B(); } }
It says constructor from class A can't be applied for given types.
You have to call the constructor A(int x) explicitly in your constructor B(). I.e., you have to write
class B extends A {
B() {
super(<<< insert some int here>>>);
System.out.println(" constructor B");
}
}
If you do not add such a super call, then java will insert super(); for you which would try to call A(). As there is no constructor A() you receive the error that your constructor cannot be applied for the argument types.
Its always good to have a default constructor in a class if your writing a paramterized one.
class A {
A(){System.out.println("Default A");}
A(int x) { System.out.println("constructor A"); } }
class B extends A {
B() { System.out.println(" constructor B"); } }
public class C {
public static void main(String args[]) { B b = new B(); } }
If we don't specify a constructor to a class A, then a default constructor with no args will be associated to the class, if you specify one/more constructor to the class A, then you have to use one of them each time you want to create an object from this class.when a class B extend a class A, then constructors have to call one constructor of the parent class ( A ) by super() , if you don't specify a constructor to A then it's implecitly called be the constructor of B, if you explecitly defined constructor of A , then You have to call it when you create constructor of B like this :
B() { super(0)/*this call should be if the first line, you have to pass your default args if the constructor have args*/;
System.out.println(" constructor B");
}
what you are seeing is constructor chaining, read
snippet from the link.
If a constructor does not explicitly invoke a superclass constructor, the Java compiler
automatically inserts a call to the no-argument constructor of the superclass. If the super
class does not have a no-argument constructor, you will get a compile-time error. Object
does have such a constructor, so if Object is the only superclass, there is no problem.
I was wondering if it was possible to do the following with a Class, that extends another class in Java, if so. How?:
public class HelloWorld {
public HelloWorld() {
A aClass = new A(22);
}
}
public class A extends B {
public A() {
System.out.println(number);
}
}
public class B {
public int number;
public B(int number) {
this.number = number;
}
}
Your A constructor needs to chain to a B constructor using super. At the moment the only constructor in B takes an int parameters, so you need to specify one, e.g.
public A(int x) {
super(x); // Calls the B(number) constructor
System.out.println(number);
}
Note that I've added the x parameter into A, because of the way you're calling it in HelloWorld. You don't have to have the same parameters though. For example:
public A() {
super(10);
System.out.println(number); // Will print 10
}
Then call it with:
A a = new A();
Every subclass constructor either chains to another constructor within the same class (using this) or to a constructor in the superclass (using super or implicitly) as the first statement in the constructor body. If the chaining is implicit, it's always equivalent to specifying super();, i.e. invoking a parameterless superclass constructor.
See section 8.8.7 of the JLS for more details.
I am learning java. The book I'm reading has a question which asks what is wrong with the following code? I have typed the code in NetBeans and I can see the error but why is this error caused and how is it resolved?
The error is highlighted over the code public A(int t) and it says
Constructor B in class B cannot be applied to given types, require int, found no arguments, reason actual and formal arguments lists differ in length.
Here is the code:
public class Test {
public static void main(String[] args) {
B b = new B(5);
}
}
class A extends B {
public A(int t) {
System.out.println("A's constructor is invoked");
}
}
class B {
public B(int k) {
System.out.println("B's constructor is invoked");
}
}
when your super class has an args constructor and doesn't have a no-args constructor you have to explicitly invoke it using a super(args) call from sub-class constructor
class A extends B {
public A(int t) {
super(t);
System.out.println("A's constructor is invoked");
}
}
Class B has defined constructor, so it does not have public implicit default constructor (with no arguments). All subclass constructors have to explicitly call superclass constructors, via super(t), if zero argument superclass constructor is not available.
The class B has only one constructor which takes an int argument. On the other hand, the constructor you have in class A (implicitly) tries to call a constructor in A's super class (namely B) that takes no arguments. This is the cause of the error. There are at least two ways to fix the problem:
Create a no-arg constructor in class B.
Explicitly call the constructor in class B which takes an int as a parameter. You can do this using the super keyword.
You need to call the constructor of the super class as the first statement in A(int):
public A(int t) {
super(t);
System.out.println("A's constructor is invoked");
}
How to inherit the constructor from a super class to a sub class?
Constructors are not inherited, you must create a new, identically prototyped constructor in the subclass that maps to its matching constructor in the superclass.
Here is an example of how this works:
class Foo {
Foo(String str) { }
}
class Bar extends Foo {
Bar(String str) {
// Here I am explicitly calling the superclass
// constructor - since constructors are not inherited
// you must chain them like this.
super(str);
}
}
Superclass constructor CAN'T be inherited in extended class. Although it can be invoked in extended class constructor's with super() as the first statement.
Default constructors -- public constructors with out arguments (either declared or implied) -- are inherited by default. You can try the following code for an example of this:
public class CtorTest {
public static void main(String[] args) {
final Sub sub = new Sub();
System.err.println("Finished.");
}
private static class Base {
public Base() {
System.err.println("In Base ctor");
}
}
private static class Sub extends Base {
public Sub() {
System.err.println("In Sub ctor");
}
}
}
If you want to explicitly call a constructor from a super class, you need to do something like this:
public class Ctor2Test {
public static void main(String[] args) {
final Sub sub = new Sub();
System.err.println("Finished.");
}
private static class Base {
public Base() {
System.err.println("In Base ctor");
}
public Base(final String toPrint) {
System.err.println("In Base ctor. To Print: " + toPrint);
}
}
private static class Sub extends Base {
public Sub() {
super("Hello World!");
System.err.println("In Sub ctor");
}
}
}
The only caveat is that the super() call must come as the first line of your constructor, else the compiler will get mad at you.
Read about the super keyword (Scroll down the Subclass Constructors). If I understand your question, you probably want to call a superclass constructor?
It is worth noting that the Java compiler will automatically put in a no-arg constructor call to the superclass if you do not explicitly invoke a superclass constructor.
Say if you have
/**
*
*/
public KKSSocket(final KKSApp app, final String name) {
this.app = app;
this.name = name;
...
}
then a sub-class named KKSUDPSocket extending KKSSocket could have:
/**
* #param app
* #param path
* #param remoteAddr
*/
public KKSUDPSocket(KKSApp app, String path, KKSAddress remoteAddr) {
super(app, path, remoteAddr);
}
and
/**
* #param app
* #param path
*/
public KKSUDPSocket(KKSApp app, String path) {
super(app, path);
}
You simply pass the arguments up the constructor chain, like method calls to super classes, but using super(...) which references the super-class constructor and passes in the given args.
You inherit class attributes, not class constructors .This is how it goes :
If no constructor is added in the super class, if no then the compiler adds a no argument contructor. This default constructor is invoked implicitly whenever a new instance of the sub class is created . Here the sub class may or may not have constructor, all is ok .
if a constructor is provided in the super class, the compiler will see if it is a no arg constructor or a constructor with parameters.
if no args, then the compiler will invoke it for any sub class instanciation . Here also the sub class may or may not have constructor, all is ok .
if 1 or more contructors in the parent class have parameters and no args constructor is absent, then the subclass has to have at least 1 constructor where an implicit call for the parent class construct is made via super (parent_contractor params) .
this way you are sure that the inherited class attributes are always instanciated .