Method Overriding in Java

Sample code for Method overirding.

File:meoride.java

class a
{
void get()
{
System.out.println(“hi”);
}
}
class b extends a
{
void get()
{
super.get();
System.out.println(“Nice”);
}
}
class meoride extends b
{
public static void main(String ar[])
{
meoride c=new meoride();
c.get();
}
}

Output:
hi
Nice

Share your queries and comments.

Advertisements

Method Overloading in Java

Here is sample code for Method Overloading.

File:meover.java

class meover
{
void san(int a)
{
System.out.println(“The Number is ” +a);
}
void san(String a)
{
System.out.println(“The String value is ” +a);
}
void san(char a)
{
System.out.println(“The Character is ” +a);
}
public static void main(String ar[])
{
meover me=new meover();
me.san(7);
me.san(“Nice”);
me.san(‘S’);
}
}

Output:
The Number is 7
The String value is Nice
The Character is S

Share your queries and comments.

Calling a Method in Java

Here is the sample Java code that demonstrate how to call a method that is available in a non-main Class.

File:demo1.java

class dem
{
void get()
{
System.out.println(“Hi! Welcome”);
}
void show()
{
System.out.println(“Thanks”);
}
}
class demo1
{
public static void main(String a[])
{
dem d=new dem();
d.get();
d.show();
}
}

Here is the Output:

Hi! Welcome

Thanks

Share your comments and queries