Java is not my strong suite, so please go easy! :)
I am trying to do construtor-chaining between below super and sub class
//SuperClass
class Furniture{
String name;
int cost;
boolean IsAvlbl;
void Furniture(String name,int cost,boolean IsAvlbl){
this.name = name;
this.cost = cost;
this.IsAvlbl = IsAvlbl;
}
}
//Sub-class
public class Table extends Furniture{
public Table(String name,int cost,boolean IsAvlbl)
{
super(name,cost,IsAvlbl);
}
public static void main(String args[])
{
Table t = new Table("dinning",2600,false);
t.runner();
}
void runner()
{
System.out.println("Name : "+this.name);
System.out.println("Cost : "+this.cost);
System.out.println("Is Avaiable : "+this.IsAvlbl);
}
}
Error popping up is :
Table.java:20: error: constructor Furniture in class Furniture cannot be applied to given types;
super(name,cost,IsAvlbl);
^ required: no arguments found: String,int,boolean
reason: actual and formal argument lists differ in length 1 error
I understand that constructor call has to be the first line and parameters have to be same....I tried doing it but error is persistent.
I would appreciate if some one can tell me why this error is popping up as i want to understand the cause of it..... try it this way kind of solution is not preferred!
Constructor will not have any return type
it should be like
Furniture(String name,int cost,boolean IsAvlbl){
this.name = name;
this.cost = cost;
this.IsAvlbl = IsAvlbl;
}
you have void keyword before constructor, which makes it a method. Since you had return type, java found only "no argument" (default) constructor in Furniture class, hence giving that compilation error.
In Java, constructor should not have return type. Remove void from Furniture constructor
Furniture(String name,int cost,boolean IsAvlbl){
this.name = name;
this.cost = cost;
this.IsAvlbl = IsAvlbl;
}
void Furniture(String name,int cost,boolean IsAvlbl){ // Method
is a method and not a constructor.
Replace it with :
Furniture(String name,int cost,boolean IsAvlbl){ // Constructor
Remember, constructors do not have return-types. Interestingly, you can define a method with the Class-Name (similar to constructors) but the only difference is that, methods will have return-types.
void Furniture() is not the constructor. It is treated as a method in your Furniture class.
public Furniture() is the proper syntax as constructors do not have any return type.
public Furniture(String name, int cost, boolean IsAvlbl) {
this.name = name;
this.cost = cost;
this.IsAvlbl = IsAvlbl;
}
Related
This question already has answers here:
this() and this
(4 answers)
Closed 2 years ago.
package test;
public class Employee {
String name;
int age;
Employee() {}
Employee(String newName, int newAge) {
this();
name = newName;
age = newAge;
}
public static void main(String args[]) {
Employee e = new Employee("N", 43);
System.out.println();
}
}
In the above code, what is the actual point of "this()" in the overloaded constructor from a usefulness perspective besides being an example of calling the no-argument constructor from the overloaded constructor?
This looks like it's the wrong way round to me.
A typical use of this() would be to provide a default value for the constructor when not supplied in the argument as you cannot call the constructor directly. Adapting your code for example:
package test;
public class Employee {
String name;
int age;
Employee() {
this("Default", 42);
}
Employee(String newName, int newAge) {
name = newName;
age = newAge;
}
public static void main(String args[]) {
Employee e = new Employee();
System.out.println();
}
}
In that exact snippet?
Literally nothing.
Any constructor MUST neccessarily have, at the top of it, either a this() call, or a super() call. You can't not. If you fail to write it, javac will inject: super(); for you, and if that isn't valid (for example, your superclass does not have a protected+ no-args constructor), then your code won't compile.
So that's the difference between the snippet as pasted and a hypothetical one where the this(); is removed. Desugaring, you get either:
Desugared, WITHOUT the this():
package test;
public class Employee {
String name;
int age;
Employee() {
super(); // invokes java.lang.Object's no-args, which does nothing.
}
Employee(String newName, int newAge) {
super(); // invokes java.lang.Object's no-args, which does nothing.
name = newName;
age = newAge;
}
}
Desugared, WITH the this():
package test;
public class Employee {
String name;
int age;
Employee() {
super(); // invokes java.lang.Object's no-args, which does nothing.
}
Employee(String newName, int newAge) {
this();
name = newName;
age = newAge;
}
}
Now, inject, say, a System.out.println("Hello!"); in the no-args constructor and now there is a small difference: With the this(), you'd see Hello! printed, and without it, you won't. Either way, though, you end up calling your superclass's constructor, as that is something that has to happen, one way or another. (Only java.lang.Object doesn't have to, hardcoded in the VM; Object has no superclass).
To call the default constructor, this() is used.
package main.java;
public class Demo {
public static void main(String[] args) {
BClass bClass=new BClass("han","男");
AClass aClass=bClass;
System.out.println(aClass.getSex());
System.out.println(aClass.sex);
}
}
The execution result of this class is
男
null
The results are confusing to me. When the superclass calls the overridden method, the results meet my expectations, but when it calls the overridden variable, the results confuse me.so why does a superclass reference calling an overridden method appear polymorphic, but not if it takes an overridden member variable?Here's the entire code.
package main.java;
public class Demo {
public static void main(String[] args) {
BClass bClass=new BClass("han","男");
AClass aClass=bClass;
System.out.println(aClass.getSex());
System.out.println(aClass.sex);
}
}
package main.java;
public class AClass {
private String name;
public String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
package main.java;
public class BClass extends AClass{
private String sex;
public BClass(String name,String sex) {
this.sex = sex;
super.setName(name);
}
#Override
public String getSex() {
return sex;
}
#Override
public void setSex(String sex) {
this.sex = sex;
}
}
While you can override a method, you can't override a field in a subclass; you are actually just declaring a field with the same name. To allow the field to also be visible in the child class, you can change its visibility to protected or package private (default modifier), if both classes are in the same package. Demo.
public class BClass extends AClass{
public BClass(String name,String sex) {
this.sex = sex;
super.setName(name);
}
#Override
public String getSex() {
return sex;
}
#Override
public void setSex(String sex) {
this.sex = sex;
}
}
public class AClass {
protected String name, sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Java doesn't allow you to really override a field.
Your BClass actually has two fields named sex, one from AClass, and one from BClass. And Java syntax doesn't really help you finding out which one is meant when you write something like x.sex. It's as if you had defined two different fields, sex_a in AClass and sex_b in BClass, only with the complication that references to both are written like x.sex, without a clear hint which of the two is meant here.
In your case:
Your BClass instance will have its sex_b initialized, and the sex_a empty (null).
aClass.getSex() always calls the most specific method, based on the instance's runtime class, being BClass. So it chooses the method from BClass, returning sex_b, and thus prints the sex.
aClass.sex accesses one of the two sex fields, depending on the variable's compile-time-deducible type, in your case being AClass. So it prints the sex_a value, being null.
Seasoned Java developers typically do their best to avoid this situation, as it can be very confusing.
If the two fields conceptually have the same meaning, do it as you did with the name field, having only one field in the parent class, and have the subclass access it via getters and setters (or by declaring protected visibility for the field).
If the two fields have conceptually different meanings (can an object have two different sexes?), use different names.
As per the Java specifications, the instance variables are not overridden from a super class by a sub class when it is extended.
I created a new class "Lecturer" which extends another class "Person", i wanted to make 2 constructors for Lecturer and one would accept a name and a stipend (just a constant to say how much pay is), the other just accepts the name and uses the default stipend set in the code. i included appropriate getters and setters. I then wrote a writeOutput method to print an output similar to this
Name: (name) which gets the name and prints it
Stipend: (stipend) same process ^
heres what i have so far
Lecturer.java
public class Lecturer extends Person{
private static String name;
static double stipend;
public Lecturer(String name) {
super(name);
}
public Lecturer(String name, double stipend) {
super(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getStipend() {
return stipend;
}
public void setStipend(double stipend) {
this.stipend = stipend;
}
public static void writeOutput() {
System.out.println("Name: " + name);
System.out.println("Stipend: " + stipend);
}
}
Person.java
public class Person {
/** Every Person has a name */
private String name;
/** Person requires a name */
public Person(String n) {
this.name = n;
}
/** return this Person's name */
public String getName() {
return this.name;
}
/** Change this Person's name */
public void setName(String nn) {
this.name = nn;
}
Main file (Inheritance.java)
Lines 41-53
Lecturer l1 = new Lecturer("Zachary");
Lecturer l2 = new Lecturer("Wilhelmina", 11017.00);
l1.writeOutput();
l2.writeOutput();
pause();
l1.setName("Zack");
l1.setStipend(10800.00);
l1.writeOutput();
pause();
System.out.printf("%s's stipend is $%,4.2f.\n",
l1.getName(), l1.getStipend());
System.out.printf("%s's stipend is $%,4.2f.\n",
l2.getName(), l2.getStipend());
This is the output
Name: null
Stipend: 0.0
Name: null
Stipend: 0.0
press enter...
Name: Zack
Stipend: 10800.0
The 2nd part works as it should but the first one isnt and i tried to change the code but nothing is working properly.
In Lecturer you are declaring another name variable. This variable is separate from the name variable declared in Person. The call to the superclass constructor is setting the name variable in Person, not in Lecturer. But you don't need the second variable; remove it. You can access the name in Person via the getName method you've already declared. This means that you also don't need to re-declare getName and setName in Lecturer, so the Lecturer class can inherit them.
Also, in Lecturer, the two variables you've declared shouldn't be static. Per the above reasoning, name shouldn't even be there, but even if it should be there, it shouldn't be static. The variable stipend should be there, but it shouldn't be static. When you declare a member variable static, then there is only one variable for the entire class, no matter how many instances you create, which doesn't sound like what you want.
Your constructors should initialize stipend.
You have a static variable inside Lecturer which has the same name as the inherited one from Person and your getter is referring to that static one - are you sure you want these static variables? For completeness if you really want to keep the static one and the inherited one with the same name then change your getter to read return this.name; which will return the inherited name instance variable.... But that method can be inherited from Person class...
There are two name fields in your program , one is private static String name; in Lecturer.java and another is private String name; in person.java .
The thing is that you are just calling Lecturer javs's name field but not setting it.
Fixed the project based on rgettman answer.
Lecturer class should look like this:
public class Lecturer extends Person {
double stipend = 9144;
public Lecturer(String n) {
super(n);
}
public Lecturer(String n, double stipend) {
super(n);
this.stipend = stipend;
}
public double getStipend() {
return stipend;
}
public void setStipend(double stipend) {
this.stipend = stipend;
}
public void writeOutput() {
System.out.println("Name: " + this.getName());
System.out.println("Stipend: " + getStipend());
}
}
public class EmployeeTest {
public static void main(String args[]) {
Employee e = new Employee ("Jordan",70000.00);
Manager m = new Manager ("William Johnson",90000.00,"Computer Science");
Executive ex = new Executive ("GPC",120000.00,"School");
System.out.println(e);
System.out.println(m);
System.out.println(ex);
}
Here is my constructor:
public class Employee {
private String name;
private double salary;
public Employee(String name, double salary, int department) {
setName(name);
setSalary(salary);
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return this.salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void getDetails() {
System.out.println("Name:" + getName());
System.out.println("Salary:" +getSalary());
}
}
I am currently stumped with this assignment. I have all four of my classes implemented with Employee. Then Manager is inheriting from Employee while also having Executive inherit from Manager. When I go to compile it I get these 2 errors found:
What am I doing wrong?
File: C:\Users\Jordan\Downloads\EmployeeTest.java [line: 6]
Error: constructor Employee in class Employee cannot be applied to given types;
required: java.lang.String,double,int
found: java.lang.String,double
reason: actual and formal argument lists differ in length
File: C:\Users\Jordan\Downloads\Manager.java [line: 9]
Error: constructor Employee in class Employee cannot be applied to given types;
required: java.lang.String,double,int
found: java.lang.String,double
reason: actual and formal argument lists differ in length
You need to pass in one extra argument to your employee creation call like so:
Employee e = new Employee ("Jordan",70000.00,42);
You have defined the constructor as String, double, int so you have to supply all three.
Your output clearly says
Error: constructor Employee in class Employee cannot be applied to given types;
required: java.lang.String,double,int
found: java.lang.String,double
So instead of calling Employee e = new Employee ("Jordan",70000.00) constructor you need to pass three arguments to it which are String, Double and int. For example,
Employee e = new Employee ("Jordan",70000.00, 1); //I passed 1 as args for example it could be anything that you defined in your code.
Edit
There you go, you just posted your Employee class code which clearly shows you have created a constructor that takes three arguments String name, double salary, int department and it looks as if you completely forgot what you want to do with the third arguments department as I do not see it being used anywhere else in the code.
This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 7 years ago.
I was studying method overriding in Java when ai came across the this keyword. After searching much about this on the Internet and other sources, I concluded that thethis keyword is used when the name of an instance variables is same to the constructor function
parameters. Am I right or wrong?
this is an alias or a name for the current instance inside the instance. It is useful for disambiguating instance variables from locals (including parameters), but it can be used by itself to simply refer to member variables and methods, invoke other constructor overloads, or simply to refer to the instance. Some examples of applicable uses (not exhaustive):
class Foo
{
private int bar;
public Foo() {
this(42); // invoke parameterized constructor
}
public Foo(int bar) {
this.bar = bar; // disambiguate
}
public void frob() {
this.baz(); // used "just because"
}
private void baz() {
System.out.println("whatever");
}
}
this keyword can be used for (It cannot be used with static methods):
To get reference of an object through which that method is called within it(instance method).
To avoid field shadowed by a method or constructor parameter.
To invoke constructor of same class.
In case of method overridden, this is used to invoke method of current class.
To make reference to an inner class. e.g ClassName.this
To create an object of inner class e.g enclosingObjectReference.new EnclosedClass
You are right, but this is only a usage scenario, not a definition. The this keyword refers to the "current object". It is mostly used so that an object can pass itself as a parameter to a method of another object.
So, for example, if there is an object called Person, and an object called PersonSaver, and you invoke Person.SaveYourself(), then Person might just do the following: PersonSaver.Save( this );
Now, it just so happens that this can also be used to disambiguate between instance data and parameters to the constructor or to methods, if they happen to be identical.
this keyword have following uses
1.used to refer current class instance variable
class Student{
int id;
String name;
student(int id,String name){
this.id = id;
this.name = name;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();
}
}
here parameter and instance variable are same that is why we are using this
2.used to invoke current class constructor
class Student{
int id;
String name;
Student (){System.out.println("default constructor is invoked");}
Student(int id,String name){
this ();//it is used to invoked current class constructor.
this.id = id;
this.name = name;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student e1 = new Student(111,"karan");
Student e2 = new Student(222,"Aryan");
e1.display();
e2.display();
}
}
3.this keyword can be used to invoke current class method (implicitly)
4.this can be passed argument in the method call
5.this can be passed argument in the constructor call
6.this can also be used to return the current class instance
This refers current object. If you have class with variables int A and a method xyz part of the class has int A, just to differentiate which 'A' you are referring, you will use this.A. This is one example case only.
public class Test
{
int a;
public void testMethod(int a)
{
this.a = a;
//Here this.a is variable 'a' of this instance. parameter 'a' is parameter.
}
}
Generally the usage of 'this' is reserved for instance variables and methods, not class methods ...
"class methods cannot use the this keyword as there is no instance for
this to refer to..."
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
Here's a trivial example ...
public class Person {
private String name;
private int age;
private double weight;
private String height;
private String gender;
private String race;
public void setName( String name ) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setAge( int age) {
this.age = age;
}
public int getAge(){
return this.age;
}
public void setWeight( double weight) {
this.weight = weight;
}
public double getWeight() {
return this.weight;
}
public void setHeight( String height ) {
this.height = height;
}
public String getHeight() {
return this.height;
}
public void setGender( String gender) {
this.gender = gender;
}
public String getGender() {
return this.gender;
}
public void setRace( String race) {
this.race = race;
}
public String getRace() {
return this.race;
}
public void displayPerson() {
System.out.println( "This persons name is :" + this.getName() );
System.out.println( "This persons age is :" + this.getAge() );
System.out.println( "This persons weight is :" + this.getWeight() );
System.out.println( "This persons height is :" + this.getHeight() );
System.out.println( "This persons Gender is :" + this.getGender() );
System.out.println( "This persons race is :" + this.getRace() );
}
}
And for an instance of a person ....
public class PersonTest {
public static void main( String... args ) {
Person me = new Person();
me.setName( "My Name" );
me.setAge( 42 );
me.setWeight( 185.00 );
me.setHeight( "6'0" );
me.setGender( "Male" );
me.setRace( "Caucasian" );
me.displayPerson();
}
}
In case of member variable and local variable name conflict, this key word can be used to refer member variable like,
public Loan(String type, double interest){
this.type = type;
this.interest = interest;
}
if you have knowladge about c,c++ or pointers, in that language this is a pointer that points object itself. In java everything is reference. So it is reference to itself in java. One of the needs of this keyword is that:
Think that this is your class
public class MyClass
{
public int myVar;
public int myMethod(int myVar)
{
this.myVar = myVar; // fields is set by parameter
}
}
If there is not this keyword you it is confused that this is paramter or class field.When you use this.myVar it refers field of this object.
I would like to modify your language. The this keyword is used when you need to use class global variable in the constructors.
public class demo{
String name;
public void setName(String name){
this.name = name; //This should be first statement of method.
}
}
this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
One more thing that should be in mind is that this keyword might be the first statement of your method.
This is used in java. We can use in inheritance & also use in method overloading & method overriding. Because the actual parameter or instance variable name has same name then we can used this keyword complsary . But some times this is not same as when we can not use this keyword complsary.....
Eg:- class super
{
int x;
super(int x)
{
this.x=x
}
}