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();
}
}
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();
}
}