• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Overriding

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i am confused in overriding . Can any one expalin me this concpet with example
thanks in advance
anita dhar
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overriding::
if want to know first understand what exactly is overriding
executing a method which you wish on account of a method which has been defined in the super class.
As you must be knowing that to implement overriding concept we need to inherit the class
the method defined in the subclass has the same signature as that in the super class, the subclass method overrides the superclass method

/*Here we have a class overRidingOne with a method display.
* we want to display the output according to our requirement.
*/
class overRidingOne
{
private int i,j;
overRidingOne(int a, int b)
{
i = a;
j = b;
}
void display()
{
System.out.println("i and j " + i + j);
}

}
/*the class overRidingTwo is extended to implement the display function of the super class i.e. overRidingOne*/

class overRidingTwo extends overRidingOne
{
private int k;
overRidingTwo( int a, int b,int c) //constructor to accept the values
{
super(a,b); //value of a&b are passed to the super class constructor
k = c;
}
/* as we want to implement the overriding attribute, so we need to declare the method with same return type, same name and same parameters.
*/
//this display will override the display method of overRidingOne class
void display()
{
System.out.println("k " + k);
}


}
public class OverRiding {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// instantiating the overRidingTwo class and assigning the values
overRidingTwo overObj = new overRidingTwo(5,6,7);
/* calling display method which will override the display of overRidingOne and provide the output of overRidingTwo*/

overObj.display();

}

}


Hope this helps to clear the doubt
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy "Akumar" !

Thanks for your contribution to this forum but...



Only one small issue: The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:
http://www.javaranch.com/name.jsp


So, could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.


I'm posting this because I am one of the moderators of this forum.


Yours,
Bu.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aneea,
Overriding method in subclass should have same name, same arguments and same return type as that of the overriden method in Superclass. Important to know is that the Overriding method's visibilty should not be restricted. That is if the overriden method in Superclass has default access modifier then overriding method's visibility should be public/protected it can never be private.

Hi Akumar,
How come private variables i and j of class overRidingOne will be accessed by class overRidingTwo? Please let me know.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
There are many rules that need to be followed for method overriding.
1. The argument list of overriding method must excatly match that of overriden method otherwise you will end up writing a overloaded method.



2. The return type same or a subtype of the overriden method.


3. The access level cant be more restrictive than the overriden method.

The above code will result in compiler error because you cannot restrict
the access level of the overriden method in subclass. This about it the parent class publishes that the access level of display() is public but class B restricts it to private. Suppose if this was allowed
then
A parent = new B();
parent.display(1,2); ///
Due to overriding you are invoking display() of class B which is private but since you are using parent reference you are allowed to do so. But at runtime this will a problem because it will try and invoke display() in class B which is private and you know that you cannot invoke private methods.

4. The access level can be less restrictive than the overriden method. Read above and you should be able to answer this sitter.
5. To override a method , the method must first be inherited and then only you can provide a better version of the method, I mean override it. And hence only instance methods can be overriden that are again not marked private or final. Because instance methods with private/final are not inherited by the subclass. And again only instance methods can be overriden and not static methods, because static methods are iherited by the subclass.
6. The overriding method can throw any runtime exception that is not thrown by the overriden method. As the runtime exception happens at offcourse runtime, the compiler has no clue to which runtime exception the overriding method will throw and hence it allows this behavior.
7. The overriding method must not throw any new or broader unchecked exceptions than the overriden method. Think about it the parent class publishes that a method display() can throw FileNotFound exception using the throws clause in the method signature, so now when you invoke the method you will have a try/catch to catch the FileNotFoundException, Suppose the compiler would have allowed the rule 7 then and if you declare in your overriding method to throw IOException [which is a parent class of FileNotFoundException] then
parent class: void display() throws FileNotFoundExcepion
child class void display() throw IOException
Parent p = new Child();
p.display() --> At compile time you say that the method display() will throw FileNotFound exception and hence you either catch it or throw it. And now when the code runs it actually invokes the overriding method in Child class which throws IOException/ a subtype of IOException say EOFException but you only have a try/catch for FileNotFoundException and hence the program will halt at run time. This is the reason you cannot have broader or new exceptions thrown by the overriding method that are not decalared in the overriden method.
8. The overriding method can throw fewer/narrow exceptions than that thrown by the overriden method. Just give a thought to this and read rule 7 and you will understand as to why.
I hope this clears all doubts on overriding method.
And only methods [instance] can be overriden and not variables.

Thanks
Deepak
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Seema,
You cannot access the private memebers of any class outside the class scope, as a result OverRidingTwo cannot access the private variables i and j of OverRidingOne.
But if you wish to access the variables of the parent class in child class then you must first make those visible in the child class and then use super keyword to access them so now say Parent class has a variable xyz marked with default/public/protected access then you can access its value in child class using super.xyz and in same way you can invoke the super class methods using super.display(1,2) in your child class.
Hope this clears your doubt.
Thanks
Deepak
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Seema,
You cannot access the private memebers of any class outside the class scope, as a result OverRidingTwo cannot access the private variables i and j of OverRidingOne.
But if you wish to access the variables of the parent class in child class then you must first make those visible in the child class and then use super keyword to access them so now say Parent class has a variable xyz marked with default/public/protected access then you can access its value in child class using super.xyz and in same way you can invoke the super class methods using super.display(1,2) in your child class.
Hope this clears your doubt.
Thanks
Deepak


Ya
I agree with Mr. Deepak jain.
Then
I believe the program was not a good one. Because if I was writing the super class. Then I would use some public methods like getI() to access i, which would be available to all the subclasses. And thats how we can use the super class's variables in the subclass's method.
 
Seema Sharma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks i got it now.
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic