what is the need of method overriding ? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 1

what is the need of method overriding ?

if u have used method overriding in ur project then why ?

12th Jul 2017, 2:14 PM
Somnath Ghosh
Somnath Ghosh - avatar
3 Respuestas
+ 8
You need a real example or just want to understand the concept? If the latter, I always give example of an RPG game. Think of a class named NPC (non-player character). It will be a blueprint of all characters your main player will meet along his quest. NPC has many attributes, of which for this example we will need only a few: strength, sex and two methods: talk() and cast() - which display a text followed by a string " said {name}" and " casted {spell_name}", respectively. Now, if you think of a "warhammer-like" environment, you may have another NPC parameter, which is Race. However, let's assume that races are so different from each other that you decide to make them subclasses of the NPC class. Things start looking clear now, I hope. A Dwarf class will have derived parameters from NPC, like strength and sex, but his method talk() will be overridden by a string " yelled {name}". A Barbarian will have the cast() method changed to " can't do {spell_name} magick!" I hope you get it :) Overriding gets interesting if you generally have a lot in common between a class and its subclasses, but there are some particular differences.
12th Jul 2017, 3:47 PM
Kuba SiekierzyŇĄski
Kuba SiekierzyŇĄski - avatar
+ 2
here is an example that you may want to check to see difference or understand the meanings of overriding and overloading https://code.sololearn.com/c0awDMpDLukF/?ref=app
11th Sep 2017, 3:02 PM
Melih Melik Sonmez
Melih Melik Sonmez - avatar
+ 1
. what is the need of method overriding except abstract class and interface(implements)... class A { void show(){ Sop("somnath");} } class B extends A { p s v m() { new B().show(); } void show(){ sop("ghosh"); } so now the question is what is need of method show in class A. Have u ever used method overriding concept in ur project where u have 'defined' both the method not declare in 1 class, define in another class.
13th Jul 2017, 7:13 AM
Somnath Ghosh
Somnath Ghosh - avatar