![]() Super() // calling statement to super class constructorĮffect of private, default, protected and public keyword on inheritance in java: B() There is no statement in java to call subclass constructor We can call super class constructor explicitly through super() calling statement from sub class constructor and we can call other constructors of the same class through this() calling statement but, we can’t call sub class constructor from super class constructor.So, every class in java has properties inherited from Object class. By default, every class is a sub class of class.Super(10) //Explicitly Calling Class A constructor From that constructor call super class constructor explicitly. To avoid this error, write the constructor for sub class. Therefore you will get a compile time error. In that default constructor, first statement is super() – it is a calling statement to default constructor of Class A. So, compiler will be providing default constructor. Because, we are not defining constructor for Class B. Compiler will force you to write constructor in Class B. You will get a compile time error saying implicit default constructor A() is undefined for Class A. Static members of super class are inheriting to sub class as static members and non-static members are inheriting as non-static members only.While creating this object, SIB, IIB and constructor of Class A are also executed. In the MainClass, We are creating an object to Class B. ![]() In the above example, Class B is extending Class A. But they are executed while creating an object to sub class. Constructors, SIB – Static Initialization Block and IIB – Instance Initialization Block of super class will not be inheriting to its sub class.Here are some points regarding inheritance in java. Points-To-Remember About Inheritance In Java : ![]() Here, you can analyse the inheritance like this – we are extending class A by adding additional properties to it through Class B or We are reusing properties of Class A in Class B. Here, Class A is called super class and Class B is called sub class. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |