+ 2

Is the default constructor remains common for the superclass and the subclass?

https://code.sololearn.com/cCBtciVogxbc/?ref=app

22nd Apr 2021, 1:15 PM
Atul [Inactive]
12 ответов
+ 5
Whenever a new object of a subclass is created, the constructor of the superclass is automatically invoked.
22nd Apr 2021, 2:28 PM
Soumik
Soumik - avatar
+ 2
Hello Atul Can you explain it a bit more what you mean? I am not completely sure if I unstand your question.
22nd Apr 2021, 2:00 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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.
22nd Apr 2021, 2:41 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Atul No. As long as the access modifiers are protected or public, you can access all the methods.
22nd Apr 2021, 2:53 PM
Soumik
Soumik - avatar
+ 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.
22nd Apr 2021, 3:13 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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.
22nd Apr 2021, 3:18 PM
D_Stark
D_Stark - avatar
+ 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?
22nd Apr 2021, 2:19 PM
Atul [Inactive]
+ 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?
22nd Apr 2021, 2:49 PM
Atul [Inactive]
+ 1
Denise Roßberg As it is inhereting so it will do all the functions of parent class . Correct me if I am Wrong
22nd Apr 2021, 2:50 PM
Atul [Inactive]
0
Yes that only. You see the code of Denise Roßberg All are public constructors
22nd Apr 2021, 2:56 PM
Atul [Inactive]
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
24th Apr 2021, 1:26 PM
kreddyt
kreddyt - avatar