Question

In: Computer Science

Based on what you know about object oriented programming, inheritance, and polymorphism, why do you think...

Based on what you know about object oriented programming, inheritance, and polymorphism, why do you think it is not possible to write code like this in Java:

public class X extends Y, Z {
  // ...
}

...but can write code like this:

public class A implements B, C {
  //...
}

Solutions

Expert Solution

Java doesn't support Multiple inheritance, to get around this the concept of Interfaces is used.

Java supports a limited form of multiple inheritance.

Java has a rule that a class can extend only one abstract class, but can implement multiple interfaces (fully abstract classes)

In Java A class can extend at most one abstract class, but may implement many interfaces.

Abstract class = a class that contains at least one abstract method, and can also contain concrete (implemented) methods

Interface = a class that is fully abstract — it has abstract methods, but no concrete methods

Explanation:

In java a class can be an abstract class without being a fully abstract class. It can be a partially abstract class.

Imagine that that we have two partially abstract classes A and B. Both have some abstract methods, and both contain a non-abstract method called foo().

Also imagine that Java allows a class to extend more than one abstract class, so we can write a class C that extends both A and B. And imagine that C doesn’t implement foo().

So now there is a problem. Suppose we create an instance of C and invoke its foo() method. Which foo() should Java invoke? A.foo() or B.foo()?


This is a form of limited multiple inheritance. Basically, the rule says that you can inherit from (extend) as many classes as you want, but if you do, only one of those classes can contain concrete (implemented) methods.

Some languages allow multiple inheritance like C++ and python , Java has limitation.


Related Solutions

Primary Concepts: Priority Queues and Polymorphism Primary Concepts Priority Queues Object-Oriented Frameworks Inheritance and Polymorphism Please...
Primary Concepts: Priority Queues and Polymorphism Primary Concepts Priority Queues Object-Oriented Frameworks Inheritance and Polymorphism Please read this Specification Document Corresponding Discussion Forum Important Design Requirement Your design must be based on Modularity and Separation of Concerns. Remember that interfaces represent behavior, while classes represent implementation. The Priority Queue Data Structure and the Simulation Framework implementations must be based on "Information Hiding" and "Encapsulation". The Software Gurus Bar is a client of the Simulation Framework. The Simulation Framework is a...
Throughout this course, you will be learning about object-oriented programming and demonstrating what you learn by...
Throughout this course, you will be learning about object-oriented programming and demonstrating what you learn by writing some programs in Java. The first step will be to install and integrated development environment (IDE) that will be where you will write and compile your programs. You will also write your first program using Java to show that you have correctly installed the IDE. The project instructions and deliverables are as follows: Download and install Java JDK and NetBeans IDE using the...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses? Explain. -What is a class diagram? How is it used in object-oriented programming? -What is an attribute in OOP? What is a data member? -What is a method in OOP? What is a member function? -What is the difference between private members and public members of a...
i need a full explanation in details Object Oriented Programming Principles Principles Polymorphism Inheritence Encapsulation Abstraction...
i need a full explanation in details Object Oriented Programming Principles Principles Polymorphism Inheritence Encapsulation Abstraction Class Object Method Message Passing
Based on what you know about symbolic interactionism and feminist theory, what do you think proponents...
Based on what you know about symbolic interactionism and feminist theory, what do you think proponents of those theories see as the role of the school? What are some ways that a conflict theorist would say that educational institutions perpetuate class differences?
Class object in C++ programming language description about lesson inheritance example.
Class object in C++ programming language description about lesson inheritance example.
Class object in C++ programming language description about lesson inheritance example.
Class object in C++ programming language description about lesson inheritance example.
Why is it more feasible to use Objects and object oriented programming as compared to using...
Why is it more feasible to use Objects and object oriented programming as compared to using method based programs? What are the disadvantages of using only methods in your programs.
OBJECT ORIENTED PROGRAMMING Design two grid based games or two block based games in c++ In...
OBJECT ORIENTED PROGRAMMING Design two grid based games or two block based games in c++ In some cases, the bulk of the project lies in producing a nice user interface, probably using the FLTK graphical library, while the algorithmic content is quite simple. In other cases, the bulk of the work is in devising and implementing the algorithms. Some projects are more difficult than others, but a good policy is to choose one which allows extensibility if you have more...
What are design patterns in object oriented programming? Minimum 200 words
What are design patterns in object oriented programming? Minimum 200 words
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT