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 :: Card view only top corner radius 
Java :: determine if a number is factorial in Java 
Java :: android bottomnav fab 
Java :: java break string at comma 
Java :: java string strip 
Java :: linear serach in java 
Java :: java timeout exception 
Java :: FlutterFirebaseCorePlugin.java uses or overrides a deprecated API. 
Java :: Salary example in method in java 
Java :: convert string to boolean java 
Java :: down casting java 
Java :: java final variable 
Java :: lcm of two number in java 
Java :: search in rotated sorted array leetcode 
Java :: what is ioexception in java 
Java :: unresolved reference activity_main 
Java :: how to use java 
Java :: format specifier in java 
Java :: java byte data type 
Java :: spring mvc 
Java :: advantages of using java 
Java :: instanceof java 
Java :: vim yank to clipboard 
Java :: android java how to stop users fromgoing back too much 
Java :: intellij error for new project 
Java :: Does JVM create object of Main class (the class with main())? 
Java :: POM error: Failure to find org.springframework.boot 
Java :: How to define lambda expression in Java? 
Java :: how to solve CopyBuffer from HiLo failed, no data 
Java :: Declaration of java list 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =