Method Overriding in Java

By: Henry Viewed: 1854 times  Printer Friendly Format    


In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden. Consider the following: // Method overriding.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}

class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
System.out.println("k: " + k);
}
}

class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}

The output produced by this program is shown here:

k: 3

When show( ) is invoked on an object of type B, the version of show( ) defined within B is used. That is, the versio n of show( ) inside B overrides the version declared in A. If you wish to access the superclass version of an overridden function, you can do so by using super. For example, in this version of B, the superclass version of show( ) is invoked within the subclass' version. This allows all instance variables to be displayed.

class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
super.show(); // this calls A's show()
System.out.println("k: " + k);
}
}

If you substitute this version of A into the previous program, you will see the following output:

i and j: 1 2
k: 3

Here, super.show( ) calls the superclass version of show( ). Method overriding occurs only when the names and the type signatures of the two methods are identical. If they are not, then the two methods are simply overloaded. For example, consider this modified version of the preceding example:

// Methods with differing type signatures are overloaded – not
// overridden.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}

// Create a subclass by extending class A.
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// overload show()
void show(String msg) {
System.out.println(msg + k);
}
}

class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show("This is k: "); // this calls show() in B
subOb.show(); // this calls show() in A
}
}

The output produced by this program is shown here:

This is k: 3
i and j: 1 2

The version of show( ) in B takes a string parameter. This makes its type signature different from the one in A, which takes no parameters. Therefore, no overriding (or name hiding) takes place.



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Good example to know the concept better.
View Tutorial          By: ARAVIND L at 2009-01-05 04:07:46

2. The Given Programms are not suitable for "How
View Tutorial          By: Rams at 2009-01-27 01:36:16

3. nice examlpe but u can make it more easy frnd
View Tutorial          By: meghendra at 2009-02-14 00:21:31

4. Indent your code, man.
View Tutorial          By: Jenga at 2009-02-16 17:30:07

5. The example shown and ur way of explaining is good
View Tutorial          By: Devi at 2009-02-24 07:19:26

6. nice examlpe but u can make it more easy so that t
View Tutorial          By: lovely at 2009-02-25 07:08:18

7. its a good example
View Tutorial          By: Sonal at 2009-03-25 08:35:10

8. Dude, u xplaining lyk u already telling pros!Pls t
View Tutorial          By: Brianjambo at 2009-04-14 06:32:13

9. Nice for java examples, it is easy to understand j
View Tutorial          By: Nirmaladevi.T at 2009-06-19 04:42:51

10. yaar this example is easy to understand but other
View Tutorial          By: richa at 2009-09-08 13:52:37

11. this is the best example of method overriding but
View Tutorial          By: Dipesh at 2009-09-09 12:52:21

12. I HAVE ONE DOUBT.

public class Anim

View Tutorial          By: georgy at 2009-10-22 02:46:10

13. I HAVE ONE DOUBT.

public class Anim

View Tutorial          By: georgy at 2009-10-22 03:13:01

14. Very good post.^^
View Tutorial          By: LAM at 2009-10-25 04:18:38

15. thats good. but not for the begineers. because in
View Tutorial          By: jaywant topno at 2009-10-31 10:05:43

16. Very Good Example Dude!

Note for re

View Tutorial          By: Erwin at 2009-12-06 20:20:24

17. This is not an easy example for begineer, please t
View Tutorial          By: Arif Islam Nayan at 2009-12-16 08:24:57

18. Its an excellent example to learn this concept!!
View Tutorial          By: Koustav Routh at 2009-12-18 11:19:03

19. Its Really Easy to understand............. superb
View Tutorial          By: Regina at 2010-01-06 23:38:11

20. Not bad,but need some implementation
View Tutorial          By: surjeet katwal at 2010-01-19 04:41:15

21. please give very easy program....this program is d
View Tutorial          By: rajesh at 2010-01-24 08:08:38

22. It is very good example to know the concept better
View Tutorial          By: abdulkadir at 2010-01-27 00:56:07

23. thanks a lot dude,my lecturer took 2 weeks to expl
View Tutorial          By: praveen malinga at 2010-02-25 07:53:01

24. Its self-explaining and fullfilling example.
View Tutorial          By: vijay at 2010-03-29 02:36:41

25. Easy and Wonderfulimple Example...to undersatnd th
View Tutorial          By: Chaitanya at 2010-04-04 02:27:04

26. Hy gr8 man..!!! It was easy understanding
View Tutorial          By: ahwe at 2010-04-07 02:50:40

