- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to create String to super class type mapping
Here, we have a superclass Vehicle and within that some subclasses −
class Vehicle { } class Motorcycle extends Vehicle { } class Bus extends Vehicle { } class Car extends Vehicle { }
Now, we will create some strings for mapping with super class type −
Map<String, Vehicle>map = new HashMap<String, Vehicle>(); map.put("motorcycle", new Motorcycle()); map.put("bus", new Bus()); map.put("car", new Car());
Example
import java.util.HashMap; import java.util.Map; class Vehicle { } class Motorcycle extends Vehicle { } class Bus extends Vehicle { } class Car extends Vehicle { } public class Demo { public static void main(String... args) { Map<String, Vehicle>map = new HashMap<String, Vehicle>(); map.put("motorcycle", new Motorcycle()); map.put("bus", new Bus()); map.put("car", new Car()); System.out.println(map); } }
Output
{bus=my.Bus@97e1986, car=my.Car@26f67b76, motorcycle=my.Motorcycle@153f5a29}
- Related Articles
- How to convert a sub class variable into a super class type in Java?
- How to convert a super class variable into a sub class type in Java
- Java Program to create a BigDecimal from a string type value
- Golang program to show use of super keyword in class
- How to ensure that child class overrides a super class method in java?
- What is the super class of every class in Java?
- Java Program to convert a Primitive Type Value to a String
- Java Program to convert a String to a float type Number
- Mapping string to Numerals in JavaScript
- Get super class of an object in Java
- Java Program to create a boolean variable from string
- Java Program to create Character Array from String Objects
- Golang Program to Create Abstract Class
- Why Object class is the super class for all classes in Java?
- Java Program to Create String from Contents of a File

Advertisements