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

Convert US Dollars to Canadian Dollars, How to write a program that convert...

How to write a program that converts US dollars into Canadian dollars.The program let the user enter an amount in US dollars and display it equivalent value in Canadian dollars whe

Write a java applet using swing components, The project is optional. It is ...

The project is optional. It is worth at least 7 bonus points beyond overall 100 points of this course. The grading will be on a pass/fail basis. Write a Java application (not ap

Determine the phase loading- java program development, Determine the phase ...

Determine the phase Loading- Java Program Development In phase 3, the program must first be placed in memory before it can be executed. This is done by the class loader, whic

What is dynaactionform, A specialized subclass of ActionForm that permits t...

A specialized subclass of ActionForm that permits the creation of form beans with dynamic sets of properties (configured in configuration file), without requiring the developer to

Java , what is thread synchronization

what is thread synchronization

Programming, write a Java program that accept two integers and determine we...

write a Java program that accept two integers and determine weather they are Coprime or not

What is spreadsheet and list any two jobs that it can do, What is spreadshe...

What is spreadsheet? List any two jobs that it can do? A spreadsheet is a grid which organizes data within columns and rows. Spreadsheets form it simple to display information,

Sudoku class used in java program, Sudoku Class (Simple Version) The ...

Sudoku Class (Simple Version) The Sudoku class will encapsulate the minimum necessary data and logic to manipulate, print, and set the SudCells , in anticipation of a high

Package, how to write bank system

how to write bank system

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