You are on page 1of 8

Dt : 8/6/2022

Execution Flow of above program:

ClassFiles:

PClass.class

CClass.class

DemoInheritance1.class(MainClass)

Note:

=>In Inheritance process ParentClass is loaded onto MethodArea first and

then CClass is loaded.

=>In Inheritance process while object creation ParentClass members will

get the memory first and then ChildClass members will get the memory.

================================================================
Case-2 : Static members from the ParentClass/SuperClass

=>Static members of ParentClass can be accessed by the ChildClass name,

because static members of ParentClass are available to ChildClass through

extends keyword.

Ex:

PClass.java

package test;
public class PClass{
public static int a=10;
public static void m1() {
System.out.println("===PClass m1()===");
System.out.println("The value a:"+a);
}
static
{
System.out.println("===PClass Static block===");
System.out.println("The value a:"+a);
}
}

CClass.java

package test;
public class CClass extends PClass{
public static int b=10;
public static void m2()
{
System.out.println("===CClass m2()===");
System.out.println("The value b:"+b);
}
static
{
System.out.println("===CClass Static block===");
System.out.println("The value b:"+b);
}
}
DemoInheritance2.java(MainClass)

package maccess;
import test.*;
public class DemoInheritance2 {
public static void main(String[] args) {
System.out.println("====Access with Class name====");
CClass.m1();
CClass.m2();
System.out.println("===Access with Object reference===");
CClass ob = new CClass();
ob.m1();
ob.m2();
}
}

o/p:

====Access with Class name====

===PClass Static block===

The value a:10

===PClass m1()===

The value a:10

===CClass Static block===

The value b:10

===CClass m2()===

The value b:10

Execution flow of above program:

ClassFiles:

PClass.class

CClass.class

DemoInheritance2.class(MainClass)
==================================================================

faq:

can we access Static members of class using Object reference?

=>Yes,we can access static members of class using Object reference

because the Object reference generated from the class.

faq:

define Empty Object reference?

=>'Empty Object reference' is created when we create object for the class

which is declared with only static members.

=====================================================================
*imp

Case-3 : Constructors of ParentClass/SuperClass

(i)0-parameter constructor from the ParentClass/SuperClass

=>when we have 0-parameter constructor in ParantClass then compiler at

compilation stage will add 'super()' to the ChildClass constructor and which

is ParentClass con_call.

Ex:

PClass.java

package test;
public class PClass {
public PClass()
{
System.out.println("===PClass con====");
}
}

CClass.java

package test;
public class CClass extends PClass{
public CClass()
{
super();
System.out.println("===CClass con()===");
}
}

DemoInheritance3.java(MainClass)

package maccess;
import test.*;
public class DemoInheritance3 {
public static void main(String[] args) {
CClass ob = new CClass();//CClass_Con_Call
}
}

o/p:

===PClass con====

===CClass con()===

(ii)Parameterized constructor from the ParentClass/SuperClass.

=>when we have parameterized constructor in ParentClass then we have to

add 'super()' to the ChildClass constructor and which PClass con_call

Ex:

PClass.java

package test;
public class PClass {
public PClass(int x)
{
System.out.println("====PClass con()====");
System.out.println("The value x:"+x);
}
}

CClass.java

package test;
public class CClass extends PClass{
public CClass(int p)
{
super(p);//PClass_Con_call
}
}

DemoInheritance4.java

package maccess;
import test.*;
public class DemoInheritance4 {
public static void main(String[] args) {
CClass ob = new CClass(123);//CClass_Con_Call
}
}

o/p:

====PClass con()====

The value x:123

=================================================================

Note:

=>'super()' is a Pre-defined and Built-in form to execute ParentClass

Constructors.

=================================================================

faq:

define Method Overriding process?

=>The method with same method Signature in PClass and CClass,then PClass

method is replaced by CClass method while Object creation,is known as

Method Overriding process.

=>Same method Signature means,

Same return_type

Same method_name

Same para_list

Same para_type

Case-1 : Instance method Overriding process


Case-2 : Static method Overriding process

Case-3 : Constructor Overriding process

You might also like