-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverrideRule1.java
More file actions
44 lines (38 loc) · 1.05 KB
/
Copy pathOverrideRule1.java
File metadata and controls
44 lines (38 loc) · 1.05 KB
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
//There are 4 rules that we must follow to override a method
/*Overriding Rule.1->
* We can not reduce the visibility of overridden method but we can increase the visibility of overridden method
*/
//Example 1->
class Demo1
{
public void disp()
{
System.out.println("Parent");
}
}
class Demo2 extends Demo1
{
// void disp() //Here we get the error because by default the access modifier is default
// { // and we know that the visibility of default is less then public hence we got the error coz this is not allowed
// System.out.println("Child");
// }
}
//Example 2->
class Demoo1
{
void disp()
{
System.out.println("Parent");
}
}
class Demoo2 extends Demoo1
{
public void disp() // here we will not get any error because the parent class disp() access modifier is default
{ // and this overridden method access modifier is public it means here we increase the visibility and this is allowed
System.out.println("Child");
}
}
public class OverrideRule1 {
public static void main(String[] args) {
}
}