Program:
class rectangle
{
int dim1,dim2;
double s;
rectangle(int a,int b)
{
dim1=a;
dim2=b;
}
void area()
{
s=dim1*dim2;
System.out.println("Area of rectangle:"+s);
}
}
class triangle extends rectangle
{
triangle(int a,int b)
{
super(a,b);
}
void area()
{
s=(dim1*dim2)/2;
System.out.println("Area of triangle="+s);
}
}
class circle extends rectangle
{
public circle(int a)
{
super(a,a);
}
void area()
{
s=3.14*dim1*dim1;
System.out.println("Area of circle="+s);
}
}
class ovriding
{
public static void main(String args[])
{
rectangle x=new rectangle(3,4);
triangle y=new triangle(3,4);
circle z=new circle(10);
rectangle p;
p=x;
p.area();
p=y;
p.area();
p=z;
p.area();
}
}
Output:
nn@linuxmint ~ $ javac ovriding.java
nn@linuxmint ~ $ java ovriding
Area of rectangle:12.0
Area of triangle=6.0
Area of circle=314.0
nn@linuxmint ~ $
class rectangle
{
int dim1,dim2;
double s;
rectangle(int a,int b)
{
dim1=a;
dim2=b;
}
void area()
{
s=dim1*dim2;
System.out.println("Area of rectangle:"+s);
}
}
class triangle extends rectangle
{
triangle(int a,int b)
{
super(a,b);
}
void area()
{
s=(dim1*dim2)/2;
System.out.println("Area of triangle="+s);
}
}
class circle extends rectangle
{
public circle(int a)
{
super(a,a);
}
void area()
{
s=3.14*dim1*dim1;
System.out.println("Area of circle="+s);
}
}
class ovriding
{
public static void main(String args[])
{
rectangle x=new rectangle(3,4);
triangle y=new triangle(3,4);
circle z=new circle(10);
rectangle p;
p=x;
p.area();
p=y;
p.area();
p=z;
p.area();
}
}
Output:
nn@linuxmint ~ $ javac ovriding.java
nn@linuxmint ~ $ java ovriding
Area of rectangle:12.0
Area of triangle=6.0
Area of circle=314.0
nn@linuxmint ~ $
Method Overriding in Java
ReplyDeleteNice post.
I am DR. GURUJI NAVGRAHA from India a spell caster that has been successfully providing results to my clients for over many years. I use nothing but 100% safe and ancient Indian spiritual methods,I will reunite your ex partner back to you, stop an impending divorce or break-up, and bring back the passion, love, lust, communication and commitment between broken lovers. Don't live in a life unfilled without the one you truly desire. Let me show you the real results you've been searching for,My Powerful Indian
ReplyDeleteSpiritual Spell are :
wedding ritual, healing ritual,Divorce stop ritual,separation ritual,love ritual,protection ritual,self-confidence ritual,child ritual,success ritual,Return Lover ritual,restore passion ritual Save Marriage Spells,Prevent Divorce Spells,Cheating ritual Remove Ex ritual,Business increase,Money
ritual,Famous ritual,Indian Spiritual Healing center is the answer
Email: casterspell481@gmail.com
Whatsap: +919108256518
DR. GURUJI NAVGRAHA
Join our Best Mern Stack Course In South Delhi and unlock the secrets of full-stack web development using the MERN technology, gaining valuable insights for a successful career.
ReplyDelete