• Register
1 vote
107 views

Problem:

Hello all,
Please explain to me the error in the following program.

​class test
{
  String name="name";
  public person(String nm){name=nm;}
}
 
class emp extends test
{
  String empid="0000";
}
 
public class emptest
{
  public static void main(String args[])
  {
    test p=new test("hello");
 
    System.out.println(p.test);
  }
}

The error I am getting is:

javac emptest.java
emptest.java:8: error: constructor test in class test cannot be applied to given types;
class emp extends test
required: String
found no arguments
reason: actual and formal argument lists differ in length

Thanks for the effort.

Best regards

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution:

If you don't add a constructor to a class the compiler adds a no-arg constructor for you. So one is added to the emp class.
All constructors call (one way or another) a superclass constructor. If you don't add an explicit call to one then the compiler adds one for you.

So the code is equivalent to:

class emp extends test  
{  
  String empid="0000";  
 
  emp() 
  {
    super();
  }
}

But that means it's trying to call a superclass constructor with no arguments. But there isn't one - the person class constructor requires a String.

What you've effectively done is to say that all person objects must have a name provided. But then your emp objects - which are also person objects - don't provide a name. The compiler considers that an error.

13 9 6
94,240 points

Related questions

0 votes
1 answer 10 views
10 views
Help me by solving this problem.
asked Dec 18, 2020 TeamScript 9.3k points
0 votes
1 answer 7 views
7 views
Problem: Hi there! I recently attempted an exam of Programming in Java. There was a statement that was to be marked as true or false and I had to justify my answer. The statement was: A protected member of a class may be directly accessed by ... concepts in programming and I am not sure whether I understand inheritance properly. Can anybody here help me clear my concepts more accurately? Thanks
asked Dec 27, 2020 Code Learner 5.5k points
0 votes
1 answer 16 views
16 views
Problem: Hi there! I have been learning to program for a long time now and I recently learned inheritance in Java. I wrote the following program for practice: class Institutions { public void print_institutions() { System.out.println("This is an institution"); } } class ... , I have no idea what is causing this error. Can anybody here please clarify the cause of the error and its solution? Thanks!
asked Dec 29, 2020 Code Learner 5.5k points
0 votes
1 answer 73 views
73 views
Problem : I have the below two classes public class A {     A() {         System.out.println("A");     } } class B extends A {     B() {         System.out.println("B");     } } And after that running 1 A c = new B(); or 2 B c = new B( ... glance, in either of the scenario, I would assume that only a B constructor would be called and so the only output would be B But this is completely wrong.
asked Jan 23, 2020 jwilliam 3.9k points
0 votes
1 answer 16 views
16 views
Problem: Hello Programmers! I just gave an interview for a programming job. The interviewer asked me a question regarding inheritance in Java. The question was: Can subclass access the private members of a superclass? I answered: No!. But, I am still confused about whether I answered correctly or not. Can any of you tell me whether my answer was correct or not? Also, explain why and why not?
asked Dec 17, 2020 Code Learner 5.5k points
0 votes
1 answer 13 views
13 views
Problem: a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. Which overriding method has the same name, number and type of parameters, and return type as the method that it overrides?
asked Dec 5, 2020 Mashhoodch 5.6k points
0 votes
1 answer 45 views
45 views
Problem : I have following two classes: JDialog and JFrame (see signature below). I want them to have few extra methods, but I also want them both to extend my code, without rewriting the code Is it possible to do that? java.awt.Window java.awt.Dialog javax.swing. ... the identical way. So I will not want to write the code twice. I want only one class to utilize the code of the other class.
asked Dec 7, 2019 alecxe 7.5k points
0 votes
1 answer 12 views
12 views
Problem: I just recently started learning inheritance in Java. I am a bit confused between method overloading and method overriding. If two classes have same method with different number of arguments, given that the classes have parent-child relationship, will it be overloading or overriding? Moreover, when does the overloading occurs in inherited classes?
asked Jan 8 Code Learner 5.5k points
0 votes
1 answer 24 views