English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am learning java...In inheritance , can anybody give me one simple prog for overloading , overriding...
how i call overloaded function in base class and is the default is derived class, if we use overload fn?
thanks

2006-07-11 17:16:16 · 6 answers · asked by Anonymous in Computers & Internet Programming & Design

6 answers

can u be a more clear about ur second part of ur question?
well i can give u a simple code for overloading and overriding

public class A
{
public int getSum(int a,int b)
{
return(a+b);
}
public double getSum(double c,double d)
{
return(c+d);
}
}

public class B extends A
{
public int getSum(int a,int b)
{
//this function returns the result multiplied with two
return((a+b)*2);
}
public static void main(String args[])
{
B bObj = new B();
int d = b.getSum(5,6);//overriding
System.out.println(d);// will print the (5+6)*2
A aobj = new A();
int e = aobj.getSum(6,5);//overloading
double f = aobj.getSum(5.5,6.3);//overloading
}
}

2006-07-11 18:29:30 · answer #1 · answered by kanna 3 · 0 0

public class SuperClass {
int intergerVar;
double floatVar;

int function(int intVar) {
this.intergerVar=intVar;
return (intergerVar+intergerVar);
}

double function(double floatVar) {
this.floatVar=floatVar;
return (floatVar+floatVar);
}
} //in this class i overloaded function method with difference method seg.


public class SubClass extends SuperClass {
int integerVar;
int function(int integerVar1, int integerVar2) {
this.integerVar=integerVar1;
super.intergerVar=integerVar2;
return(this.integerVar+super.intergerVar);
}
} // this is sub class with is overrided int function method

//main class
class MainClass {
public static void main(String args[]) {
SubClass SubObject= new SubClass();
System.out.println("sub class method (overrided method) "+SubObject.function(5,6));
System.out.println("calling super class method using subclass object "+SubObject.function(7));
System.out.println("calling super class overloaded method using subclass object "+SubObject.function(7.0));
}
}
run this program.

2006-07-11 18:30:28 · answer #2 · answered by ram 2 · 0 0

make positive you position it aside as "motor vehicle.java" instead of "veh.java" once you've the want to make it in a unmarried record.. do not use 'public' for the kind veh. motor vehicle.java kit inheritance; type veh // no public !! { public veh() { } int regnum; String ownername; } type motor vehicle extends veh { int numdoor; public static void significant(String[] args) { motor vehicle newcar = new motor vehicle(); equipment.out.println("finding out"); } }

2016-12-01 02:50:26 · answer #3 · answered by ? 3 · 0 0

Good question.
ram answer is good. He had given good example.
after ur having any doubt then give me mail i will send clear example and notes about it ok
my e-mail is "sarma_rama@yahoo.co.in"
Regards
Sarma

2006-07-12 18:19:38 · answer #4 · answered by rama s 2 · 0 0

you can find it from sun java tutorial form sn web site.

2006-07-11 17:42:40 · answer #5 · answered by OrangeApple 5 · 0 0

search @ http://www.studyjava.org

2006-07-11 23:49:12 · answer #6 · answered by ihoston 3 · 0 0

fedest.com, questions and answers