Explain subclasses and polymorphism, JAVA Programming

Assignment Help:

Explain Subclasses and Polymorphism ?

Car and Motorcycle are subclasses of MotorVehicle. If you instantiate a Car or a Motorcycle along with new, you can use in which object anywhere you can use a MotorVehicle, since cars are motor vehicles. As same you can use a Motorcycle anywhere you can use a MotorVehicle. This use of a subclass object in place of a superclass object is the starting of polymorphism. I'll say more about polymorphism later.

The converse is not true. While all cars are motor vehicles, not all motor vehicles are cars. A few are motorcycles. Thus if a method expects a Car object you shouldn't provides it a MotorVehicle object instead.

Remember that I said you shouldn't provide a method in which expects a Car a MotorVehicle. I didn't say you couldn't. Objects can be cast into their subclasses. This is useful while using data structures like Vectors that only handle generic objects. It's up to the programmer to remain track of what kind of object is stored in a Vector, and to use it accordingly.

The proper choice of classes and subclasses is a skill learned primarily by experience. There are often different ways to define classes.
toString() as example of polymorphism


Related Discussions:- Explain subclasses and polymorphism

Probability, Mike sells on the average 15 newspapers per week (Monday – Fri...

Mike sells on the average 15 newspapers per week (Monday – Friday). Find the probability that 2.1 In a given week he will sell all the newspapers [7] 2.2 In a given day he will se

Explain about the dynamic java, Dynamic JAVA Class and type informatio...

Dynamic JAVA Class and type information is kept around at runtime. This allows runtime loading and inspection of code in a very flexible way.

Minimum spanning tree based heuristic, Problem description: A travelling s...

Problem description: A travelling salesman wants to make a tour of the cities and returns back to the starting point. What is the minimum length tour? Formal Definiti

Program, Luminous Jewels - The Polishing Game Byteland county is very famo...

Luminous Jewels - The Polishing Game Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various lum

Explain java flow control, Explain java flow control ? if els...

Explain java flow control ? if else else if while for do while switch case break continue goto is a reserved word.

Program, write code employe managment

write code employe managment

What is the order of function invocation in an applet? , The Applet's life ...

The Applet's life cycle functions are as follows: public void init() : Initialization function called only once by the browser. public void start() : Method called after

Program, Write a program called Power that displays the positive powers of ...

Write a program called Power that displays the positive powers of 2. When the user enters the exponent at a prompt, the program displays 2 to that power. The program halts when the

How can you pass parameters in rmi?, RMI parameters : Primitive types ...

RMI parameters : Primitive types are given by value. 2. References to remote objects are given as remote references that allow the client process to call methods on the rem

Client for nokia symbian platform, Project Description: I need Team spea...

Project Description: I need Team speak 3 client for Nokia Symbian platform. Project for Nokia E72, but should work on all Java/Symbian mobiles. Feature list should include

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd