Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. The following program shows example of abstract class and abstract method. a. Write, compile and run the following program of three classes. Give

imageimageimage

1. The following program shows example of abstract class and abstract method. a. Write, compile and run the following program of three classes. Give the output. abstract class Card String recipient; public abstract void greeting(); class Holiday extends Card public Holiday String ) ( recipient } public void greeting() System.out.println("Dear + recipient + " "); System.out.println("Season's Greetings! "); public class CardTester f public static void main(String[] args) ( } Holiday hol new Holiday ("Santa"); hol.greeting(); b. Add Birthday class that inherits from abstract Card class as following skeleton. public class CardTester public static void main(String[] args) f Card card new Holiday( "Amy" ); card. greeting(); //Invoke a Holiday greeting() card=new Birthday( "Cindy", 17 ); card. greeting(); //Invoke a Birthday greeting () } c. Instantiate a Birthday object and assign to greeting() method in the Card Tester class as following code: Birthday bd = new Birthday( "Maria", 21 ); bd.greeting(); d. Compile and Run. Give the output. e. Rewrite your CardTester class with below program. class f int age; extends public { recipient = 1; age = years; } (String r, int years ) public void greeting () { } System.out.println("Dear System.out.println("Happy " + recipient + ", "); + "th Birthday "); 2. The following program demonstrates polymorphic behavior. Write, compile and run the following program. Give the output. import java.io.*; import java.awt.*; public abstract class Animal { private String name; public Animal (String nm) { name nm; } } public String getName() { } return(name); public abstract void speak(); public class Dog extends Animal { public Dog (String nm) super (nm); } public void speak () System.out.println("Woof"); } public class Cow extends Animal { public Cow (String nm) } super (nm); } public void speak() System.out.println("Moo"); } public class VariousAnimal { { public static void main(String[] args) Animal ref; Dog aDog = new Dog ("Lassie"); Cow aCow = new Cow ("Mabel"); ref=aCow; ref. speak(); ref=aDog; ref. speak(); Explain the polymorphic behavior. 3. Extend the program in exercise 2 by adding two more subclasses of Animal i.e. Snake and Cat where each has a method speak() which prints "Sss" and "Meow" respectively. 4. Modify the extended program created in Exercise 3 in this lab by creating an array of superclass objects and manipulate it by invoking the appropriate method for each subclass member to print the sound of each animal eg. dog, cat, snake and cow.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OCP Oracle Certified Professional Java SE 11 Developer Practice Tests Exam 1Z0 819 And Upgrade Exam 1Z0 817

Authors: Scott Selikoff, Jeanne Boyarsky

1st Edition

1119696135, 978-1119696131

More Books

Students also viewed these Programming questions