+ 2
Is the default constructor remains common for the superclass and the subclass?
12 Respostas
+ 5
Whenever a new object of a subclass is created, the constructor of the superclass is automatically invoked.
+ 2
Hello Atul
Can you explain it a bit more what you mean? I am not completely sure if I unstand your question.
+ 2
Atul
Yes, you can see it here:
https://code.sololearn.com/c1jcBSbgF0cU/?ref=app
What you can't see but what also happens, all classes call instructor of Object class. This class is the super class of all classes.
If you want that a inherited class calls a specific constructor of your super class you need to use super().
For example Mom has two constructors:
Mom() and Mom(String s) and your Kid should call Mom(String s):
Kid(String s){
super(s);
}
And if I remember it correctly you need to call super() if your super class don't have a default constructor.
+ 2
Atul No. As long as the access modifiers are protected or public, you can access all the methods.
+ 2
Atul
If you mean constructor it calls only the one which is called in the constructor of your inherited class.
Here is another example:
https://code.sololearn.com/cQWfMMCnE26G/?ref=app
constructor of Kid -> constructor of Mom (-> constructor of Object)
In the end it means Kid is an instance of Mom and an instance of Object.
You can test it with the keyword instanceof
About the methods:
The class needs to have access and than you can call them.
+ 2
Atul remeber that a sub class cannot be created without its parent being created first so when you do
SubClass s = new SubClass();
In SubClass constructor Parent constructor is called first and then subclass is then created inheriting all none private stuff.
+ 1
Why" animal forms the ecosystem"line is output 2 times. I want to ask when we call a inherited class then the parent class having a default constructor is also executed?
+ 1
https://code.sololearn.com/cCBtciVogxbc/?ref=app
Can you please tell me what contrast is there in this code and the earlier code posted by me?
+ 1
Denise Roßberg As it is inhereting so it will do all the functions of parent class . Correct me if I am Wrong
0
Yes that only. You see the code of Denise Roßberg All are public constructors
0
class Animal{
String eat;
String name;
Animal (){
System. out.println ("ANIMAL FORMS THE ECOSYSTEM");
}
Animal (String name){
this.name=name;
}
public void setName(String name){
this. name=name;
}
public String getName(){
return name;
}
public void setAnimal (String eat){
this. eat=eat;
}
public String getAnimal(){
return eat;
}
}
class Dog extends Animal{
int fourlimbs;
public void setDog(int fourlimbs){
this.fourlimbs=fourlimbs;
}
public int getDog(){
return fourlimbs;
}
}
public class Program
{
public static void main(String[] args) {
Animal sc=new Animal("dog");
System.out .println(sc.getName());
Animal s=new Animal("food");
System. out.println(s.getName ());
Dog ac=new Dog();
ac.setDog(4);
System.out.println (ac.getDog());
}
}
u need to add new object if u want to access it again with different value... hope this helps