-
Notifications
You must be signed in to change notification settings - Fork 391
/
overriding_java
62 lines (44 loc) · 1.14 KB
/
overriding_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//example of the overriding method
//return type and signature(parameter) same means the overriding method
class a1
{
void hell()
{
System.out.println("hell i am from a1");
}
}
class b1 extends a1
{
void hell()
{
System.out.println("hello i am from b1");
}
}
class c1 extends b1{
void hell()
{
System.out.println("hello i am from c1");
}
}
public class polymorph {
public static void main(String[] args)
{
//where c1 is called reference
// where c1() is called instance
//c1 obj1=new c1();
// obj1.hell();
// now we take the reference of b1 and instance c1 as same
// b1 obj1=new c1();
// obj1.hell();
//it's means that if we change the instance than automatically
//sentence will be changed or method /function will be change
//reference is a1 and instance c1
//output is of c1 method is display
// a1 obj1=new c1();
//obj1.hell();
//reference is a1 and instance b1
//output is of b1 method is display
// a1 obj1=new b1();
// obj1.hell();
}
}