0% found this document useful (0 votes)
1 views8 pages

Java

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views8 pages

Java

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

18-4-2026

Saturday, April 18, 2026 6:08 PM

Blocks in inheritance

package p1;

public class PClass {


public static int a;
static {
[Link]("=====PClass Static Block=====");
[Link]("The value of a:"+a);
}
}

package p1;

public class CClass extends PClass {


public static int b;
static
{
[Link]("====CClass Static block===");
[Link]("The value of b:"+b);
}
}

java batch-24 Page 1


package p1;

public class main1 {

public static void main(String[] args) {


CClass ob=new CClass();

=====PClass Static Block=====


The value of a:0
====CClass Static block===
The value of b:0

package p1;

public class PClass {


public static int a;
static {
[Link]("=====PClass Static Block=====");
[Link]("The value of a:"+a);
}
}

package p1;

public class CClass extends PClass {


public static int b;
static
{
[Link]("====CClass Static block===");
[Link]("The value of b:"+b);
}
}

package p1;

java batch-24 Page 2


public class main1 {

public static void main(String[] args) {


CClass.a=200;

=====PClass Static Block=====


The value of a:0

package p1;

public class PClass {


public static int a;
static {
[Link]("=====PClass Static Block=====");
[Link]("The value of a:"+a);
}
}

package p1;

public class CClass extends PClass {


public static int b;
static
{
[Link]("====CClass Static block===");
[Link]("The value of b:"+b);
}
}

package p1;

java batch-24 Page 3


public class main1 {

public static void main(String[] args) {


CClass.b=200;

=====PClass Static Block=====


The value of a:0
====CClass Static block===
The value of b:0

What is empty object reference?

Empty object reference means whenever we assign a value to a static


variable , inside a cclass then static block of pclass and static block of child
class will be executed with default value

package p1;

public class PClass {


public static int a;
static {
[Link]("=====PClass Static Block=====");
[Link]("The value of a:"+a);
}
{
[Link]("===PClass Instance Block====");
[Link]("The value of a:"+a);
}
}

package p1;

public class CClass extends PClass {


public static int b;
static
{

java batch-24 Page 4


{
[Link]("====CClass Static block===");
[Link]("The value of b:"+b);
}
{
[Link]("===CClass Instance block===");
[Link]("The value of b:"+b);
}
}

package p1;

public class main1 {

public static void main(String[] args) {


CClass ob=new CClass();

=====PClass Static Block=====


The value of a:0
====CClass Static block===
The value of b:0
===PClass Instance Block====
The value of a:0
===CClass Instance block===
The value of b:0

Static block in PClass will have highest priority in execution than


Static block in CClass

Instance block in PClass will have highest priority in execution than


instance block available in CClass

Method Overriding Process


====================
If we have a method with same method signature in PClass and CClass then
PClass method is replaced by CClass method is known method overriding
process
java batch-24 Page 5
process

method signature
===========
return type
method name
para_list
para_type

Method overriding is divided into

a)Instance method overriding process


b)Static method overriding process
c)Constructor Overriding Process

a)Instance method overriding process


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

package p1;

public class PClass {


public int calculate(int x) {
[Link]("===PClass method====");
return x*x;
}
}

package p1;

public class CClass extends PClass {


public int calculate(int x) {
[Link]("=====CClass method======");
return x*x*x;
}
}

package p1;

public class main1 {

public static void main(String[] args) {


CClass ob=new CClass();
java batch-24 Page 6
CClass ob=new CClass();
[Link]("Result:"+[Link](11));

=====CClass method======
Result:1331

b)Static method overriding process


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

package p1;

public class PClass {


public static void m(int x) {
[Link]("===PClass m()====");
[Link]("The value of x:"+x);
}
}

package p1;

public class CClass extends PClass {


public static void m(int x) {
[Link]("===CClass m()====");
[Link]("The value of x:"+x);
}
}

package p1;

public class main1 {

public static void main(String[] args) {


PClass.m(123);

java batch-24 Page 7


}

===PClass m()====
The value of x:123

package p1;

public class PClass {


public static void m(int x) {
[Link]("===PClass m()====");
[Link]("The value of x:"+x);
}
}

package p1;

public class CClass extends PClass {


public static void m(int x) {
[Link]("===CClass m()====");
[Link]("The value of x:"+x);
}
}

package p1;

public class main1 {

public static void main(String[] args) {


CClass.m(123);

===CClass m()====
The value of x:123

java batch-24 Page 8

You might also like