0% found this document useful (0 votes)
5 views16 pages

Hi 5

The document provides examples of Java inheritance, demonstrating instance blocks, method overloading, and constructor overloading across multiple classes. It includes code snippets for PClass and CClass, illustrating how instance and static methods can be interlinked, as well as the different types of inheritance. Additionally, it explains the concept of method overloading and constructor chaining, along with sample outputs for each example.

Uploaded by

MONICA NAHAK
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)
5 views16 pages

Hi 5

The document provides examples of Java inheritance, demonstrating instance blocks, method overloading, and constructor overloading across multiple classes. It includes code snippets for PClass and CClass, illustrating how instance and static methods can be interlinked, as well as the different types of inheritance. Additionally, it explains the concept of method overloading and constructor chaining, along with sample outputs for each example.

Uploaded by

MONICA NAHAK
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
You are on page 1/ 16

Dt : 21/8/2023

Ex-Application:(Demonstrating Instance Blocks in PClass and CClass)

ProjectName : Inheritance_App8

packages,

ii
p1 : PClass.java

ath
package p1;
public class PClass
{
public int a;

aip
System.out.println("****PClass-Instance
Block****");
hM
System.out.println("The value a:"+a);
}
}
tes

p1 : CClass.java
package p1;
a

public class CClass extends PClass