27. This is totally great. Your explaination make me u
View Tutorial          By: Cipom at 2010-04-11 18:41:04

28. not good example to learn the overriding methods
View Tutorial          By: george at 2010-04-25 12:08:20

29. programs are making the concept easy.its a nice pr
View Tutorial          By: veeru at 2010-05-12 22:10:25

30. really...........its very good explanation . sort
View Tutorial          By: gunjan shah at 2010-05-28 22:28:43

31. not that much clear exampl.. you should explain it
View Tutorial          By: elangovan at 2010-06-09 02:36:19

32. explaination is very good ,(lucid and succinct app
View Tutorial          By: shailendra singh at 2010-06-08 11:24:29

33. very good example!! this example make me feel bett
View Tutorial          By: Karthik at 2010-06-09 06:07:07

34. very good example...........
View Tutorial          By: debojit dey at 2010-07-20 15:48:38

35. good example..easy to understand
View Tutorial          By: aleena at 2010-07-24 21:56:01

36. im not clear in this concept coz im a beginner.. s
View Tutorial          By: vani at 2010-07-30 01:18:18

37. example is good but in more deatin & advanteg
View Tutorial          By: smita at 2010-08-08 06:09:09

38. example is good but in more deatin & advanteg
View Tutorial          By: smita at 2010-08-08 06:10:25

39. examples are very use full
View Tutorial          By: keerthe at 2010-08-12 23:00:21

40. ..i have a question. is it possible to override a
View Tutorial          By: rein at 2010-08-25 07:07:15

41. Could not have been easier than this! It's only my
View Tutorial          By: Sohail at 2010-08-29 11:09:52

42. Gooooooooooodd !!!
View Tutorial          By: Digvijoyee at 2010-08-30 22:11:31

43. very nice example.......
i got the concept

View Tutorial          By: sagar at 2010-09-01 01:11:15

44. Not bad,but need some implementation
View Tutorial          By: ravinder kaur at 2010-09-11 10:58:32

45. i need method overloading with io package
View Tutorial          By: ravinder kaur at 2010-09-11 11:00:57

46. In first ex. Show() method is differ by no. of par
View Tutorial          By: ashok at 2010-09-13 03:15:43

47. If i run this program----



View Tutorial          By: pratham at 2010-09-22 10:10:11

48. very nice but i want some more exapmles
View Tutorial          By: prasad lengare at 2010-10-03 23:12:47

49. very nice but i want some more exapmles
View Tutorial          By: prasad lengare at 2010-10-03 23:13:24

50. @pratham: save dis program as Override.java.

View Tutorial          By: saahil batra at 2010-10-04 03:51:38

51. yes, that's a very explicit example of how to over
View Tutorial          By: johnny at 2010-10-21 01:20:58

52. Your style is very nice , even a beginner could un
View Tutorial          By: Jonathan O. at 2010-11-08 07:49:02

53. ya its good example
View Tutorial          By: vijaychellappa at 2010-11-10 04:57:42

54. ya its good example
View Tutorial          By: vijaychellappa at 2010-11-10 04:58:32

55. Really its awesome.
View Tutorial          By: Sathish at 2010-12-01 23:20:03

56. its realy good example,understand the concept..
View Tutorial          By: VEnkatachala T V at 2010-12-15 01:28:47

57. its realy good example,understand the concept..
View Tutorial          By: VEnkatachala T V at 2010-12-15 01:30:14

58. It encourage the learners

Learners

View Tutorial          By: sureshkumar p at 2010-12-22 04:13:04

59. good explaination ... very helpful
View Tutorial          By: rohit sharma at 2010-12-27 03:43:58

60. can u pls tell me 4m which book u got this? My te
View Tutorial          By: Fahim Ahmed at 2011-01-09 11:31:39

61. This is awesum !!!
easy to understand ....

View Tutorial          By: Sud at 2011-01-19 08:37:55

62. Hi....this is really a nice and simple example....
View Tutorial          By: mayuri at 2011-01-21 01:05:18

63. It is easy n clear .Useful 4 begginers like me....
View Tutorial          By: RAMESH REDDY at 2011-01-30 00:19:21

64. It is easy n clear .Useful 4 begginers like me....
View Tutorial          By: RAMESH REDDY at 2011-01-30 00:20:47

65. these above examples of overloading and overriding
View Tutorial          By: janmejay pant at 2011-03-02 22:06:16

66. the exampe was really useful........
View Tutorial          By: smitha at 2011-03-10 21:27:55

67. @readers
this is an example of Hiding and N

View Tutorial          By: DevJ at 2011-03-18 05:41:52

68. Hi
Its nice example ..
som

View Tutorial          By: munirasu at 2011-04-01 06:36:38

69. Hi
Its nice example ..
som

View Tutorial          By: munirasu at 2011-04-01 06:38:46

70. nice example . and good job.
View Tutorial          By: mohit sinha at 2011-04-07 03:19:01

71. /*Overridding & equals:--> Overriding is an
View Tutorial          By: Naveen Kumar at 2011-04-30 18:44:22

72. /*Overridding & equals:--> Overriding is an
View Tutorial          By: Naveen Kumar at 2011-04-30 18:44:55

73. Good example thank you so much
View Tutorial          By: Albadri at 2011-05-14 15:48:17

74. super example...it very useful for me...
View Tutorial          By: elan at 2011-06-02 07:54:39

75. this is not good example we want better...
View Tutorial          By: sabari nathan.p at 2011-06-15 03:27:18

76. gr8t man.....! i am satisfied with ur explanation
View Tutorial          By: raja deepa at 2011-07-05 13:44:21

77. using a same name ,return type and parameter type
View Tutorial          By: sam at 2011-06-24 15:13:34

78. need to be explained more
View Tutorial          By: parul at 2011-06-30 08:34:29

79. sooo nice...:****...love u...
View Tutorial          By: vinoo gopal rao at 2011-07-03 08:54:29

80. it is a good example to understand.
View Tutorial          By: nivedha at 2011-07-12 05:15:39

81. nice example..
View Tutorial          By: righana at 2011-07-19 08:46:50

82. the above given cood is superb Example ..........<
View Tutorial          By: don_naveen at 2011-08-01 07:54:02

83. Its Very Good Example
View Tutorial          By: Madhu at 2011-08-26 12:11:49

84. IT'S NOT GOOD PROGRAM FOR BEGGNER'S SO THAT WRITE
View Tutorial          By: ANIL KUMAR at 2011-09-19 11:12:07

85. good example
View Tutorial          By: Swapna at 2011-09-26 07:42:35

86. very bad example.......can v call overrided functi
View Tutorial          By: abdul nasir khayam at 2011-10-27 20:57:00

87. good keep it up
View Tutorial          By: bharath at 2011-11-14 17:44:20

88. 2nd example not gud
View Tutorial          By: prakash at 2011-11-15 11:07:43

89. Thanx................!!!!!!!!!!!
View Tutorial          By: ankita at 2012-01-21 19:16:54

90. thanks....good explanation...
View Tutorial          By: jiby at 2012-01-14 05:57:33

91. which method is called if i call overriden method
View Tutorial          By: sarah at 2012-02-01 13:36:10

92. class Abc
{
public void display()
View Tutorial          By: Umapathi REddy at 2012-05-23 09:54:29

93. you have extended class Abc within class Abc

View Tutorial          By: SUNIL SHAH at 2012-05-25 06:17:23

94. everything is easy to learn from your website.I un
View Tutorial          By: sumitra barua at 2012-05-30 03:13:10

95. first eg. was vry gud ........& vry helpfully
View Tutorial          By: Mohit at 2012-07-07 12:42:01

96. nice example you have given
i need know one

View Tutorial          By: sundar at 2012-08-17 13:21:32

97. If this was meant for someone new to Java it was a
View Tutorial          By: tricky Dick at 2012-08-24 04:58:54

98. Programs are so good..!
i wish to say one t

View Tutorial          By: Shashikumar Misal at 2012-09-01 20:35:47

99. This is just perfect. Thanks :)
View Tutorial          By: NISHIT GURURANI at 2012-09-07 11:27:29

100. Realy helpfull in understanding this concept..

View Tutorial          By: riteeka at 2012-09-18 10:32:36

101. Thank you very much for such a nice topic
View Tutorial          By: CHANDAN at 2012-11-01 04:40:02

102. finally an understandable example
View Tutorial          By: sdf at 2012-11-26 21:44:34

103. its simple and easy... good example.
View Tutorial          By: madhu kk at 2012-12-05 07:56:04

104. Hats off man !!! Very Simple & Straightforward
View Tutorial          By: Nitesh at 2013-01-06 00:50:53

105. simply super...every one can understand
View Tutorial          By: prashanth at 2013-03-10 12:20:25

106. I want to use concept of overriding and i want to
View Tutorial          By: Aniket at 2013-06-20 10:23:10

107. You can get more details about methodoverloading a
View Tutorial          By: anuroop at 2013-07-31 15:51:31

108. very good exmpl and explantion
View Tutorial          By: harshita negi at 2013-10-16 02:19:35

109. what are benefits of using overriding method?
View Tutorial          By: Ah Socheat at 2013-12-11 04:44:57

110. U did'nt explain super(a,B)
View Tutorial          By: Rafiqullah at 2013-12-16 10:30:41

111. Thanks you very much such a nice blog. If someone
View Tutorial          By: Maddy at 2014-01-09 09:48:32

112. frnds i need overriding concept in bubble sort
View Tutorial          By: rabbu at 2014-08-21 12:46:52

113. Dude,
Atleast indent and try to make sense

View Tutorial          By: Phani at 2015-03-19 16:51:57

114. very simple example.....................
View Tutorial          By: kajal at 2015-08-22 09:28:16

115. super in explanation
View Tutorial          By: janakiraman at 2016-09-07 09:33:52

116. esosahatiline
View Tutorial          By: esosahatiline at 2017-01-12 10:37:36

117. ezunadkehono
View Tutorial          By: ezunadkehono at 2017-01-12 10:38:47

118. uchomoc
View Tutorial          By: uchomoc at 2017-01-12 10:55:23

119. uabexokuj
View Tutorial          By: uabexokuj at 2017-01-12 11:26:35

120. ilqoneyul
View Tutorial          By: ilqoneyul at 2017-01-12 12:48:46

121. odahaliqad
View Tutorial          By: odahaliqad at 2017-01-25 00:14:30

122. eejiloo
View Tutorial          By: eejiloo at 2017-01-25 00:31:34

123. eqelipi
View Tutorial          By: eqelipi at 2017-01-25 00:37:15

124. Thomasmi
View Tutorial          By: Thomasmi at 2017-01-28 01:02:25

125. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-02-22 09:01:36

126. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-02-23 17:05:25

127. Thomasmi
View Tutorial          By: Thomasmi at 2017-02-24 07:14:32

128. RichardCemo
View Tutorial          By: RichardCemo at 2017-02-25 02:07:23

129. RichardCemo
View Tutorial          By: RichardCemo at 2017-02-28 06:57:55

130. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-13 12:00:53

131. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-17 06:14:40

132. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-19 02:23:07

133. RichardCemo
View Tutorial          By: RichardCemo at 2017-03-19 02:43:37

134. XRumerTest
View Tutorial          By: XRumerTest at 2017-03-21 14:40:05

135. Thomason
View Tutorial          By: Thomason at 2017-03-28 02:16:31

136. XRumerTest
View Tutorial          By: XRumerTest at 2017-03-31 11:40:33

137. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-04-07 20:22:09

138. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-04-22 16:14:19

139. Donaldwax
View Tutorial          By: Donaldwax at 2017-05-06 04:18:23

140. RichardCemo
View Tutorial          By: RichardCemo at 2017-05-12 11:34:36

141. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-05-15 07:54:34

142. RichardCemo
View Tutorial          By: RichardCemo at 2017-05-18 17:30:55

143. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-05-30 14:05:28

144. Eugenehob
View Tutorial          By: Eugenehob at 2017-05-30 22:04:58

145. Scottjeace
View Tutorial          By: Scottjeace at 2017-06-05 14:54:12

146. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-06-08 08:07:15

147. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-06-10 00:06:13

148. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-06-11 05:03:23

149. Thomasmi
View Tutorial          By: Thomasmi at 2017-06-30 21:18:40

150. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-07-01 04:48:45

151. efilugopos
View Tutorial          By: efilugopos at 2017-07-04 20:10:40

152. upozisbovo
View Tutorial          By: upozisbovo at 2017-07-04 20:23:50

153. RichardCemo
View Tutorial          By: RichardCemo at 2017-07-17 13:12:43

154. MerlinMub
View Tutorial          By: MerlinMub at 2017-07-25 10:08:13

155. MerlinMub
View Tutorial          By: MerlinMub at 2017-07-29 22:22:05

156. asaxixikgobo
View Tutorial          By: asaxixikgobo at 2017-09-02 08:37:30

157. ecupcot
View Tutorial          By: ecupcot at 2017-09-02 08:39:13

158. ubutepini
View Tutorial          By: ubutepini at 2017-09-02 09:01:12

159. DonaldMit
View Tutorial          By: DonaldMit at 2017-09-04 11:41:47

160. ayatovoyo
View Tutorial          By: ayatovoyo at 2017-09-12 02:48:53