public abstract class Person {
private String name;
public Person(String name) {
this.name = name;
System.out.println("Person");
}
public String getName() {
return name;
}
abstract public String getDescription();
}
public class Student extends Person {
private String major;
public Student(String name, String major) {
super(name);
this.major = major;
}
public String getMajor() {
return major;
}
#Override
public String getDescription() {
return "student" + super.getName() + " having" + major;
}
}
public class PersonTest {
public static void main(String[] args) {
Person person = new Student("XYZ", "ABC");
System.out.println(person.getDescription());
}
}
Ques: We cannot create objects of abstract classes, then why Person Constructor has been invoked, even its an abstract class?
Because it's still a class and its constructor is invoked as a part of the object instantiation. The fact that it is abstract doesn't have anything to do with this.
Ques: We cannot create objects of
abstract classes, then why Person
Constructor has been invoked, even its
an abstract class?
If a class is declared abstract, no objects of that class can be created. That DOESNOT mean you cannot create objects of its subclasses.
You can have references(of type abstract class) refer to a subclass(non abstract) object.
Person person = new Student("XYZ",
"ABC");
And in order to construct a Student object, you need to have the "person" parts of the student initialized, thats what exactly the constructor of the abstract super class is called for.
What you can't do is to create an instance of an abstract class.
As a Student is 'partly' a Person super(...) initializes the 'Person part' of the student, it does not create a Person.
I hope you understand what I try to say
The constructor is just a method like others. And you are calling it explicitly from your child class' constructor with:
super(name);
The Person constructor is invoked from the Student class. From the Java tutorials
An abstract class is a class that is
declared abstract—it may or may not
include abstract methods. Abstract
classes cannot be instantiated, but
they can be subclassed.
An abstract class can have constructors - they cannot be invoked directly, but only as part of constructing a subclass instance, via a call to super() in the subclass constructor.
One cannot make an instance of an abstract class but subclasses can call super(name);. Even though it is a constructor, it is just another method.
The Abstract class is a part of your overall concrete class. An Abstract class is a class that is allowed to defer many parts to its concrete implementations but it must still initialize itself.
As such it has a constructor and as such the constructor with any parameters it requires to set itself up.
When you call the super(...) method from your Student class you are explicitly calling the constructor in the Person template class. The super() call must be in the first line of the Person constructor so if your person class wishes to override the defaults set up by the Person() constructor, then you have that option. But exactly one person constructor MUST be invoked when you extend a class (Abstract or concrete).
Related
I'm writing program that demonstrates the use of inheritance and I have created a variable using the super() keyword. I am now trying to place the value of that variable into a new method that calls it so that I can call that method in my main method to use its value within other classes.
Here is the relevant code:
Food class (super class)
public class Food {
//field that stores the name of the food
public String name;
//constructor that takes the name of the food as an argument
public Food(String name){
this.name = name;
}
public String getName() {
return name;
}
}
Meat class (sub class with super keyword)
public class Meat extends Food
{
public Meat() {
super("Meat");
}
public String getName() {
return //get super() value??;
}
}
Main class
public class Main {
public static void main(String[] args)
{
Wolf wolfExample = new Wolf();
Meat meatExample = new Meat();
System.out.println("************Wolf\"************");
System.out.println("Wolves eat " + meatExample.getName());
}
}
Any help is appreciated, thanks.
You could just do
public String getName() {
return super.getName();
}
Although you don't even need to override the method in the first place, because you declared the field name in super class to be public which means it can be accessed from anywhere.
Don't override public String getName() in Meat class.
The inheritance allows to inherit public and protected methods of Food in all subclasses of Food, therefore in Meat.
So Meat which IS a Food has by definition this behavior :
public String getName() {
return name;
}
which returns the name field stored in the parent class.
Overriding a method in subclass to write exactly the same code than in the parent method is useless and should not be done because it is misleading. A person which reads the code will wonder : why having overrided the method in the child class if it does the same thing than the parent class ?
Edit
Besides, if you want to access a field declared in a super class from a subclass, you should :
provide a public getter in the super class if the field is private. Here :
public String getName() {
return name;
}
use directly the field in the subclass if the field has the protected modifier.
As a general rule, you should avoid declaring instance fields with the modifier public because by default properties of a object should be protected and you should provide methods to modify the field only if needed.
So, declaring your Food class like that seems more suitable :
public class Food {
//field that stores the name of the food
private String name;
//constructor that takes the name of the food as an argument
public Food(String name){
this.name = name;
}
public String getName() {
return name;
}
}
In your Meat class, imagine you would like to add an additional information in the string returned by getName(), you could override it and why not using the field from the super class :
public class Meat extends Food {
public Meat() {
super("Meat");
}
#Override
public String getName() {
return super.getName + "(but don't abuse it)";
}
}
Here overriding the method is helpful because the behavior of the method in the child class differs from which one definedin the super class.
Simply write:
public String getName() {
return name;
}
This is because when searching for a variable named name, Java proceeds in this order:
Local variables (none)
Current class's fields (none)
Superclass's fields (found)
Super-super-class's fields (etc.)
However, you didn't need to override getName() in the subclass in the first place. If you didn't define it, then it would inherit the superclass's implementation, which corresponds exactly to the behavior you wanted. Thus you were doing extra work for no gain.
The other answers showed you how to do what you want.
But you should't do it (in real life projects)!
The most important principle in object oriented programming is encapsulation (aka information hiding). This means that the internal structure of a class should not be visible or accessible to the outside.
Therefore all member variables should be private.
Also you should avoid setter/getter methods since they just redirect the access. (except the class is a DTO without any logic of its own).
Since food class has the method getName declared as public do
public String getName() {
return super.getName();
}
public class Test {
public static void main(String args[]){
Student s = new Student();
s.printPerson();
}
}
class Person{
private String getInfo(){
return "Person";
}
public void printPerson(){
System.out.println(getInfo());
}
}
class Student extends Person{
private String getInfo(){
return "Student";
}
}
The output is Person. I am confused with this result. In the main method, the reference type is Student. So when executing s.printPerson(), it should executes the method of Student. The key point is, in the public method inherited from superclass, which private method is invoked? And why?
I thought in s.printPerson(), it invokes getInfo() of Student. But it turns out not. The IDE tells me
private String getInfo(){
return "Student";
}
is never used.
Can anyone help me with this please?
In Java, private methods are not inherited.
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
You cannot override getInfo by creating another getInfo method because it's not inherited. That's why your IDE gave you the "never used" warning, because nothing in Student uses it. The getInfo from Person is called, because it is available to Person's printPerson method, so Person is printed.
To override a method, it must not be private. You can make getInfo package-private (no access modifier), protected, or public. Then Student will be able to override it.
You use the private modifier. This modifier means that the method is only visible from within the class itself, not other classes, not even its sub- and superclasses. So when you call getInfo() from class Person, the only method visible is the method defined in the Person class. Effectively this means that private methods are not inherited in Java.
Try to change the private modifier to protected. Then the subclasses can see the method from the superclass, and so they can overide the method.
This is a piece of code that I have that I'm testing right now, and I noticed that when I change public to private on the getInfo() method in Person, both Student().printPerson() & Person().printPerson() print out "Person". However, when it's public, "Student" is displayed. Can someone explain this? Why is it that the private modifier disallows access to the getInfo method from Student?
new Student().printPerson();
new Person().printPerson();
}
}
class Student extends Person {
public Student(){
System.out.println("student invoked") ;
}
public String getInfo() {
return "Student";
}
}
class Person {
public Person(){
System.out.println("person invoked");
}
private String getInfo() {
return "Person";
}
public void printPerson() {
System.out.println(getInfo());
}
public (or protected) method in Java are always virtual.
This means that if a derived class overrides the method, calls made through the base class will call the derived class (if the object is actually an instance of the derived class).
When your base method is public, this happens, and calling it from printPerson() calls the overriding derived version.
When it's private, it is not virtual, so calls from the base class always call the base version (since it doesn't know about the derived class' method).
I read in scjp guide as following
In fact, you can't make a new object without invoking not just the
constructor of the object's actual class type, but also the
constructor of each of its superclasses!
For example
public class Person{
}
public class Employee extends Person{
public Employee(){
}
}
I don't create a Person instance but it is legal.
Please explain for me, thank for your help.
Whenever you instantiate a subclass, it'll call your superclass' constructor first.
You can find more about this here: JSL §8.8.7
Person.java
public class Person {
public Person() {
System.out.println("Super class constructor called");
}
}
Employee.java
public class Employee extends Person {
public Employee() {
System.out.println("Sub class constructor called");
}
}
If you then instantiate your Employee:
Employee e = new Employee();
Output:
Super class constructor called
Sub class constructor called
What they actually mean is
When you are creating a subclass object, i.e. its constructor is getting called, then superclass constructor gets callued internall
This is because for the default no-argument constructor there is a default super() call to the superclass constructor.
This goes on like the class hierarchy until the Object class.
In fact, if you do not write a no-argument constructor in superclass then the subclass declaration will throw compiler error.
public class Super {
public Super(int num){
}
}
public class Sub extends Super {
}
Here, class Sub will not compile giving the error Implicit super constructor Super() is undefined for default constructor because it cannot find a no-argument constructor in super class as the default no-argument constructor i.e. provided by compiler will have an implicit call to super().
Compiler provides a default no arg constructor only if there is no other constructor defined
As we have explicitly defined Super(int num), we will have to exlicitly create no-arg constructor as follows.
public Super(){
}
First, you don't have to create a parent instance (Parent) to instantiate a child class (Employee). You must have understood wrong.
Invoking the constructor of the parent class doesn't mean to create a new parent instance object (you're not calling it with new, so no new instance is created). You are creating a child instance, and for this, you need to first invoke the parent's constructor because of inheritance. Imagine for example the parent class has private fields that must be initialized in the constructor (for example private final fields). This fields cannot be accessed from the child class, but they can be initialized from the parent class constructor. You need to initialize this fields in the child instance, and the only way is calling super().
In this case Person has a default contructor which is invoked by default, no need to explicitly call it.
But in case Person has no default constructor, you need to call it explicitly. For example:
public class Person{
private final String name;
public Person(final String name) {
this.name = name;
}
}
public class Employee extends Person {
public Employee() {
}
}
This will not compile. You need to modify Employee so it calls Person constructor explicitly. For example:
public class Employee extends Person {
public Employee(final String name) {
super(name);
}
}
Superclass' nullary constructor is implicitly called.
Its because of constructor chaining:
First statement inside a any constructor by default is super();(This is a call to super class default constructor).
I don't create a Person instance but it is legal:
thats because you have a default constructor in Person class. So Employee class constructor can actually invoke super class constructor. Person()
Bottom line, the current class in which you are declaring a constructor, all the constructor upto the base class should be accessible via super(). If not, you have to explicitly make them accessible by explicitly making a call via super with proper parameters.
You have encountered one of the oddities of java.
If you don't define any constructors, the default, or "no args", constructor is implicitly defined. It's like invisible code.
If you define any constructor, args or no args, the implicit default constructor goes away.
To further compound the mystery, the first line of any subclass constructor must be to call a constructor of the super class. If you don't explicitly call one, the no-args constructor is implicitly called.
Good day!
I am reviewing the Java OO concept.
And wrote the following codes:
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.eat();
dog.sleep();
}
}
abstract public class Animal {
private int age;
public Animal (){
age = 1;
}
public Animal (int age){ //How can I call this constructor?
this.age = age;
}
public void eat(){
System.out.println("Eat");
}
abstract public void sleep();
}
abstract public class Canine extends Animal{
abstract public void roam();
}
public interface Pet {
public String petName = null; //i want the pets to have a variable petName.
public void trick();
}
public class Dog extends Canine implements Pet{
public void roam(){
System.out.println("roam");
};
public void sleep(){
System.out.println("sleep");
};
public void eat(){
System.out.println("Eat Dog");
};
public void trick(){
System.out.println("trick");
}
}
I have several questions as follows:
How can I call the Animal Overloaded constructor?
How can I use the variable petName in the PET Interface?
Am I doing the concept of OO correctly? What rules am I violating?
Thank you in advance.
Subclasses will call the super constructor from within their own constructor using super(...) as the first line!
Interfaces cannot have variables or state - only methods!
You have a sound concept, but your code would not compile (because of item 2 above).
Some solutions:
public interface Pet {
String getName();
void trick();
}
Now the Dog class (or any class that implements Pet) will have to implement Pet.getName(). Give the Dog class a field of type String called 'name' and return it from Dog.getName().
public abstract class Canine extends Animal {
public Canine(int age) {
super(age); // pass the age parameter up to Animal
}
...
}
public class Dog extends Canine implements Pet {
private final String name;
public Dog(String name,int age) {
super(age); // call the age constructor
this.name=name;
}
public String getName() { return name; }
... rest of class ...
}
Each subclass (esp. the abstract ones) will need to provide matching constructors for all parent class constructors you want to call! (So I added the age parameter to the Canine constructor so that Dog could pass an age argument to it.
check out this ,it might help you,
http://www.roseindia.net/java/beginners/constructoroverloading.shtml
http://www.jchq.net/certkey/0602certkey.htm
Calling either constructor is calling an overloaded constructor: both constructors use the same name. To call the constructor that takes an int, call dog = new Dog(1);.
The class Dog implements Pet, so it will have a public field petName (is it super.petName?).
I don't see any fundamental errors.
1) you can call argumented contructor as" this(20);"
Example of explicit this constructor call
public class Point {
int mx;
int my;
//============ Constructor
public Point(int x, int y) {
mx = x;
my = y;
}
//============ Parameterless default constructor
public Point() {
this(0, 0); // Calls other constructor.
}
. . .
}super(...) - The superclass (parent) constructor
An object has the fields of its own class plus all fields of its parent class, grandparent class, all the way up to the root class Object. It's necessary to initialize all fields, therefore all constructors must be called! The Java compiler automatically inserts the necessary constructor calls in the process of constructor chaining, or you can do it explicitly.
The Java compiler inserts a call to the parent constructor (super) if you don't have a constructor call as the first statement of you constructor. The following is the equivalent of the constuctor above.
//============ Constructor (same as in above example)
public Point(int x, int y) {
super(); // Automatically done if you don't call constructor here.
m_x = x;
m_y = y;
}Why you might want to call super explicitly
Normally, you won't need to call the constructor for your parent class because it's automatically generated, but there are two cases where this is necessary.
You want to call a parent constructor which has parameters (the automatically generated super constructor call has no parameters).
There is no parameterless parent constructor because only constructors with parameters are defined in the parent class.
A call to the parent constructor is super(); in your case it would be super(12); for example. Note that a call to a constructor (either this() or super() must be the first statement. Note that you can only go up one level, so to call the constructor in Animal, you'd have to code that into Canine and not Dog.
public Dog(int age)
{
super(age); //this will invoke Canine's overloaded constructor
}
//You must now provide the overloaded constructor in Canine which invokes
//the overloaded constructor in Animal
public Canine(int age)
{
super(age); //this will invoke Animal's overloaded constructor
}
You may access the value of Pet.petName as, well, Pet.petName. This is because Interfaces can only have methods (which are implicitly public) and constants (which are implicitly public static final). You could use this.petName but it's pointless (and potentially confusing). You cannot set the value of petName anywhere else, ever since it's final, even if you don't declare it to be.
However in this case, declaring the name as part of the interface doesn't make sense since it's not constant. It should be a part of some abstract or concrete class so that it can be set per instance of Pet. You should instead define a getter (and maybe a setter) for Pet -
public String getName();
public String setName(String name);
these will force implementing classes to provide some sort of implementation to get and set the name for a Pet without enforcing how that name is stored inside of that class.
How can I call the Animal Overloaded constructor?
You need to chain it.
abstract public class Canine extends Animal{
public Canine(int age) {
super(age);
}
// ...
}
and
public class Dog extends Canine implements Pet{
public Dog(int age) {
super(age);
}
//...
}
How can I use the variable petName in the PET Interface?
Pet.petName. If a class implements such an interface, then the class can refer to those constants without a qualifying class name like this: petName
Am I doing the concept of OO correctly? What rules am I violating?
Interface constants