0% found this document useful (0 votes)
7 views11 pages

Polymorphic Variables in OOP Explained

The document explains polymorphic variables, which can hold references to multiple object types, and includes examples of simple polymorphic variables and pseudo-variables. It also discusses reverse polymorphism, which is the process of undoing a polymorphic assignment. Additionally, it highlights potential problems related to identification and assignment in polymorphism.

Uploaded by

olowumm
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)
7 views11 pages

Polymorphic Variables in OOP Explained

The document explains polymorphic variables, which can hold references to multiple object types, and includes examples of simple polymorphic variables and pseudo-variables. It also discusses reverse polymorphism, which is the process of undoing a polymorphic assignment. Additionally, it highlights potential problems related to identification and assignment in polymorphism.

Uploaded by

olowumm
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

y

nl
O
se
U
tre
en
Session: 9 C
h
ec

Polymorphic Variable
pt
rA
Fo

Version 1.0 © 2012 Aptech Limited.


y
nl
 Explain polymorphic variable

O
 List types of polymorphic variables

se
Explain simple polymorphic variable

U

tre
 Explain pseudo-variable

en
 Explain reverse polymorphism
C
h
ec
pt
rA
Fo

Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 9 2


y
A variable that can hold

nl
O

reference
The figure of morevariable
shows a polymorphic than one
f1 of class

se
Father which is used to access both parent as well as
type of object

U
child class study() methods.

tre
en
C
h
ec
pt
rA
Fo

Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 9 3


y
nl
O
se
U
tre
en
C
h
ec
pt
rA
Fo

Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 9 4


y
 The C# code shows the creation of polymorphic

nl
variable.

O
se
Code Snippet

U
class Emp

tre
{

en
public virtual void display()
{
C
[Link](“Class Emp”);
h
ec

}
pt

}
rA

// class FullTimeEmp inherits class Emp


Fo

class FullTimeEmp : Emp

Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 9 5


y
nl
{

O
// overridden method

se
public override void display()

U
{

tre
[Link](“Class FullTimeEmp”);
}

en
}
C
// class PartTimeEmp inherits class Emp
h
class PartTimeEmp : Emp
ec

{
pt

// overridden method
rA

public override void display()


Fo

Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 9 6


y
nl
{

O
[Link](“Class PartTimeEmp”);

se
}

U
static void Main()

tre
{
Emp e1 = new FullTimeEmp(); // polymorphic

en
variable e1
[Link](); // line3
} C
h
ec

}// end of class


pt

Output:
rA

Class FullTimeEmp
Fo

Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 9 7


y
nl
O
Invisible receiver
is calledthat works inside of

se
The pseudo-variable

U
a method and is normally not

tre
declared
‘this’ inlike
C++, a
C#,normal
and Java variable

en
C
‘self’ in Small talk
h
Eliminates the need to give different
ec

names to the method


in Eiffel parameters from
pt

‘current’
rA

that of the actual parameters


Fo

Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 9 8


y
nl
 Reverse polymorphism is a process of undoing a

O
polymorphic assignment.

se
U
The C# code shows reverse polymorphism by assigning

tre

the object e1 again to a variable of type child class.

en
Code Snippet
C
h
ec

Emp e1 = new FullTimeEmp(); // assign child


pt

class reference to parent class


rA

FullTimeEmp e2 = (FullTimeEmp)e1; // cast the


parent object back to child type
Fo

Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 9 9


y
 Problems that can arise

nl
O
Problem of identification Problem of assignment

se
 The figure shows the identification and assignment

U
problem.

tre
en
C
h
ec
pt
rA
Fo

Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 9 10


y
nl
 A polymorphic variable is one that can hold reference

O
of more than one type of object.

se
 A pseudo-variable is an invisible receiver that works

U
inside of a method and is normally not declared like a

tre
normal variable.

en
 The pseudo-variable is called ‘this’ in C++, C# and Java,
C
‘self’ in Small talk, and ‘current’ in Eiffel.
h
The pseudo-variable eliminates the need to give
ec

different names to the method parameters from that


pt

of the actual parameters to resolve name ambiguity.


rA

Reverse polymorphism is a process of undoing a


Fo

polymorphic assignment.
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 9 11

You might also like