Why are different types of object references allowed in Java?

I wonder why it is allowed to have a link to other types of objects? For instance:

Animal cow = new Cow(); 

Can you give an example where it is useful to use a link to other types of objects?

Edit: Cow extends Animal

+4
source share
7 answers

This underlies polymorphism and abstraction. For example, this means that I can write:

 public void handleData(InputStream input) { ... } 

... and handle any input stream, whether from a file, network, memory, etc. Or similarly, if you have a List<String> , you can request element 0 from it, regardless of implementation, etc.

The ability to consider an instance of a subclass as an instance of a superclass is called the Liskov Substitution Principle . This allows you to freely bind and reuse code.

Also read the Java tutorial polymorphism part for more information.

+10
source

This is called polymorphism and is one of the most powerful aspects of Java.

Polymorphism allows you to process different objects in the same way.

This is a great way to create reusable, flexible code.

Unfortunately, this is part of Java that new programmers often use for understanding.

The example you provided includes inheritance (class extension).

Another way to take advantage of polymorphism is to use interfaces.

Different classes that implement the same interface can be handled the same way:

 class Dog extends Animal implements Talker { public void speak() { System.out.println("Woof woof"); } } class Programmer implements Talker { public void speak() { System.out.println("Polymorphism rocks!"); } } interface Talker { public void speak(); } public static void testIt() { List<Talker> talkerList = new ArrayList<Talker>(); talkerList.add(new Dog()); talkerList.add(new Programmer()); for (Talker t : talkerList) { t.speak(); } } 
+2
source

In a simpler note, this allows polymorphism. For example, you might have several objects that come from Animal, and they all look like a descriptor.

You might have something like:

 Animal[] myAnimal = {new Cow(), new Dog(), new Cat()}; foreach (Animal animal in myAnimal) animal.Feed(); 

The Feed () method must be overridden in each child class.

By the way, the code is similar to C #, but the concept in Java is the same.

+2
source

This is basically the concept of standardization.

We know that every animal has some things in common. Let's take an example of food and sleep, but each animal may have a different way to eat or sleep ... then we can define

 public abstract class Animal { public abstract void Eat(); public abstract void Sleep(); } //Now Define them in various classes.. public class Cow extends Animal { pubic void Eat() { //process of eating grass } public void Sleep() { //process of sleeping } } public class Lion extends Animal { public void Eat() { //process of eating flesh } public void Sleep() { //process of sleep } } 

Now you don’t need to define different objects for different classes ... just use Animal and usually call

 public class MyClass { public static void main(String[] args) { Animal _animal = new //think the type of animal is coming dynamically //you can simply call _animal.Eat(); _animal.Sleep(); // irrespective of checking that what can be the animal type, it also reduces many if else } } 
+2
source

In another class / method, you can use different implementations of the same interface. After your example, you might have something like:

 public void feed( Animal animal ) { animal.getHome().insertFood(animal.getFavFood()); } 

Now you can implement the details in your animal classes and should not distribute this method at any time when you add a new animal to your program.

Therefore, in some cases, you need a common interface in order not to implement a method for each implementation, while in other cases you will need to use an explicit implementation.

+1
source

Just put all the Cows Animals . Therefore, JAVA understands that when Cow extends Animal , a cow can also be called as Animal.

This is Polymorphism, as others have pointed out. You can expand Animal with Dog and say that Dog is also an animal.

+1
source

This is a matter of inheritance.

It allows objects sharing common functions to be handled the same way. It also allows you to provide specific runtime implementations that are subclasses of an abstract type.

I could probably fly for centuries. Perhaps the question is too broad to answer here.

+1
source

All Articles