Java Programming

Method Overriding in Java

Method Overriding happens when the method (function) having the same signature as that of the base class overrides the original method. This makes it possible for the method of the derived class to be used and not the base class.

Language : Java

Related : Method Overloading in Java
[sourcecode language='java']
//Method Overriding
class marks
{
private int total;
private float avg;

marks()
{
total=511;
avg=(total/7);
}

void show() //Show of marks
{
System.out.println(“Average : “+avg);
}
}
class Student extends marks
{
String name;
int roll;

Student()
{
name=”ABC”;
roll=123456;
}
void show() //Show of Student
{
System.out.println(“Name “+name+”\n”+”Roll : “+roll);
}

}

class mainMethod
{
public static void main(String args[])
{
Student std=new Student();
std.show(); //Show of marks is overriden by show of Student
}
}
[/sourcecode]



{ 4 comments… read them below or add one }

1 str October 19, 2009 at 9:49 am

Hi Vaibhav,

Thaks for the script, hmm may i expect php in future ?
Your site is with very usefull information, keep rocking!

Reply

2 Programming Kid October 19, 2009 at 10:02 am

@Str
Thanks for the appreciation, Yes, I would include PHP scripts soon. Programming Kid is relatively new and I am posting Source codes every week.
Stay connected!

Reply

3 str October 19, 2009 at 11:49 am

Hi Vaibhav,

Thaks for the script, hmm may i expect php in future ?
Your site is with very usefull information, keep rocking!

Reply

4 Programming Kid October 19, 2009 at 12:02 pm

@Str
Thanks for the appreciation, Yes, I would include PHP scripts soon. Programming Kid is relatively new and I am posting Source codes every week.
Stay connected!

Reply

Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.


WordPress - Vaibhav Kanwal