java polymorphism
Java
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Pig extends Animal {
public void animalSound() {//override superclass method
System.out.println("The pig says: wee wee");
}
}
class Dog extends Animal {
public void animalSound() {//override superclass method
System.out.println("The dog says: bow wow");
}
}
class Main {
public static void main(String[] args) {
//You can add all objects inherited from Animal to an Animal type list
Animal[] animals = new Animal[3]; //Creating the Animal List
animals[0] = new Animal(); //Add a new animal object to the list
animals[1] = new Dog(); //Add a new dog object to the list
animals[2] = new Pig(); //Add a pig object to the list
for (Animal a: animals){
a.animalSound(); //This statement prints the correct string no matter the class
}
}
}#student's implementation of "read" will be called at runtime
Person p = new Student();
p.read();
Inheritance refers to using the structure and behavior of a super class in a subclass.
Polymorphism refers to changing the behavior of a super class in the subclass.
Also in Java:
- Title
- .length array java
- Category
- Java
- Title
- fusion vecteur ordonner java
- Category
- Java
- Title
- java check for string length
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- scanner in java
- Category
- Java
- Title
- main method java
- Category
- Java
- Title
- how to create a 2d arraylist java
- Category
- Java
- Title
- write file from a specific location in java
- Category
- Java
- Title
- java stack empty
- Category
- Java
- Title
- java pass array as method parameter
- Category
- Java
- Title
- java get current milliseconds
- Category
- Java
- Title
- how to change the color of a menubar in java
- Category
- Java
- Title
- android get distance between two locations kotlin
- Category
- Java
- Title
- mutable string in java
- Category
- Java
- Title
- java command line arguments
- Category
- Java
- Title
- iterate through hashtable java
- Category
- Java
- Title
- java set value of arraylist
- Category
- Java
- Title
- hide element selenium
- Category
- Java
- Title
- how to add to a file in java
- Category
- Java
- Title
- android java retrofit offline cache
- Category
- Java
- Title
- basic java coding
- Category
- Java
- Title
- java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.security.MessageDigest$Delegate
- Category
- Java
- Title
- java how to serialize a file into a local server
- Category
- Java
- Title
- java projects
- Category
- Java
- Title
- java decler variabel
- Category
- Java
- Title
- java do while
- Category
- Java
- Title
- how to create a linked list in java
- Category
- Java
- Title
- java regex replace all characters before
- Category
- Java
- Title
- error message pushes button down
- Category
- Java
- Title
- how to write a merge sort array method ni java
- Category
- Java
- Title
- how to byheart faster
- Category
- Java
- Title
- how take inputs in java with message
- Category
- Java
- Title
- arraylist add method
- Category
- Java
- Title
- binary numbers java
- Category
- Java
- Title
- has places api got deprecated???
- Category
- Java
- Title
- create object of static class in java
- Category
- Java
- Title
- how to split a string in java
- Category
- Java
- Title
- getcolor deprecated
- Category
- Java
- Title
- how to extract data from delimited text file in java
- Category
- Java
- Title
- java iterator example
- Category
- Java
- Title
- how to fill a list with a single value java
- Category
- Java
- Title
- how to convert string to double in android studio
- Category
- Java
- Title
- string method example in java
- Category
- Java
- Title
- how to import jframe in java
- Category
- Java
- Title
- iterate through hashmap in java
- Category
- Java
- Title
- parse string to int java
- Category
- Java
- Title
- center textview programmatically android
- Category
- Java
- Title
- init admob
- Category
- Java
- Title
- web app runtime for java
- Category
- Java
- Title
- java comments
- Category
- Java
- Title
- java 8 random string generator
- Category
- Java
- Title
- java script print date in YYYY-MM-DD format
- Category
- Java
- Title
- java stack peek
- Category
- Java
- Title
- palindrome function java
- Category
- Java
- Title
- applicationcontext.xml
- Category
- Java
- Title
- java switch display panel
- Category
- Java
- Title
- how to create a button in java
- Category
- Java
- Title
- java map iteration
- Category
- Java
- Title
- continue in java
- Category
- Java
- Title
- close scanner java
- Category
- Java
- Title
- how to delete character in string java
- Category
- Java
- Title
- print a string java
- Category
- Java
- Title
- print in one line in java
- Category
- Java
- Title
- java strin to int
- Category
- Java
- Title
- change fab image programatically
- Category
- Java
- Title
- index 1 out of bound for length 1 java
- Category
- Java
- Title
- spigot custom join message
- Category
- Java
- Title
- tostring() java example
- Category
- Java
- Title
- bubble sort in java
- Category
- Java
- Title
- loop array using stream java
- Category
- Java
- Title
- java implement interface
- Category
- Java
- Title
- string to double java
- Category
- Java
- Title
- null pointer exception java
- Category
- Java
- Title
- how to print to console in java
- Category
- Java
- Title
- java create folder
- Category
- Java
- Title
- java fx display simple rectangle
- Category
- Java
- Title
- how to parse a string into a number in java
- Category
- Java
- Title
- java get first char
- Category
- Java
- Title
- java int to int array
- Category
- Java
- Title
- java obtain list string from list object
- Category
- Java
- Title
- java 8 hashmap example stackoverflow
- Category
- Java
- Title
- copy array in java
- Category
- Java
- Title
- java 8 group a collections by 2 property
- Category
- Java
- Title
- java how to read a text file
- Category
- Java
- Title
- deleting elements of an array in java
- Category
- Java
- Title
- convert date to calendar java
- Category
- Java
- Title
- add video in bootstrap
- Category
- Java
- Title
- java how to compare strings
- Category
- Java
- Title
- set java
- Category
- Java
- Title
- nextchar in java
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- java syntax
- Category
- Java
- Title
- create java windows application
- Category
- Java
- Title
- map java
- Category
- Java
- Title
- how to do 4th root java
- Category
- Java
- Title
- how to run java program in linux server
- Category
- Java
- Title
- java iterate through hashmap
- Category
- Java
- Title
- how to make an array in java
- Category
- Java
- Title
- override java
- Category
- Java
- Title
- how to make a copy of an array java
- Category
- Java