My question is. Are these the same?
public class Pet {
}
public class Fish extends Pet {
}
If I extend the class Pet to my Fish class, is that the same as if I instantiate the Pet class in my Fish class? The extends is above and the instantiate is below. Are they the same?
public class Pet {
}
public class Fish {
Pet myPet = new Pet ();
}
First example describes inheritance, the second one - composition. Those are two OOP concepts. They allow programmer to reuse common logic. Your should prefer to use composiiton over inheritance.
Copy from other SO answer: