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? 
OOP involves use of the following concepts: inheritance, polymorphism, and encapsulation. What are your definitions of...
OOP involves use of the following concepts: inheritance, polymorphism, and encapsulation. What are your definitions of these terms? Why are they important? Does a program necessarily suffer if one or more of these items are absent?
Write a java program (use value returning method) that gives simple math quizzes. The program should...
Write a java program (use value returning method) that gives simple math quizzes. The program should display two random integers from 1 to 100 that are to be added, such as:    47 + 29 The program allows the user to enter the answer. If the answer is correct, display “congratulations”. If the answer is incorrect, the program should show the correct answer. Your result should look like, for example:    47 + 29 Enter your answer: 1 Wrong, the right answer...
In this program we are going to utilize multiple classes to demonstrate inheritance and polymorphism. We...
In this program we are going to utilize multiple classes to demonstrate inheritance and polymorphism. We will be creating a base class for our game character. The base class will be LifeForm. We will have derived classes of Human, Dragon, and Unicorn. LifeForm will have the following attributes: hitPoints – range 0 to 100 strength – range 0 to 18 You will need to provide a default constructor that initializes these attributes as follows: strength – 15 hitPoints – 100...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT