JAVA: Proper way to use superclass' getter and setter in subclass - java

I have a class Person and its subclass Student:
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Student extends Person {
private int grade;
public Student(String name, int grade) {
super(name);
this.grade = grade;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public void printDescription() {
System.out.println("Name: " + getName());
System.out.println("Grade: " + Integer.toString(grade));
}
}
So Person have getter and setter for name property and Student have only getter and setter for its new grade property, as long as a printDescription() method.
The problem is how should I call the name property in Student's printDescription() method correctly?
I implemented it like in code above considering that Student inherits getter and setter from parent class.
But at my university Java teacher asks to use it like this:
public void printDescription() {
System.out.println("Name: " + super.getName());
System.out.println("Grade: " + Integer.toString(grade));
}
So he offers to directly call parent's getter.
I think it is not the best way because in case we override name's getter in Student class, getter from Person will still be called instead.
So what approach is best in this situation to use name property?
UPD: it is important to mention that for this task there is no requirement to call specifically superclass' getter implementation, this is why I was confused by teacher's recommendation to use super.

You're correct that if you override the method in the subclass and you're using the super keyword then you'll invoke the method on the parent class.
In this case unless you wanted to guarantee that the method in the parent class was used then it's fine to just invoke the method without the super keyword and that way if you override the method then you get the behaviour you want in the subclass.

if you extends a class , it will has all properties in its superclass. instead of calling super.getName() you can just call this.getName().

Related

Purpose of this() in following code snippet? [duplicate]

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.

Constructor printing out NULL instead of object value

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());
}
}

How the code is working here in the subclass?

package practice;
class person{
private String firstname;
private String lastname;
public person(String firstname,String lastname){
set_first(firstname);
set_last(lastname);
}
public String get_first() {
return firstname;
}
public void set_first(String firstname) {
this.firstname=firstname;
}
public void set_last(String lastname) {
this.lastname=lastname;
}
public String get_last() {
return lastname;
}
}
class employee extends person {
private int empid;
public employee(String firstname, String lastname, int empid) {
super(firstname,lastname);
set_empid(empid);
}
public void set_empid(int empid) {
this.empid=empid;
}
public int get_empid() {
return empid;
}
}
class testing_super_keyword {
public static void main(String args[]) {
employee emp=new employee("Paul","Anderson",1234);
System.out.println(emp.get_first()+" "+emp.get_last());
System.out.println(emp.get_empid());
}
}
I got two classes here person superclass and employee subclass. So i just wanted to know this code isn't supposed to work as the firstname and lastname variable is private in superclass? But how the subclass i.e employee is inheriting those members and using it??
I thought private variable of superclass cant be inherited so how come it works fine here?
I am totally confused please help......
Although, the private variables of parent class are not inherited by child class i.e employee but there are public functions that are called getter and setter that allows to access the private members of class from its sub-class.
public String get_first() {
return firstname;
}
public void set_first(String firstname) {
this.firstname=firstname;
}
public void set_last(String lastname) {
this.lastname=lastname;
}
public String get_last() {
return lastname;
}
You see when you want to access firstname from parent, you'll call get_first() from employee object to get the firstname. If you want to set the firstname you'll call set_first("name") to set the name. Hope it might help.
private variable of superclass cant be inherited
Yes, you are absolutely right, they won't be inheriting. But in your code, you are not accessing those fields directly right?
public employee(String firstname,String lastname,int empid){
super(firstname,lastname);
set_empid(empid);
}
Here you are passing parameters (from the main method) to your employee constructor, the names of parameters are similar to the fields in person, but they are not the same. You can change parameters names like this and it will still work fine.
public employee(String fName, String lName,int empid){
super(fName,lName);
set_empid(empid);
}
Here the parameter values are taken into super class constructor and its private fields are initialized and then empid of employee is initialized.
System.out.println(emp.get_first()+" "+emp.get_last());
System.out.println(emp.get_empid());
Here also you are not accessing the private fields directly, you are calling the public methods which will be inherited to the employee and can be called on its reference.
Keeping members of a class as private and their behaviours (methods) as public is a part of encapsulation, so that you cannot directly access then but can set and get its value using public methods.
PS : Try to make the getters and setters using the IDE, if it gives the option and try to follow naming conventions for classes and methods.
lets review how firstname and lastname are being initialized here:
in subclass constructor firstname, lastname has been taken
then in the body of constructor by super(firstname,lastname), firstname and lastname pass to the parent (i.e. Person) to be taken care of.
so assume we are now in the parent constructor (Person) so, By calling set_first(firstname) and set_last(lastname) , parameters firstname and lastname is being set in parent class itself with the values which passed from the child constructor (i.e. Employee)
Regarding this description there is no violation.
The violation happens if you want to initialize private variable directly in Employee class as :
public employee(String firstname,String lastname,int empid){
this.firstname=firstname; //Violation
this.lastname=lastname; //Violation
set_empid(empid);
}

Trying to implement the interface Comparable

just wondering what this error means and how I should fix this.
Error: The type Student must implement the inherited abstract method java.lang.Comparable.compareTo(java.lang.Object)
I am trying to implement this so I can use the class's compareTo Method.
Thanks so much for your help!
import java.util.*;
import java.io.*;
public class Student implements Comparable
{
private String name;
private double gpa;
public Student()
{
name = "";
gpa = 0.0;
}//end default constructor
public Student(String n, double g)
{
name = n;
gpa = g;
}//end two arg constructor
public double getGPA()
{
return gpa;
}
public String getName()
{
return name;
}
public void setGPA(double g)
{
this.gpa = g;
}
public void setName(String n)
{
this.name = n;
}
public String toString()
{
return " Name: " + name + " GPA: " + gpa;
}
public static void compareTo()
{
}
}//end class
All you have to do is implement the comparable interface and override the compareTo() method in the class you wanted to be sorted. Inside the compareTo method you must mention on which basis your object should be sorted. The below code will be helpful in this:
public class Student implements Comparable<Student>
{
private String name;
private double gpa;
public Student()
{
name = "";
gpa = 0.0;
}//end default constructor
public Student(String n, double g)
{
name = n;
gpa = g;
}//end two arg constructor
public double getGPA()
{
return gpa;
}
public String getName()
{
return name;
}
public void setGPA(double g)
{
this.gpa = g;
}
public void setName(String n)
{
this.name = n;
}
public String toString()
{
return " Name: " + name + " GPA: " + gpa;
}
public Integer compareTo(Student student)
{
// if object is getting sorted on the basis of Name
return this.getName().compareTo(student.getName())
// if object is getting sorted on the basis of gpa
return Double.valueOf(this.gpa).compareTo(Double.valueOf(student.getGPA()));
}
}//end class
Since you are using primitive data type double instead of Object Double so we need to get the object using Double.valueOf(this.gpa)
You should use only one return statement as per your requirement.
You have to make the following changes to your code in order to implement the Comparable interface correctly.
(1) Specify the type of object your class is comparable with:
public class Student implements Comparable<Student>
(2) And the corresponding signature of the compareTo method is as follow:
public int compareTo(Student other)
And of course you have to implement the body of the compareTo method too.
If the this instance is "smaller" than the argument other then compareTo should return a negative number.
If the this instance is "larger" than the argument other then compareTo should return a positive number.
If the this instance is considered to be "of the same value" as the argument other then compareTo should return 0.
You may refer to the official java document for more information about the implementation details.
Note that Interface Comparable<T> is a generic interface. Here T is type (Class, Interface mostly). You want to compare Student with another type (Student).
So you have to pass type(T) Student to Comparable<T>.
Hence you change it to
public class Student implements Comparable<Student>
Another point you have to implement the Comparable<T> interface as same method signature and return type to Student class.
public int compareTo(Student anotherStudent)
You have written
public static void compareTo()
which not follow the interface Comparable<T> method signature and return type in Student class.
Here you should implement your domain specific logic. I don't know how you want to implement it ? You can compare the name alphabetically or base on their CGPA.

Use of "this" keyword in java [duplicate]

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
}
}

Categories