Reputation: 41
i have an exercise to solve, i have the correct answer, but I can't get it... Could someone help me what is happening after every "step". here's the exercise. We have 2 classes: enter code here
public class CA
{
protected String descricao;
private float valor;
public CA(String descricao, float valor)
{
this
.descricao = descricao;
this.valor = valor;
}
public String getDescricao()
{
return descricao;
}
public float getValor()
{
return valor;
}
public float teste(float a)
{
return soma(a);
}
public float soma(float a)
{
return valor + a;
}
}
and the second :
public class CB extends CA
{
private final int maxStock = 15;
private int stock;
public CB(String descricao, float valor)
{
super(descricao,valor);
stock = 0;
}
public int getStock()
{
return stock;
}
public void setStock(int actual)
{
stock = actual;
}
public int emFaltaStock()
{
return (maxStock-stock);
}
public float soma(float a)
{
return getValor() + a * 2;
}
public boolean noLimite(int minStock)
{
return ((minStock-stock) <= 0);
}
}
The question is what will be the result of these statements:
CB cb1 = new CB("cb1",10);
CA ca1 = cb1;
float v1= ca1.soma(2);
I know it will be 14, but why? Can anyone tell me?
Upvotes: 1
Views: 63
Reputation: 225
I know it will be 14, but why? Can anyone tell me?
Why because, that is the beauty of Polymorphism
.
Step 1: CB cb1 = new CB("cb1",10);
Here the value of valor = 10
Step 2: CA ca1 = cb1;
here reference
of CA
is reference
of CB
.
Step 3: float v1= ca1.soma(2);
So when you call soma
, the method of CB
will be called and return the value as 14.0
.
Go through: What is polymorphism?
Upvotes: 0
Reputation: 3783
This is Because you created an object of a child class and assign it to a super class like so :
Father obj = new Child();
if you have a declaration like this which yours is exactly the same :
CB cb1 = new CB("cb1",10);
CA ca1 = cb1;
float v1= ca1.soma(2);
OR
CA ca1 = new CB("cb1",10);
float v1= ca1.soma(2);
and there is a method with the similar signature sharing in father and child, always the child method will be proceeded ~
this technique is so useful in Object Oriented Programming because you may have many child class which are extending a Father class and each of those child classes might have an overwriting method with different algorithms
This approach calls Polymorphism
in Object Oriented Programming.
Upvotes: 0
Reputation: 412
It is an exhibition of what we call (polymorphism). While you are setting CB values (Subclass values) in first statement. and calling the subclass instance with superclass ca1 refernce in 2nd statement. In 3rd statement when you call method with superClass ca1 reference, the subclass CB method is called, due to polymorphism in java. hence the result is 14.
Upvotes: 1
Reputation: 2722
Please try to understand the flow
CB cb1 = new CB("cb1",10); // Object of CB has been created
CA ca1 = cb1; // Referencing CA instance to CB instance
float v1= ca1.soma(2); // Calling *soma* Method
as soma is an overriden method ... method of CB will be called even with ca1.soma(2);
as ca1 is actually referring to CB instance. Method Calling depends on Instance and not on Reference .
Upvotes: 0