{
nk

{
System.out.println("****CClass-Instance
Block***");
Ve

System.out.println("The value a:"+a);


}
}

p2 : DemoInheritance8.java(MainClass)

package p2;
import p1.*;

import java.util.*;

public class DemoInheritance8

public static void main(String[] args)

ii
{

ath
Scanner s = new Scanner(System.in);

CClass ob = new CClass();

aip
System.out.println("Enter the value for a:");

ob.a = s.nextInt();
hM
System.out.println("****main()****");

System.out.println("The value a:"+ob.a);

s.close();
tes

}
a
nk

o/p:

****PClass-Instance Block****
Ve

The value a:0

****CClass-Instance Block***

The value a:0

Enter the value for a:

120
****main()****

The value a:120

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

Note:

=>Instance blocks from PClass are executed first,then Instance blocks

ii
ath
from CClass are executed.

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

faq:

define Method Overloading process?

aip
=>More than one method with same method name but differentiated by their
hM
Para_list or Para_type is known as Method Overloading process.
tes

Note:

(i)return_type of methods are not included in Method Overloading process.


a

(ii)To perform Method Overloading process,Inheritance is not manditory


nk

which means we can perform Method Overloading using single class also.
Ve

(iii)we can perform Instance method Overloading,Static method Overloading

and Constructor Overloading.

Ex-1 : (Demonstrating Constructor Overloading process)


ProjectName : Inheritance_App9

packages,

p1 : PClass.java
package p1;
public class PClass {
public PClass(int a)
{

ii
System.out.println("****PClass(a)****");
System.out.println("a:"+a);

ath
}
public PClass(int a,int b)
{
this(a);

aip
System.out.println("****PClass(a,b)****");
System.out.println("a:"+a);
System.out.println("b:"+b);
hM
}
}

p1 : CClass.java
tes

package p1;
public class CClass extends PClass
{
a

public CClass(int a,int b,int c)


nk

{
super(a,b);
System.out.println("*****CClass(a,b,c)****");
Ve

System.out.println("a:"+a);
System.out.println("b:"+b);
System.out.println("c:"+c);

}
public CClass(int a,int b,int c,int d)
{
this(a,b,c);//3_para_Con_call
System.out.println("*****CClass(a,b,c,d)****");
System.out.println("a:"+a);
System.out.println("b:"+b);
System.out.println("c:"+c);
System.out.println("d:"+d);

}
}

p2 : DemoInheritance9.java(MainClass)

ii
ath
package p2;

import java.util.*;

import p1.*;

public class DemoInheritance9 {

aip
hM
public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.println("Enter the value a:");


tes

int a = s.nextInt();

System.out.println("Enter the value b:");


a

int b = s.nextInt();
nk

System.out.println("Enter the value c:");


Ve

int c = s.nextInt();

System.out.println("Enter the value d:");

int d = s.nextInt();

CClass ob = new CClass(a,b,c,d);//4_para_COn_call


s.close();

o/p:

ii
Enter the value a:

ath
11

Enter the value b:

aip
12

Enter the value c:


hM
13

Enter the value d:

14
tes

****PClass(a)****

a:11
a
nk

****PClass(a,b)****

a:11
Ve

b:12

*****CClass(a,b,c)****

a:11

b:12

c:13
*****CClass(a,b,c,d)****

a:11

b:12

c:13

d:14

ii
==================================================================

ath
Note:

=>while Object creation process we can call only one constructor for

aip
execution,but other Constructors can also be executed using Constructor

interlinking process or Constructor Chaining process.


hM
super() - Interlinking of PClass and CClass Constructors

this() - Interlinking of Constructors from the same class

===================================================================
tes

Ex-2 :(Demonstrating Instance method Overloading process)


a
nk

ProjectName : Inheritance_App10

packages,
Ve

p1 : PClass.java
package p1;
public class PClass {
public void m1(int a)
{
System.out.println("****PClass m1(a)****");
System.out.println("a:"+a);
}
public void m1(int a,int b)
{
this.m1(a);//1_para_method_call
System.out.println("****PClass m1(a,b)****");
System.out.println("a:"+a);
System.out.println("b:"+b);
}
}

ii
p1 : CClass.java

ath
package p1;
public class CClass extends PClass
{

aip
public void m2(int a,int b,int c)
{
super.m1(a, b);//2_para_method_call
System.out.println("*****CClass m2(a,b,c)****");
hM
System.out.println("a:"+a);
System.out.println("b:"+b);
System.out.println("c:"+c);

}
tes

public void m2(int a,int b,int c,int d)


{
this.m2(a, b, c);//3_para_method_call
a

System.out.println("*****CClass m2(a,b,c,d)****");
System.out.println("a:"+a);
nk

System.out.println("b:"+b);
System.out.println("c:"+c);
System.out.println("d:"+d);
Ve

}
}

p2 : DemoInheritance10.java(MainClass)

package p2;
import java.util.*;

import p1.*;

public class DemoInheritance10 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

ii
System.out.println("Enter the value a:");

ath
int a = s.nextInt();

System.out.println("Enter the value b:");

aip
int b = s.nextInt();

System.out.println("Enter the value c:");


hM
int c = s.nextInt();

System.out.println("Enter the value d:");

int d = s.nextInt();
tes

CClass ob = new CClass();


a
nk

ob.m2(a, b, c, d);//4_para_method_Call

s.close();
Ve

o/p:

Enter the value a:

11
Enter the value b:

12

Enter the value c:

13

Enter the value d:

ii
14

ath
****PClass m1(a)****

a:11

aip
****PClass m1(a,b)****

a:11
hM
b:12

*****CClass m2(a,b,c)****

a:11
tes

b:12

c:13
a
nk

*****CClass m2(a,b,c,d)****

a:11
Ve

b:12

c:13

d:14

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

Note:
=>we use "this" and "super" keywords to perform Instance methods

interlinking process or Instance methods Chaining process.

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

Ex-3:(Demonstrating Static method Overloading process)

ii
faq:

ath
Can we perform static method Interlinking process using "this" and "super"

keywords?

aip
=>No,we cannot Interlink static methods using "this" and "super" keywords

because "this" and "super" keywords are NonStatic NonPrimitive datatype


hM
variables.

(static methods cannot access NonStatic variables)


tes

faq:
a
nk

can we access static methods using "super" and "this" keywords?

=>yes,we can access static methods using "super" and "this" keywords,
Ve

but these keywords are declared within the Instance methods.

ProjectName : Inheritance_App11

packages,

p1 : PClass.java
package p1;
public class PClass {
public static void m1(int a)
{
System.out.println("****PClass m1(a)****");
System.out.println("a:"+a);
}
public static void m1(int a,int b)
{
System.out.println("****PClass m1(a,b)****");

ii
System.out.println("a:"+a);

ath
System.out.println("b:"+b);
}
}

aip
p1 : CClass.java
package p1;
hM
public class CClass extends PClass
{
public static void m2(int a,int b,int c)
{
System.out.println("*****CClass m2(a,b,c)****");
tes

System.out.println("a:"+a);
System.out.println("b:"+b);
System.out.println("c:"+c);
a

}
nk

public static void m2(int a,int b,int c,int d)


{
System.out.println("*****CClass m2(a,b,c,d)****");
Ve

System.out.println("a:"+a);
System.out.println("b:"+b);
System.out.println("c:"+c);
System.out.println("d:"+d);

}
public void access(int a,int b,int c,int d)
{
super.m1(a);
super.m1(a, b);
this.m2(a, b, c);
this.m2(a, b, c, d);
}
}

p2 : DemoInheritance11.java(MainClass)

ii
package p2;

ath
import java.util.*;

import p1.*;

aip
public class DemoInheritance11 {

public static void main(String[] args) {


hM
Scanner s = new Scanner(System.in);

System.out.println("Enter the value a:");

int a = s.nextInt();
tes

System.out.println("Enter the value b:");


a

int b = s.nextInt();
nk

System.out.println("Enter the value c:");

int c = s.nextInt();
Ve

System.out.println("Enter the value d:");

int d = s.nextInt();

CClass ob = new CClass();

ob.access(a, b, c, d);//Instance method_call


s.close();

o/p:

Enter the value a:

ii
10

ath
Enter the value b:

20

aip
Enter the value c:

30
hM
Enter the value d:

40

****PClass m1(a)****
tes

a:10

****PClass m1(a,b)****
a
nk

a:10

b:20
Ve

*****CClass m2(a,b,c)****

a:10

b:20

c:30

*****CClass m2(a,b,c,d)****
a:10

b:20

c:30

d:40

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

ii
ath
Types of Inheritances:

=>Inheritances are categorized into the following:

1.Single Inheritance

2.Multiple Inheritance

3.Multi-Level Inheritance
aip
hM
4.Hierarchal Inheritance

5.Hybrid Inheritance
tes

Diagrams:
a
nk
Ve
ii
ath
aip
hM
tes

=================================================================
a
nk
Ve

You might also like