Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Java Polymorphism

class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {					//extends Animal
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {					//extends Animal
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

class Main {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();  // Create a Animal object
    Animal myPig = new Pig();  // Create a Pig object
    Animal myDog = new Dog();  // Create a Dog object
    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}
Comment

java polymorphism nedir

class Canli {

    public void sesCikar() {

        System.out.println("ses çıkarma");
    }

}

class Insan extends Canli {
    
    @Override
    public void sesCikar() {

        System.out.println("konuşma");
    }
}

class Hayvan extends Canli {

    @Override
    public void sesCikar() {

        System.out.println("bağırma");
    }
}

public class Example {

    public static void main(String[] args) {

        Insan insan1 = new Insan();
        Hayvan hayvan1 = new Hayvan();
        Canli canli2 = new Canli();

        // Upcasting
        Canli canli1 = insan1;
        canli1.sesCikar();
        // Downcasting
        Insan insan2 = (Insan) canli1;
        insan2.sesCikar();

        // Polymorphism
        polymorphism(canli1);
        polymorphism(insan2);
        polymorphism(hayvan1);
        polymorphism(canli2);

    }

    public static void polymorphism(Canli c) {
        
        c.sesCikar();
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java map keyset to list 
Java :: char array to string in java 
Java :: java hash map 
Java :: launch java batch 
Java :: java anonymous class 
Java :: how to output a list in java 
Java :: java create arraylist with size 
Java :: java remove item from list 
Java :: java download for windows 10 
Java :: abstract method declaration 
Java :: java checked exception 
Java :: declare function in java 
Java :: method parameters in java 
Java :: app "restart" the home activity (and dismiss all other activities). 
Java :: how to get orientation lock to portrait android stackoverflow 
Java :: an internal error occurred during initializing java tooling . java.lang.nullpointerexception 
Java :: java print 2d array row and column 
Java :: java mostrar resultados ventana grafica 
Java :: linked list vs vector 
Java :: string to char 
Java :: int[] java 
Java :: java scanner int to string 
Java :: module java.base does not "opens java.io" to unnamed module 
Java :: how to initialize main in java 
Java :: java local method 
Java :: java exponencial 
Java :: update in spring boot 
Java :: Create EntityManager Hibernate 
Java :: java try...catch 
Java :: java instanceof operator 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =