views:

3778

answers:

4

Hi I just want to make sure I have these concepts right. Overloading in java means that you can have a constructor or a method with different number of arguments or different data types. i.e

public void setValue(){
   this.value = 0;
}
public void setValue(int v){
   this.value = v;
}

How about this method? Would it still be considered overloading since it's returning a different data type?

public int setValue(){
   return this.value;
}


Second question is: what is overwriting in java? Does it relate to inheritance. Let's I have the following:

public class Vehicle{
  double basePrice = 20000;
  //constructor defined
  public double getPrice(){
     return basePrice;
   }
}

public class Truck extends Vehicle{
  double truckPrice = 14000;
  //constructor defined
  public double getPrice(){
     return truckPrice;
   }
}

So now let's say I have the following

Truck truck = new Truck();

if I call

truck.super.getPrice()

this would return the price from the Vehicle class, 20,000

if I call

truck.getPrice()

this would return the price in the truck class, 14,000

IS MY KNOWLEDGE CORRECT FOR BOTH QUESTIONS?

+2  A: 

Correct; overloading is providing multiple signatures for the same method.

Overriding, which is what I think you mean by "overwriting" is the act of providing a different implementation of a method inherited from a base type, and is basically the point of polymorphism by inheritance, i.e.

public class Bicycle implements Vehicle {
    public void drive() { ... }
}

public class Motorcycle extends Bicycle {
    public void drive() {
        // Do motorcycle-specific driving here, overriding Bicycle.drive()
        // (we can still call the base method if it's useful to us here)
    }
}
Rob
+2  A: 

You are basically correct. Overloading is having multiple methods in a single class where the method has the same name. However, the return value is not seen as part of the signature of the method. Thus, you cannot overload a method by changing only the return value. You cannot have the following code, from your example:

public void setValue() {
   this.value = 0;
}

public int setValue() {
   return this.value;
}

This will fail to compile.

As Rob identified, I believe you mean overriding, and you have that correct. Note with overriding, you cannot change the return type. As of Java 5, you can return a derived type of what the base class method returned. Before Java 5, it must be the identical type. That is, you cannot do the below until Java 5 and later:

public class AnimalNoise {}
public class Miaw extends AnimalNoise {}

public class Animal {
    public AnimalNoise makeNoise() {
        return new AnimalNoise();
    }
}

public class Cat extends Animal {
    public Miaw makeNoise() {
        return new Miaw ();
    }
}

However, even in Java 5 and later, you cannot do the following:

public class Animal {
    public String makeNoise() {
        return "silence";
    }
}

public class Cat extends Animal {
    public Miaw makeNoise() {
        return new Miaw ();
    }
}
public class Miaw {}

Finally, a big difference between overloading and overriding that is often overlooked is that overloading is decided at compile time and overriding is decided at runtime. This catches many people by surprise when they expect overloading to be decided at runtime.

Eddie
A: 

what you have described is correct.

For more clarification take a look at polimorphism concept. The Wikipedia has a good article

http://en.wikipedia.org/wiki/Polimorphism#Computing

http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

Luixv
A: 

difference between overrindin and overlaping