Method Overriding in Java

By: Henry  

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.




Archived Comments

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

48. Dude,
Atleast indent and try to make sense.
Tutorials doesn't mean you teach the pros

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

49. frnds i need overriding concept in bubble sort
plzzzzzzzzzzzzzz........send to my email id

View Tutorial          By: rabbu at 2014-08-21 12:46:52

50. Thanks you very much such a nice blog. If someone interested in rule of methods overriding in Java <
View Tutorial          By: Maddy at 2014-01-09 09:48:32

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

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

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

54. You can get more details about methodoverloading at http://letusprogram.wordpress.com/2013/07/30/met
View Tutorial          By: anuroop at 2013-07-31 15:51:31

55. I want to use concept of overriding and i want to print both function from base and derived class
View Tutorial          By: Aniket at 2013-06-20 10:23:10

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

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

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

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

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

61. Realy helpfull in understanding this concept..
thank u :)

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

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

63. Programs are so good..!
i wish to say one thing
when we invoke the show(); method

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

64. If this was meant for someone new to Java it was a terrible example and from some of the posts it wa
View Tutorial          By: tricky Dick at 2012-08-24 04:58:54

65. nice example you have given
i need know onething. is it super keyword method have same number

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

66. first eg. was vry gud ........& vry helpfully 4 me.........
thanx

View Tutorial          By: Mohit at 2012-07-07 12:42:01

67. everything is easy to learn from your website.I understand everything easily.I want learn about thre
View Tutorial          By: sumitra barua at 2012-05-30 03:13:10

68. you have extended class Abc within class Abc
the correct code is
class Abc
{
View Tutorial          By: SUNIL SHAH at 2012-05-25 06:17:23

69. class Abc
{
public void display()
{
System.out.println("hai"

View Tutorial          By: Umapathi REddy at 2012-05-23 09:54:29

70. which method is called if i call overriden method with superclass object?
View Tutorial          By: sarah at 2012-02-01 13:36:10

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

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

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

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

75. very bad example.......can v call overrided function of parent class method with child class objec
View Tutorial          By: abdul nasir khayam at 2011-10-27 20:57:00

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

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

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

79. the above given cood is superb Example ..........

But it could be Explained in better

View Tutorial          By: don_naveen at 2011-08-01 07:54:02

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

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

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

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

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

85. using a same name ,return type and parameter type for a method in a derived class as that of a metho
View Tutorial          By: sam at 2011-06-24 15:13:34

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

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

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

89. /*Overridding & equals:--> Overriding is another useful feature of object-oriented programmin
View Tutorial          By: Naveen Kumar at 2011-04-30 18:44:55

90. /*Overridding & equals:--> Overriding is another useful feature of object-oriented programmin
View Tutorial          By: Naveen Kumar at 2011-04-30 18:44:22

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

92. Hi
Its nice example ..
something should be able to understood like this, the

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

93. Hi
Its nice example ..
something should be able to understood like this, the

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

94. @readers
this is an example of Hiding and Not Overriding. As overriding doesnt apply to stati

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

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

96. these above examples of overloading and overriding are very helpful to understand the concept of the
View Tutorial          By: janmejay pant at 2011-03-02 22:06:16

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

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

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

100. This is awesum !!!
easy to understand .... clearl n crisp explanations ....
dere cud b

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

101. can u pls tell me 4m which book u got this? My teacher gave me a lecture sheet on overriding exactl
View Tutorial          By: Fahim Ahmed at 2011-01-09 11:31:39

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

103. It encourage the learners

Learners expect this kind of examples to understand the co

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

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

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

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

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

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

109. Your style is very nice , even a beginner could understand , but I believe you should put more sourc
View Tutorial          By: Jonathan O. at 2010-11-08 07:49:02

110. yes, that's a very explicit example of how to overload a function. Thanks...I mean, I know this stuf
View Tutorial          By: johnny at 2010-10-21 01:20:58

111. @pratham: save dis program as Override.java.
O as capital.

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

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

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

114. If i run this program----


// Methods with differing type signatures are o

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

115. In first ex. Show() method is differ by no. of parameters in sub class and super class.how it is pos
View Tutorial          By: ashok at 2010-09-13 03:15:43

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

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

118. very nice example.......
i got the concept of method overriding!!!

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

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

120. Could not have been easier than this! It's only my second day learning Java and your example makes t
View Tutorial          By: Sohail at 2010-08-29 11:09:52

121. ..i have a question. is it possible to override a method when it is already overloaded?? or its vice
View Tutorial          By: rein at 2010-08-25 07:07:15

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

123. example is good but in more deatin & advantege and disadvantege is also impotans & whith ex
View Tutorial          By: smita at 2010-08-08 06:10:25

124. example is good but in more deatin & advantege and disadvantege is also impotans & whith ex
View Tutorial          By: smita at 2010-08-08 06:09:09

125. im not clear in this concept coz im a beginner.. so u must try to make it easyyyyyyy way.. your exa
View Tutorial          By: vani at 2010-07-30 01:18:18

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

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

128. very good example!! this example make me feel better about java
View Tutorial          By: Karthik at 2010-06-09 06:07:07

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

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

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

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

133. not good example to learn the overriding methods
give at least two example of every concept

View Tutorial          By: george at 2010-04-25 12:08:20

134. This is totally great. Your explaination make me understand more easier
View Tutorial          By: Cipom at 2010-04-11 18:41:04

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

136. Easy and Wonderfulimple Example...to undersatnd the concept of Overriding for the Beginners !
View Tutorial          By: Chaitanya at 2010-04-04 02:27:04

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

138. thanks a lot dude,my lecturer took 2 weeks to explain this but only 15 min of reading this i get it
View Tutorial          By: praveen malinga at 2010-02-25 07:53:01

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

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

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

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

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

144. This is not an easy example for begineer, please try to make it easy.
View Tutorial          By: Arif Islam Nayan at 2009-12-16 08:24:57

145. Very Good Example Dude!

Note for readers, This is only a quick reference for remindin

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

146. thats good. but not for the begineers. because in this example you have used super class also. and t
View Tutorial          By: jaywant topno at 2009-10-31 10:05:43

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

148. I HAVE ONE DOUBT.

public class Animal {

public static void testCla

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

149. I HAVE ONE DOUBT.

public class Animal {

public static void testCla

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

150. this is the best example of method overriding but it is not a complete solution...........
View Tutorial          By: Dipesh at 2009-09-09 12:52:21

151. yaar this example is easy to understand but other examples of inheritence are difficult to understan
View Tutorial          By: richa at 2009-09-08 13:52:37

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

153. Dude, u xplaining lyk u already telling pros!Pls try a simpler explanation for al 2undastand.
View Tutorial          By: Brianjambo at 2009-04-14 06:32:13

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

155. nice examlpe but u can make it more easy so that the concept can be easily understand......
View Tutorial          By: lovely at 2009-02-25 07:08:18

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

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

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

159. The Given Programms are not suitable for "How to solve Method Overriding " .Coz in 2nd mod
View Tutorial          By: Rams at 2009-01-27 01:36:16

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


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial