Class
Class
Overloading occurs when two or more methods in one class have the same
method name but different parameters.
Example:
class Dog
{
public void bark()
{
System.out.println("woof ");
}
//overloading method
public void bark(int num)
{
for(int i=0; i<num; i++)
System.out.println("woof ");
}
}
Overriding means having two methods with the same method name and
parameters (i.e., method signature). One of the methods is in the parent class
and the other is in the child class. Overriding allows a child class to provide a
specific implementation of a method that is already provided its parent class.
Example:
class Dog
{
public void bark()
{
System.out.println("woof ");
}
}
class Hound extends Dog
{
public void sniff()
{
System.out.println("sniff ");
}
Throw Example