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 :: remove part of string java 
Java :: if statement with string java 
Java :: how to make a for loop increment by 2 in java 
Java :: += in java 
Java :: okhttp3, android okhttp 
Java :: class syntax in java 
Java :: java join list 
Java :: print treeset java 
Java :: junit maven dependency 
Java :: find power of number in method java 
Java :: java stream group by multiple fields 
Java :: java float precision 
Java :: extract substring in string java 
Java :: code to get date and time in android 
Java :: postfix operator in java 
Java :: how to convert integer to list in python 
Java :: array srting line by line in textview android 
Java :: Fab to CircularRevealFrameLayout example 
Sql :: delete mysql ubuntu 20.04 
Sql :: cannot truncate a table referenced in a foreign key constraint 
Sql :: mysql get date difference in hours 
Sql :: uninstall mysql on ubuntu 
Sql :: select duplicates in sql 
Sql :: sql server get users 
Sql :: start mysql server linux terminal 
Sql :: pdo mysql insert 
Sql :: tsql merge example 
Sql :: psql filed name alter 
Sql :: dump mysql 
Sql :: mariadb get column names from table 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =