Question

In: Computer Science

Write a simple short Java program of your choice which illustrates inheritance, polymorphism and the use...

Write a simple short Java program of your choice which illustrates inheritance, polymorphism and the use of interfaces. The program should not take up more than half a page of size A4.

Solutions

Expert Solution

interface Sample
{
void show();
}
class A implements Sample
{
public void show()
{
System.out.println("A method");
}
}
class B extends A
{
public void show()
{
System.out.println("B method");
}
}
class C extends A
{
public void show()
{
System.out.println("C method");
}
}
class D extends A
{
public void show()
{
System.out.println("D method");
}
}
class E extends A
{
public void show()
{
System.out.println("E method");
}
}
class F extends A
{
public void show()
{
System.out.println("F method");
}
}
class DemoProgram
{
public static void main(String[] args)
{
Sample obj=new A();
obj.show(); //calls A show()
obj=new B();
obj.show(); //calls B show()
obj=new C();
obj.show();//calls C show()
obj=new D();
obj.show();//calls D show()
obj=new E();
obj.show();//calls D show()
obj=new F();
obj.show();//calls F show()
}
}

Output

A method
B method
C method
D method
E method
F method


Related Solutions

Write a short code segment which includes/illustrates the concepts of inheritance, overloading, and overriding in java language.
Write a short code segment which includes/illustrates the concepts of inheritance, overloading, and overriding in java language.
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface called EmployeeInfo with the following constant variables: FACULTY_MONTHLY_SALARY = 6000.00 STAFF_MONTHLY_HOURS_WORKED = 160 2. Implement an abstract class Employee with the following requirements: Attributes last name (String) first name (String) ID number (String) Sex - M or F Birth date - Use the Calendar Java class to create a date object Default argument constructor and argument constructors. Public methods toString - returning a string...
Which Feature of OOP illustrates code reusability? Abstraction, Encapsulation, Inheritance, Polymorphism or All of them.
Which Feature of OOP illustrates code reusability? Abstraction, Encapsulation, Inheritance, Polymorphism or All of them.
Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea...
Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea behind polymorphism is that several different types of objects can have the same methods, and be treated in the same way. For example, have a look at the code we’ve included for this problem. We’ve defined Shape as an abstract base class. It doesn’t provide any functionality by itself, but it does supply an interface (in the form of .area() and .vertices() methods) which...
Which diagram illustrates multiple inheritance?
Which diagram illustrates multiple inheritance? 
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the...
This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the following members: • A field for the name of the ship (a string). • A field for the year that the ship was built (a string). • A constructor and appropriate accessors and mutators. • A toString method that overrides the toString method in the Object class. The Ship class toString method should display the ship’s name and the year it was built. Design...
Write a simple java program that produces any text art ("ASCII art") picture. Your program can...
Write a simple java program that produces any text art ("ASCII art") picture. Your program can produce any picture you like, with the following restrictions: • The picture should consist of between 3 and 200 lines of output, with no more than 100 characters per line. • The code must use at least one for loop or static method but should not contain infinite loops.
in a gui ' in java write a program that draws equal a simple fence with...
in a gui ' in java write a program that draws equal a simple fence with vertical, spaced slats backed by two boards. Behind the fence show a simple house support Make sure the in the und. house is visible between the slats in the fence.
Write a short, cohesive java program in jGrasp that interacts with the user and does the...
Write a short, cohesive java program in jGrasp that interacts with the user and does the following: create at least 2 double variables create at least 1 constant get at least 1 string input from the user get at least 1 int/double input from the user include both, incorporating the input you got from the user: 1 multiway if-else with at least 3 "else if" 1 nested if Your program should combine all of these into one cohesive, interactive program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT