-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcwh_48_method_overriding.java
53 lines (41 loc) · 1.28 KB
/
cwh_48_method_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
package company;
class A
{
public int harry()
{
return 4;
}
public void meth2()
{
System.out.println("This is method 2 of class A");
}
}
class B extends A
{
@Override // this annotates that the method below is overridden. And it is a valid Java code.
// It is not must to write this annotation, but for our own convenience.
public void meth2()
{
System.out.println("This is method 2 of class B");
}
public void meth3()
{
System.out.println("This is method 3 of class B");
}
}
public class cwh_48_method_overriding
{
public static void main(String[] args)
{
A a = new A();
a.meth2();
B b = new B();
b.meth2();
/* Method Overriding --> If a child class implements the same method present in the parent class again, it is known as Method Overriding.
* (redefining a method of super-class in sub-class)
* -When an object of subclass is created and the overridden method is called, the method which has been implemented in the sub-class is called & its code is executed.
* -The method that is to be overridden in the child class must have its implementation same as the method in the parent class, like their
* access modifiers, return data-type , arguments etc.
*/
}
}