Method overriding is a runtime polymorphism mechanism where a child class provides a specific implementation of a method already defined in its parent class, requiring the same method name, parameters, and return type, and occurring only through inheritance; in contrast, method overloading is a compile-time polymorphism mechanism where multiple methods with the same name but different parameters exist within the same class. Key rules for method overriding include: the overriding method cannot be more restrictive than the overridden method in access modifiers, cannot override private methods, cannot override static methods (which results in method hiding instead), cannot override constructors, and can use covariant return types (subclass return types) for class types but not primitive types.
深掘り
前提条件
- データがありません。
次のステップ
- データがありません。
深掘り
Method Overideing And Method OverLoading Day 44 By Kishan追加:
can you please respond in the chat all right all right all right yes everybody greeting guys very very green all right so let us start our session here today so guys before starting our sessions as always let us try to have a very very small and a quick recap of the concepts which we had covered in our yesterday's class right so guys yesterday we have started discussing about polymorphism and I told you like what is actually polymorphism polymorphism means it is a process of Performing various tasks with the same identity I already told you that and yes there are actually two types of polymorphisms are there first one is compile time polymorphism which is also known as method overloading and second one is the runtime polymorphism which is also known as method overriding now yesterday we have discussed about method overloading yes tell me what do you mean by method overloading which word you need to start writing exactly writing two or more methods in the listen listen guys listen writing two or more methods in the same class same same class once again writing two or more methods in the same class having same method name but different parameters method name should be same parameters must be different okay I mean you can write minimum two methods or more than two methods and kindly please do remember you need to write them in the same class same class so writing two or more methods in the same class having same method name but different parameters that is actually known as method overloading and guys I already told you any method can be overloaded any method private method protected method static method final method main method Constructor even if a method is having different different return types that does not matter any method can be overloaded any method any method okay so yes those are the concepts which are covered in our stress class now now rathod overload overload overload don't change the statement this is where confusion happens okay so please please even in sleep also you should not doing the mistakes if you got a question on method overloading and overriding nice okay can you please do remember that particular Point okay now method overriding m-e-t-h-o-d method over r i d d i n g method overriding now let us try to have an idea about what is actually this method over writing now now just a minute let me just make it like this okay now guys please listen to me writing writing to our more methods in to different classes writing two or more methods right now we are discussing about method overriding method overriding okay writing two or more methods in two different classes two different classes okay just let me try to write like this two two different classes having same method name same parameters foreign type and same origin type that is actually method over writing once again what do you mean My Method overriding writing two or more methods writing two are more methods in two different classes having same method name method name should be same parameters should be same return type also should be same now answer my question guys please answer my question what is the main difference in method overloading and method overriding what is the main difference in method overloading and Method already can you tell me guys the first difference is the first difference is Method overloading occurs in the same class am I right or wrong please tell me thod the first difference first difference is Method overloading occurs in the same class but when you're speaking about method over writing method overriding guys method overriding occurs in occurs in two different classes kindly please do remember this okay method overloading occurs in the same class method overriding occurs in two different classes means without using inheritance I repeat without using inheritance you cannot override a method let us try to write a small program on this are you ready guys that is great that is great okay I'm having Class A and classmate okay now I want to achieve method over writing method overriding okay let me try to take a method void method one like this system dot out dot printlength system.out.printlen I am writing class EA method okay class a method all right now guys I want to perform over riding method overriding it is also known as runtime polymorphism it is also known as runtime polymorphism okay now if I want to perform method overriding pretty much damn sure how many classes should be involved how many classes should be involved two classes two classes and guys please do remember this key point please without using inheritance without using inheritance we cannot achieve method over writing sorry yes just let me try to write like a small point without using inheritance we cannot achieve method overriding I hope it's clear guys is it clear for everyone without using inheritance we cannot achieve methanol writing okay all right all right so guys come on I want I want to perform method overriding tell me first what I need to write I want to perform method overriding for this method for this method what do you mean by Method overriding same method name okay same method name same parameters yes same parameters and same return type okay void same return type open braces and close braces and here simply I am writing system dot out dot println Class B method Class B method now guys did You observe the code here for method one which is present in class A you are having one implementation for method 1 which is present in class A you are having one implementation for method one which is present in class B we have changed the implementation of method one emulator wrong to be precise to be precise we have over reading the implementation of method one sir what do you mean by overriding guys overriding means you are changing the implementation of a particular method are you understanding me guys method name is same method name is same same method name same parameters same return type same return type now for example just let me try to make it like in ta I'm trying to write it like NTA now is this method overriding no way this is not method overriding I hope it's clear guys for everyone okay we'll do one thing we'll do one thing guys now please observe here let me go to my main method let me go to my main method now inside my main method I'm trying to guys what is the second point in inheritance can you please give me the code for second point in inheritance second point foreign in inheritance it's okay take time you can refer no problem second point foreign complete point please okay okay okay okay right right so second point is we can hold child class object with parent class reference am I right or wrong guys please respond in the chat for me am I right or wrong yes that is very good now with the help of this reference which class methods we can call answer my question which class methods we can call sir we can call only parent class method sir yeah that is very good that is very good are you observing here we are calling method one which is present in class A right okay now let me execute my program by the way what is your expected output what is your expected output your expected output is class a method right click run as Java application what is your output guys what is output we got Class B method are we calling Class B method no no we are not calling Class B method we are calling class a method only but as we are performing method overriding instead of executing class a method Class B method will be getting executed disease method overriding this is exactly method overriding you are trying to call parent class method you are trying to call parent class method but which class method is getting executed which class method is getting executed Shale class exactly okay guys for example for example I will be trying to write into a like this now if I execute my program what is my output if I execute my program right now what is my output am I going to get my Class a method or Class B method output right you are saying that I'll be getting class a method right click run as Java application of course I'll be getting class a method why why because I did not perform a method overriding here I did not perform method already are you understanding me guys what do you mean by Method overriding what do you mean by method of writing writing two or more methods in two different classes having same method name same method name same parameter okay this is parameter is changed parameter is changed so nope now how about now now it is having same method name same parameters and same return type so right now if I am trying to execute my program instead of class a method I will be getting Class B method as Optimum is it clear for everyone optimize it clear for everyone please respond in the chart for me that is literally very good guys fantastic now now guys please observe here please observe here Point number two point number two if we can't inherit a method we can't override that method did you understand this point did you understand this point if we can't inherit a method we can't override that method for example for example let me make my class a method as private you know that private methods I cannot access in other classes am right or wrong guys yes now please observe here please observe here in class B am I getting an error yes or no if we can't inherit a method we can't override that method I hope you guys are understanding me guys am I clear with this am I clear with this okay okay for example for example let me make this method as a default method and Sir okay right now we are performing method overriding and uh we are able to get Class B method okay but I want to get my Class a method executed any ideas I want to get my no I want to see this output I want to see this output any ideas from the main method from the main method from the main method okay guys what is the first point first point first point first point in inheritance new class EA equal to aob J2 belongs to class EA now aov jet2 dot method one right click run as Java application now did you understand the difference between first point and second point in inheritance now did you understand the difference between first point and second point in inheritance guys please respond please respond exactly exactly okay I already told you guys those what are the key points which I told you in inheritance those literally those points literally rule the oops Concepts am I cleared with these guys I hope it's clear guys for everyone right okay okay now now for example for example guys answer me question answer my question please answer my question line number 12.
executes which method I mean the method present in class A or Class B line number 12 execute switch method the method present in class B very good line number 15 executes which method the method present in class here Class B Class A very good very very good now guys please please concentrate guys guys this is very very important and a very important very very important please please concentrate my question is very very simple we are having aob J1 and a obj2 okay now my question is [Music] is referring to Which object is referring to Which object referring referring referring to guys this is this is not referring okay this is belongs to class A but I'm asking a o b j 1 is pointed towards Which object Class B object so which method is getting involved which method is getting invoked method present in method present in class B this caobj2 is referring to Which object class a object so whenever you are calling method one which method will be getting executed method present in class A will be getting executed are you understanding me guys are you understanding me the method present in the method present in parent class is known as overridden method the method present in child class is known as overriding method foreign method and this is overriding method I hope it's clear guys for everyone right do me a favor do me a favor please keep us citing as method overriding please keep your studying as method overriding and I want you to please write the definition of this method overriding first one please write this definition please write this definition if possible try to write it by your own okay guys completed completed right right now now again Suresh the method which is present in class A is known as over ridden method over written Class A is overridden Class A is overridden Class B is overriding Method okay right now guys do me a favor do me a favor I want you to please write these two points can you do that for me these two points please write them in your notes right now whenever you're writing read them read them and write guys this this point please read it correctly this point this point please please read it correctly please read it correctly slowly read it slowly read it slowly okay guys is it completed is it completed foreign guys generally method overriding when you are speaking about method overriding whenever we are running our program only running running whenever we are running our program only at that time only these objects are created am I right or wrong whenever we are running only these objects are created so run time only it will be decided which method should be called that is the only reason it is known as runtime polymorphism okay now please please have this program for me Class A please have this program Class A Mukesh uh asking can we override a method only once you mean like in the same class or else in different class Mukesh in different class you can go ahead no issues now uh Asma which version of that method means whether that method one is present in class A or Class B okay for example you are calling here method one which version of method will be getting executed class a method or Class B method here here in line number 12 which method will be getting executed Class B method will be getting executed that is it yes yes yes please please Mukesh yeah you can override in another child class no problem that is actually done with hierarchical inheritance right okay for example if you are having Class C if you are having Class C if you want to override method one which is present in class A yeah you can overwrite no problem it will not be having any impact okay buddy no not multi-level hierarchical Class A will be inherited in class B and Class A will be narrated in class C also right please don't get countries please don't get countries guys please inform if it is completed please inform me if it is completed we'll be going with some important key points which we need to discuss informative community right one more minute one more minute just one more minute right right right right I hope it's completed guys for everyone yeah Ganesh if you are guys if you are trying to provide method overriding for a class which is present in another package obviously we need to use import statement am I right or wrong guys right right right right okay guys okay okay I hope it's complete for everyone I'm removing this I'm removing this removing removing right just let me try to save it okay now Class A and Class B okay now guys just like over loading in overloading we have seen some important points key points likewise we will try to see some key points in method already okay now guys kindly please do remember kindly please do remember whenever you are writing a program on method overriding means if the interviewer asks you please try to write like please try to implement a program on method overriding remember you need to use the second point in inheritance means new class B hold it with parent class reference parent class reference and please call parent class method you should call parent class method but child class method should be executed I hope it's clear guys is it clear for everyone yes that is very good that is very good now guys please check this please check this okay method overriding means same method name same parameters same return type okay but two different classes right right so I already told you without using inheritance we cannot perform method overriding and if we can't inherit a method if we can't inherit a method we cannot override that method I already told two points here two points now now for example guys right now I want to make my overridden method what is this method guys is it over reading or overriding method over reading or overriding method please tell me please tell me yes this is over r i d d e n over again method all right okay over written method sorry yes already method now right what I want to do here is I want to make my overridden method as public now in my overriding method I am getting an error Did You observe that Did You observe that y why why I am getting an error why I am getting an error right guys kindly please listen to my statement please please uh listen to my statement correctly please listen to my statement correctly so some people are saying that some people are saying that sir here access modifiers are not same but guys I never told in my definition access modifiers should be same did I told like that did it all like that no no I did not know like okay for example for example for example let me make this as default default and here I'm making this as public now also access modifiers are different but am I getting any error no I'm not getting any error then why if I write public if I write public and if I make this as a default then why I am getting an error because guys listen listen listen listen very important point overriding method let me try to write like this over riding method should not be should not be more restrictive than the over ridden method overriding method overriding method means parent class method or child class method should not be more restrictive than the over ridden method means parent class method okay guys now I am going to take your help whenever I discussed about access modifiers concept I gave you a hierarchy did you remember that like I told you what is the most restricted access modifier and I gave you a small hierarchy and I told you that guys I'm going to ask this when I'm dealing with better overriding concept I told you this did you remember that what is it foreign on which date we had covered that point third match on third March when we have covered that yeah I think on third March only I think on third much only I had covered that particular point so no yes yes on third match I gave you that program also yes I told you that I told you hierarchy can you please tell me that hierarchy like what is the most restricted access modifier private after that after that default after that protect it after that public yes did you remember these guys you remember this uh now what I am trying to say is if in the parent class if the parent class method is declared as default if the parent class method is declared as default we cannot declare child class method as child class method as exactly private we cannot declare like that why because private is more restrictive than the default am I right or wrong this is very crucial role sir can we declare I mean can we declare protected can you declare protected yes p r o t a c t protected fine can you declare public fine no problem why because parent class method is default so we can have either default or protected or public in the chair class but we cannot have private for example for example guys for example in my parent class if I declared as protected what are my options in my Cherry class how many options I am having tell me how many options I'm having what are those what are those one or two guys why some people are saying only one for example if this is p r o t c Theory protected in the child class I can have protected I can have protected and I can have public also no problem guys yes or no here tell me protected and public right for example in my parent class if I'm having public then how many options I'm having exactly now you got it guys now you got it if I'm having public Pub LIC public only public only public I cannot have protected vro tactd protected nope why because protected is more restricted than the public so I cannot have data is it clear is it clear right right okay or I'll be having only option that is public now tell me in my parent class if I'm having private what are my options uh-huh all the four very good very good I can have all the four options uh nice can you inherit a private method can you inherit a private method can you override a private method so why you are saying all the four options did you understand the tricky question guys if you are trying to declare this as private basically we cannot override private methods guys we cannot override see that is what I said this please observe here if we can't inherit a method we can't override that method example example ah which methods which methods private methods we cannot overwrite private methods I hope it's clear guys for everyone is it clear guys please respond please respond yes that is good that is good okay okay now now okay for example my next question is guys can I override if two methods are having different return types can I override if two methods are having different return types very good I am impressed with your answer guys okay if I'm trying to have enp here and please observe here r e t u r return 10.
return 10 like this okay return 10 obviously obviously I'll be getting an error here why because this is not method overriding same method name same parameters same return type should be there am I right or wrong now now guys please listen to my question correctly can we override a method if we are having different return types the answer is yes sir what you are saying just now you said no hang on hang on guys hang on guys please observe here did you remember what are our options for giving the return types how many options are there did you remember can you please tell me what are those can you please tell me what are those we can avoid we can avoid we can have eight primitive data types we can have classes and we can have objects I told you right ah now what is this guys it is a primitive data type I'm right or wrong primitive data type okay now what I want to do you know instead of primitive data type I'll be using a class I'll be using a class did you remember this keyword I can use this to return the instance of the class am I right or wrong now now here also if I want to perform method overriding which class name I need to write come on tell me which class name ah exactly class a like this same methane same with the name same parameters same parameters same return type same return type okay and here r e t u are in return this return this okay return this let me execute my program right click run as Java application and I am getting my output like this I'm getting the output like this class B method called Class B method okay so here overriding is happening overriding is happening sir but you said that different return types yes I'm coming there I'm coming there instead of Class A I am giving Class B am I getting any error am I getting any error no I'm not getting any error let us execute right click run as Java application is Method overriding happened is Method overriding happen or not yes it got happened a return type same or different is red type same or different these are different return types are these primitive data types no these are what what are these classes classes am I right now Guys these are known as covariant written types what are those what are those Co variant covariant return types covariance written types okay in covariance covariant return types means these return types will not be applicable for primitive data types how many primitive data drives are there how many premature types are there eight primitive data types are there right exactly I'm not speaking about primitive data types these are covariant return types covariant written types applicable to classes not for primitive data types okay so in covariant return types return types just let me try to okay in covariant return types we can achieve method over writing we can achieve method overriding even though even though there are there are ah what even though there are even though there are exactly different return types even though there are different written types this is applicable only for covariant written types please don't forget that this is this this particular point is only for covariant Redux covariate types means classes simply classes I'm not speaking about primitive dead types just classes as simple as this now now let me just get it back I'm making this as okay guys do me a favor do me a favor I'll just keep it like this I mean class this is Class B okay I mean classy classic covert interior types let me keep it like this okay now I'm trying to have one more method one more method okay uh avoid method to void method two and here system dot outdoor println I'm having hi I'm having high now I want to override this method in class B okay I'm trying to override this method in class B this is Method two and here system.out.println I am writing hello okay now simply a o b j dot method two uh what is my output right now what is my output here what is my output here higher Hollow very good very good guys very good very good I'll be getting my output as hello okay now my question is can I overload a static method with a non-static method can I override my mistake I'm not speaking about overloading overriding sorry my mistake can I override can I override a static method with a non-static method overloading we can do that like yesterday we already seen that overloading we can do that I am speaking about overriding okay let us check let us check guys please observe here please observe here I'm making this as static answer my question answer my question can I override a static method with a non-static method no okay so let me make it as static s t a tic static now can I override can I override okay right now what is my expected output here what is my expected output come on come on right right right we are expecting our output to be hello but we will be getting our output as high why because static methods can't be overridden we cannot override static methods we can't override static methods it may seem like you are overriding but no that is actually method hiding we can't override static methods as simple as this fasting blasters so now answer my question guys can we overload static methods can we overload static methods yes can we overload private methods private methods overload listen to the question correctly yes can we override static methods no can we override private methods no can we override Constructor Constructor let us check let us check guys please observe here give me a Constructor here yes Krishna exactly now guys please tell me give me give me an example for a Constructor here okay just let me go with like class here this is a default Constructor system.printle in class A Class A DFA T4 c o n s t r u c t or default Constructor okay now I want to write I mean I want to achieve method overriding for this Constructor Point number one guys Constructors participate in inheritance will construct us participated inheritance yesterday not as the day before yesterday only I told you no Constructors will not be participating inheritance so can we override no I always if we can't inherit we can't override anyways let us check here okay guys tell me one thing for example for example if I want to override a Constructor tell me what I need to write here tell me what I need to write here Class A uh okay same name uh class name same parameters if I write like this 100 I will be getting an error why I will be getting an error Constructor in class A in class B this is not a Constructor what is the name of a Constructor in class B guys exactly it will be Class B am I right or wrong so different names different names means that is literally not polymorphism guys I hope it's clear for everyone is it clear so Constructor over loading is possible but overriding is not possible I told this point at the time of Constructors did you remember that okay Constructor over RI overriding is not possible guys did you remember that point like at the time of Constructors I gave you this yes yes yes yes that is very good that is literally very good guys yeah last point last point in Constructor right now guys if I ask you to say the difference between overloading and overriding can you say difference between overloading and overriding okay do me a favor do me a favor first write this program for me please first try this program go ahead Class A go ahead please write it please write it okay Ganesh what do you mean by class level what do you mean by class level please be precise in your question please guys class here completed for everyone oh okay okay please be first be first be first wait for it wait for it wait for it you will be understanding the importance of overriding whenever we are dealing with abstraction please wait for it upendra we have covered this concept in our this keyword did you remember that did you attended that class this keyword class did you attended that I told you this keyword is used to like is used in four ways I told you four ways use it to resolve the ambiguity between local variables and instance variables used to call the present class methods yes nobody you should not be attending late it is used to return the instance right now we are returning the instance of the class okay right Class B guys Class B foreign types means nothing but instead of using primitive data types we will be using class names that's it okay that is perfect that is perfect and guys again I am saying you every day I'll try to upload a question on my Instagram page please try to answer that question guys don't leave that answer it don't look and leave please try to answer that please answer if I give a program if I give a program try to write that program in your eclipse and paste that program in the comment so that others are also will be seeing that right please make that happen guys please make that happen foreign okay there is literally I mean when I see hey book I'll understand each and every concert any book is good actually okay some people are asking Instagram ID this is the ID guys I'm in pranita you can check this and a hitch who is Hitch I don't know right please yeah I will I will alright guys I hope it's complete guys for everyone is it done is it done one more minute just one more minute just one more minute please check with nareshity website foreign fifth means what do you mean by that mukul you want to okay okay I'm sorry I'm sorry I'm sorry I'm sorry I think okay now it's okay now it's fine yes yes please please I'm sorry I did not check that I'm going with your chats uh please go with uh Frameworks also please go with Frameworks also okay Java in the sense a lot of things will be covered core Java Advanced Java Frameworks UI Technologies a lot of things will be there so which one you are good and you should be telling me that not simply like Java like are you green core Java Advanced Java Frameworks UI Technologies like that video okay video compared core Java no it's going on okay anyways I'll speak it all right so guys I hope it's completed guys for everyone now please please check these differences just just read them just read them whether or not completed okay hang on please please please please please please carry on carry on carry on please carry on foreign [Music] okay now guys please do me a favor read just read these differences I don't want you to write I'll be uploading this uh in our Google Classroom just read them please foreign foreign guys foreign all right guys that is great that is great so guys I'll be trying to upload all these our slides in our Google Classroom kindly please go through them once for me guys okay please go through them please go through them all right all right that's it guys that's it for today that's it for today so thank you guys thank you for listening I hope you have enjoyed our today's session we'll be trying to continue with our oops Concepts on our Mondays class guys okay and uh have a fantastic weekend have a fantastic weekend and uh please go through the Concepts which we had covered up to now okay please please thank you guys thank you
関連おすすめ
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











