-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynmic_method_dispatch.java
48 lines (42 loc) · 1.09 KB
/
dynmic_method_dispatch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Java program to illustrate the fact that
// runtime polymorphism cannot be achieved
// by data members
// class A
class A1
{
int x = 10;
int y= 15;
void display(){
System.out.println("A1");
}
void display1(){
System.out.println("accessing method of base class by created object is allowed but u when there is same variable in both parent and child class then parent class variable is accessible we cannot access variable of child i.e base class.");
}
}
// class B
class B1 extends A1
{
int x = 20;
int z = 25;
void display(){
System.out.println("B1");
}
void display2(){
System.out.println("not allowed by object a");
}
}
// Driver class
public class dynmic_method_dispatch
{
public static void main(String[] args)
{
A1 a = new B1(); // object of type B
// Data member of class A will be accessed
System.out.println(a.x);
System.out.println(a.y);
//System.out.println(a.z);// not allowed
a.display();
a.display1();
//a.display2();-------> not allowed
}
}