|
| 1 | +package vid24oops; |
| 2 | + |
| 3 | +//Method Overloading or Run time Polymorphism (Functions can be called as per required in any type of Polymorphism) |
| 4 | +// class Student{ |
| 5 | +// String name; |
| 6 | +// int age; |
| 7 | + |
| 8 | +// public void displayInfo(String name) { |
| 9 | +// System.out.println(name); |
| 10 | +// } |
| 11 | + |
| 12 | +// public void displayInfo(int age) { |
| 13 | +// System.out.println(age); |
| 14 | +// } |
| 15 | + |
| 16 | +// public void displayInfo(String name, int age) { |
| 17 | +// System.out.println(name); |
| 18 | +// System.out.println(age); |
| 19 | +// } |
| 20 | +// } |
| 21 | + |
| 22 | +// public class polymorphism { |
| 23 | +// public static void main(String[] args) { |
| 24 | +// Student s1 = new Student(); |
| 25 | + |
| 26 | +// s1.name = "Adi"; |
| 27 | +// s1.age= 21; |
| 28 | + |
| 29 | +// s1.displayInfo(s1.name); //If we want only name to display |
| 30 | + |
| 31 | +// s1.displayInfo(s1.age); //If we want only age to display |
| 32 | + |
| 33 | +// s1.displayInfo(s1.name,s1.age); //If we want name and age to display |
| 34 | +// } |
| 35 | +// } |
| 36 | + |
| 37 | +//Run time polymorphism |
| 38 | +// abstract class Animal { |
| 39 | +// abstract void walk(); |
| 40 | +// void breathe() { |
| 41 | +// System.out.println("This animal breathes air"); |
| 42 | +// } |
| 43 | + |
| 44 | +// Animal() { |
| 45 | +// System.out.println("You are about to create an Animal."); |
| 46 | +// } |
| 47 | +// } |
| 48 | + |
| 49 | +// class Horse extends Animal { |
| 50 | +// void walk() { |
| 51 | +// System.out.println("Horse walks on 4 legs"); |
| 52 | +// } |
| 53 | +// } |
| 54 | + |
| 55 | +// class Chicken extends Animal { |
| 56 | +// void walk() { |
| 57 | +// System.out.println("Chicken walks on 2 legs"); |
| 58 | +// } |
| 59 | +// } |
| 60 | + |
| 61 | +// public class polymorphism { |
| 62 | +// public static void main(String args[]) { |
| 63 | +// Horse horse = new Horse(); |
| 64 | +// horse.walk(); |
| 65 | +// horse.breathe(); |
| 66 | + |
| 67 | +// Animal animal = new Animal(); |
| 68 | +// animal.walk(); //Gives error when you run, but not when you compile |
| 69 | +// } |
| 70 | +// } |
0 commit comments