-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh10_47_MethodOverriding.java
More file actions
58 lines (48 loc) · 2.37 KB
/
Ch10_47_MethodOverriding.java
File metadata and controls
58 lines (48 loc) · 2.37 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class firstClass{
int intA;
public int firstMethod(){
return 45;
}
public void secondMethod(){
System.out.println("This is Second method of First Class !");
}
}
class secondClass extends firstClass{
@Override // Fun Fact: This is known as Annotation used to locate, changes done in code and it doesn't prod error.
// Note only works, if method is actually over ridden or else produces an error.
public void secondMethod(){ // Over ridden method
System.out.println("This is Second method of Second Class !");
}
public void thirdMethod(){
System.out.println("This is third method of Second Class !");
}
}
public class Ch10_47_MethodOverriding {
public static void main(String[] args) {
System.out.println();
firstClass object1 = new firstClass();
System.out.println("First Class starts here: ");
object1.secondMethod();
System.out.println(); // and
secondClass object2 = new secondClass();
System.out.println("Second class starts here: ");
object2.secondMethod();
/* Note: If here secondMethod() for secondClass was not created it would have used it from first because of
Inheritance. But here , due to method overriding it's implementation (msg printed) have changed. */
object2.thirdMethod();
/* What is Method Overriding?
Imagine we want to create a method with same name but with different implementation of work
in inherited class it can be done , and this is known as Method Overriding.
Ex. Here , creating another secondMethod() but it prints Second method of second class */
/* Difference between Method Overloading and Method Overriding?
Ans: In Method Overloading same method has different arguments in Same Class.
Whereas in , Method Overriding , method with same name and same arguments serves for different classes. */
// Note: If there are two overridden methods in inherited class the latest method will be executed upon calling.
/* Very Important note: For overriding to occur these must satisfy:
1. Works with inherited classes that is Types and SubTypes.
2. Arguments of methods must be same and obviously name.
3. Access Modifiers must be same.
4. Datatype must be same.
*/
}
}