0% found this document useful (0 votes)
9 views15 pages

Downcasting Programs

The document contains Java code defining various classes representing animals, people, machines, and devices, showcasing inheritance and polymorphism. It includes examples of classes like Animal, Horse, Person, Doctor, Machine, WashingMachine, Device, Printer, Bird, and Penguin, each with properties and methods. The code demonstrates object creation, method invocation, and downcasting in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
9 views15 pages

Downcasting Programs

The document contains Java code defining various classes representing animals, people, machines, and devices, showcasing inheritance and polymorphism. It includes examples of classes like Animal, Horse, Person, Doctor, Machine, WashingMachine, Device, Printer, Bird, and Penguin, each with properties and methods. The code demonstrates object creation, method invocation, and downcasting in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
1 v2 3 mn 5 6 vi 8 9 10 mT iy ar) 7 ir iT v/ aT} 19 oy) 21 22 23 oy pr 26 27 28 yA) 30 31 YA Sr 35 vi public class Animal { //Properties String name; String color; int noOfLegs; MC ile Colg public Animal(String name, String color, int noOfLegs) { rualomer-leeeoe trl [Link] = color; [Link] = noOfLegs; i Ato MLNS) Foye) o) TomVO(oRr-1 (OR § [Link]("Eating’); i public void sleep() { [Link]("Sleeping"); i } class Horse extends Animal { //Additional Properties String breed; int noOfTypes; // Constructor Horse(String name, String color, int noOfLegs, String breed, int noOfTypes) { super(name, color, noOfLegs); [Link] = breed; stalomale1@) BY 9am (eLO 1 B91 -1331 // Additional Behaviours public void canRun() { [Link]("Running’); public void canJump() { [Link]("Jumping"); } public static void main(String[] args) { Pz 25 26 va 28 29 30 a 32 33 SU) 35 36 vi 38 39 40 a 42 ce) 44 45 46 Lv ce] 49 Meola leicelg Horse(String name, String color, int noOfLegs, String breed, int noOfTypes) { super(name, color, noOfLegs); [Link] = breed; [Link] = noOfTypes; i // Additional Behaviours public void canRun() { [Link]("Running’); i public void canJump() { [Link]("Jumping’); i public static void main(String] args) { Animal a = new Horse("Horse", "Black", 4, "Arabian Horse", 400); if(a instanceof Horse){ //Downcasting Horse h=(Horse)a; [Link]([Link]); [Link]([Link]); [Link]([Link]); [Link](); [Link](); iol Black Arabian Horse Eating UIT aB aay 34 ace maT ToR| | 1 | public class Person { String name; String gender; AL :1 28 caret) Ui public Person(String name, String gender, int age) { i) SMT mL 9 [Link] = gender; 10 [Link] = age; can’ v4 K} public void canEat() { oe [Link]("Eating"); 15 16 public void canSleep() { 17 [Link]("Sleeping"); Te 19 |} 20 | class Doctor extends Person { 21 Ya String type; 23 int noOfTypes; 24 a Doctor(String name, String gender, int age, String type, int noOfTypes) { super(name, gender, age); RRS] eee oH i public void canCheck() { [Link]("Diagnosis"); public void canTreat() { [Link](‘Treating Patients"); i public static void main(String[] args) { Tee) MATIN Pele OWN, - ee Oe eS | Vx} int noO a ols // Constructor Doctor(String name, String gender, int age, String type, int noOfTypes) { super(name, gender, age); [Link] = type; // Additional Behaviours public void canCheck() { [Link]("Diagnosis"); i public void canTreat() { [Link]("Treating Patients"); i public static void main(String[] args) { // creating instance of Doctor Person p = new Doctor("Allen’, "Male", 34, "Neurologists", 2); if (p instanceof Doctor) { //Downcasting Doctor doctor=(Doctor)p; [Link]([Link]); [Link]. printIn([Link]); [Link]([Link]); [Link](); [Link](); = ays} Allen Male Neurologists heal 4 iat hehe ae ea) [Program finished] 1 | public class Machine { String name; 4 String color; 5 public }Machine(String name, String color) { TaN ALU om atl \o [Link] = color; i public void canUse() { [Link]("Using"); i public void onn() { [Link](‘turning on’); i 18 | class WashingMachine extends Machine { 20 String brand; 1 String material; Tale ano WashingMachine(String name, String color, String brand, String material, int price) { Eilers Grlmee (lai [Link] = brand; [Link] = material; [Link] = price; Y public void canWash() { [Link]("Washing’); } public void canDry() { [Link]("Drying’); i public static void main(String[] args) { Machine m = new WashingMachine(" U ee eS | ashingMacnine(string name, string Color, string brand, String material, int price) { super(name, color); [Link] = brand; [Link] = material; [Link] = price; } 30 | // Additional Behaviours public void canWash() { [Link]("Washing"); H public void canDry() { [Link]("Drying"); i public static void main(String[] args) { // creating instance of Washing Machine Machine m = new WashingMachine("Washing Machine", "Brown", "Voltas Beko", "Plastic", 25000); if (m instanceof WashingMachine) { //Downcastinlg WashingMachine wm = (WashingMachine)m; [Link]([Link]); [Link]([Link]); [Link]([Link]); [Link](); [Link](); € TAB Washing Machine STatel Ta} Voltas Beko dela aP ea) eels Washing [Program finished] 17 18 she) 20 21 YeA 23 PL) ya public class Device { String name; String color; public Device(String name, String color) { [Link] = name; [Link] = color; i public void onn() { [Link]("Switch on"); H public void canUse() { [Link]("Using "); H i class Printer extends Device { String brand; Tala nelela hh String type; Printer(String name, String color, String brand, int noOfTypes, String type) { super(name, color); [Link] = brand; [Link] = noOfTypes; TUS o1 eam oo public void canPrint() { [Link](’Printing’); i public void canScan() { [Link]("Scanning’); Ss 7 noOtTypes, String type super(name, color); HUalISM oN 1ale Ream el e-LaLeH [Link] = noOfTypes; IS AX em] oH // Additional Behaviours public void canPrint() { [Link]("Printing’); public void canScan() { [Link]("Scanning’); i public static void main(String[] args) { // creating instance of Printer Device d = new Printer("Printer", "Black", "Hp", 4, "Inkjet Printer’); if (d instanceof Printer) { Printer printer = ((Printer)d; [Link]([Link]); [Link]([Link]); [Link]([Link]); [Link]([Link]); aia Moat OF [Link](); eS ay\5) ete SaI-Tol a Lette de a) Teel a) ern ee aay -4 [Program finished] 1 v2 i) mt cs} 6 7 va 30 31 32 33 er 35 36 37 ts} public class Bird { SS) (ile mac lualon String color; int noOfWings; public Bird(String name, String color, int noOfWings) { [Link] = name; [Link] = color; [Link] = noOfWings; } public void eat() { SNC ROU ea =tcl new 8 i public void sleep() { [Link]("Sleeping’); } } class Penguin extends Bird { String ee int noOfTypes; Talal tia Penguin(String name, String color, int noOfWings, String breed, int noOfTypes, int height) { super(name, color, noOfWings); [Link] = breed; Sue rel@] MR 7am eel hoe [Link] = height; i public void canSwim() { [Link]("Swimming’); } public void canHunt() { [Link]("Hunting’); Ss +7 a} H/ SMCs 26 Penguin(String name, String color, int noOfWings, String breed, int noOfTypes, int height) { super(name, color, noOfWings); [Link] = breed; [Link] = noOfTypes; [Link] = height; // Additional Behaviours public void canSwim() { [Link]("Swimming"); i public void canHunt() { [Link]("Hunting’); i public static void main(String[] args) { // creating instance of Penguin Bird bird = new Penguin("Penguin’, "Black", 2, "King", 28, 3); if (bird instanceof Penguin) { //Downcasting Penguin penguin = (Penguin)bird; [Link]([Link]); [Link]([Link]); [Link]([Link]); [Link](); [Link](); Pea AUB) a Tol.g King ohana 4 STAUB 4 [Program finished]

You might also like