#Polymorphism meaning
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
is there anything else to it?
Polymorphism in Java refers to the ability of a variable to reference objects of different classes in an inheritance hierarchy.
In the context provided, the line of code "ParentClass bbj = new ChildClass(stuff in param);" demonstrates polymorphism through a concept called dynamic method dispatch. This means that the reference variable 'bbj' of type ParentClass can refer to an object of ChildClass.
By using this approach, you can write more flexible and reusable code as you can create methods that accept objects of the parent class but can also work with objects of any child class. This allows for code that is easier to maintain and extend.
Polymorphism is closely related to inheritance in Java, as it relies on class hierarchies where child classes inherit from parent classes. By utilizing polymorphism, you can write more generic code that can work with multiple related classes without needing to know the specific details about each class.
Overall, polymorphism is a key feature of object-oriented programming in Java that allows for flexibility and extensibility in your code by enabling different classes to be treated uniformly through their common parent class.
its ust using the parent class to refer to child class?
but whats the difference between
ParentClass obj = new ChildClass();
and
ChildClass obj = new ChildClass();
i think its like u can access childclass and parentclass methods but cant u already do that with the extends keyword
parentObj can point to a boyObj or girlObj, but boyObj type variable can't point to a girl
because girl IS A parent, but girl IS NOT A boy
The value of it is not in the declaration of a variable, but in the signature of methods.
public T doSomething(ParentClass o) {
...
}
This method can be used with any subclass of ParentClass.
Fun with pattern matching as well.
We generally declare variables with the most general type, to (a) communicate to readers/maintainers that we're only using properties common to this type, not only the subtype, and (b) to make it easier to change our minds about the subtype with minimal change to the application.
Usually justified as an application of the Liskov Substitution Principle, though often that's an overreach.
yeah but functionally is it different? or is it just a convention used
You can only access methods declared on the supertype (or it's supertype). But the implementation that is executed is whatever is on the actual object type (ChildClass in this case).
supertype is the parent class right
u need to differentiate variables from the objects they refer to.
You can think of it as the compiler sees ParentClass, but the runtime applies ChildClass.
Any ancestral class.
when u write Animal animal = new Dog(); then u created a dog, not an "animal"
the instance is the dog
yes
but u point to it via a variable that says "i can refer to anything thats an animal"
by that u increase ur code modularity
ur code can now also work with a cat
should u decide in the future
ohh ok
so its mainly used if you want to refer to the child classes easily
like if you want to add a cat class or smth you can do
Animal animal = new Cat();
i think ur missing the point
see, if u need a list in ur code
then u should write List
not ArrayList
u dont want to be too specific
but only mention what u actually need
bc that way ur code can then work with anything that fulfils the requirements
ok
lets say u write a method that feeds sth
then all u need is that the thing u want to feed has a eat(Food food) method
u dont need the fact that it can walk
or bark
or jump
yeah
so ideally what u want to do in this situation to maximize code modularity/flexibility is to create an interface that specifies just eat
interface CanEat {
void eat(Food food);
}
and now u can write
ohh so if you want a method to work generically then you use polymorphism?
public void feedIt(CanEat canEat) {
canEat.eat(new Banana());
}
People ask "why can't I"
Animal animal = new Dog();
animal.bark();
But think of animal in the more general form. The compiler may not be able to prove it's a dog.
void m(Animal animal) {
animal.bark();
}
This could never safely compile, because I might pass a reference to a Cat object.
The benefit of being able to use an object reference as one of its ancestral types, is so you can define behaviour that is valid for all subclasses. That is the 'polymorphic' behaviour.
now the code u wrote can deal with anything that can eat. a dog, a human, a bird, ... everything can be passed. maximum code modularity
maximum flexibility
yeah
void m(Animal animal) {
animal.makeNoise();
}
Dog and Cat can make different noises, but our code works with both.
ohh
And as Zabuzard indicates, we would generally favour interface types that are more general than the object-hierarchy. This is less restrictive for the caller.
these things make a lot of sense once u write larger projects with hundreds of files
or thousands...
so if you want smth to work more generally you use polymorphism
cause u really dont want to have to change all ur code just to let it additionally handle a cat and a bird
ideally u just always write down the type that u actually need
if all u need to do is iterate over sth then ideally u write Iterable and not List and not ArrayList
ooo
it ties in very well with the purpose of interfaces
giving behaviors to objects
"Make whatever noise this object makes" instead of writing "if it's a bird, chirp, else if it's a cat, meow, else if it's a dog, bark, else fail because we don't handle this one yet"
so pretty much this right
was my explanation good
like if you want something to work for all versions of the object you use polymorphism
and add override methods only when you need to
It will always work for all versions, an override will just alter what it does.