Share on Google+Share on Google+

santhiya
javacode
1 Answer(s)      6 years and 8 months ago
Posted in : Java Beginners
how to use inherritance using switch case.if suppose i use two classes named customer details and student details and i want to call either one at a time so how to use switch case in these type of situation.
help me out please

Ads

View Answers

September 2, 2010 at 12:34 PM


I am trying to answer you....

class Person{ //this is the base class
String name;
String type;
public Person(String name,String type){
this.name=name;
this.type=type;
}
public String getType(){
return this.type;
}
}

class Customer extends Person{ //first derived class
public Customer(String name){
super(name,"customer");
}
}

class Student extends Person{ //second derived class
public Student (String name){
super(name,"student");
}
}

class Test{

private Person getInstance(String name,String type){
Person person;
switch(type){
case("customer"): person=new Customer(name);
break;
case("student"): person=new Student(name);
break;
}
return person;
}
public static void main(String args[]){

}
}




Now call the method getInstance() and on the returned object call the method getType().U will get the type of object returned...









Related Tutorials/Questions & Answers:
usb detection
usb detection  i want to detect usb device using javacode
Advertisements
hai - Java Beginners
(file[0].getAbsoluteFile()); javacode......... } Error
struts - Struts
to write and how to get the attributes from java(means how to write javacode
struts - Struts
to write and how to get the attributes from java(means how to write javacode
java - SQL
javacode; import java.sql.*; import java.io.*; public class InsertImage... = con.createStatement(); File imgfile = new File("javacode/rose.jpg
java - JDBC
. package javacode; import java.sql.*; import java.sql.Connection; import... res = null; StringBuffer query=null; String filename = "javacode/rose.jpg... FileOutputStream("javacode/rose.jpg"); byte b[]= new byte[size]; x.read(b
Session Tracking in servlet - Servlet Interview Questions
rewritting: package javacode; import javax.servlet.*; import... -----------------------------------------------  Hidden Form Fields: package javacode; import java.io....   Cookies: package javacode; import java.io.*; import
JAVA(JDBC) - JDBC
javacode; import java.sql.*; import java.sql.Connection; import... = null; StringBuffer query=null; String filename = "javacode/rose.jpg... FileOutputStream("javacode/rose.jpg"); byte b[]= new byte[size]; x.read(b
Exception: java.lang.IllegalStateException: getWriter() has already been called for this response - JSP-Servlet
, package javacode; import java.sql.*; import java.sql.Connection; import...; ResultSet res = null; StringBuffer query=null; String filename = "javacode...(); OutputStream out=new FileOutputStream("javacode/rose.jpg"); byte b[]= new byte[size
thumbnails - JSP-Servlet
thumbnails  how to create thumbnails using servlets?  Hi friend, package javacode; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.awt.*; import
JSP-EL - JSP-Servlet
} -------------------------------------- JavaBeans.java package javacode; public class JavaBeans
Session ID - Java Beginners
Session ID  Do we get new session id for a new domain after clicking a page link.  Hi friend, package javacode; import java.io....); } } ----------------------------------------------------   package javacode; import
servlets - Servlet Interview Questions
javacode; import... package javacode; import javax.servlet.*; import javax.servlet.http.
Computer - JSP-Interview Questions
for this   hi friend, package javacode; import java.io....); } } } ------------------------------------------------- package javacode; import
store the image in the oracle using blob and get it back - JDBC
jdbc connection, package javacode; import java.sql.*; import java.io....("javacode/rose.jpg"); FileInputStream fin = new FileInputStream(imgfile... image code package javacode; import java.sql.*; import java.sql.Connection
JSP-EL - JSP-Servlet
package javacode; public class JavaBeans{ private String name
java - JSP-Servlet
. --------------------------------------------- Here is the simple code. package javacode; import java.io.*; import
java - Java Beginners
java  hello,i work on one project automated placement cell and i need to view and print the resume of student/employee in pdf format .i need a help how to generate .pdf file format using javacode...its a window based application
JSP-EL - JSP-Servlet
} -------------------------------------- JavaBeans.java package javacode; public
JSP-EL - JSP-Servlet
package javacode; public class JavaBeans{ private String name=""; public
servlets - JSP-Servlet
servlets  How would you set an error message in the servlet,and send the user back to the JSP page?Please give java or pseudo code examples.   Hi package javacode; import java.io.*; import javax.servlet.
java - JDBC
java  How to insert and retrieve image from oracle database? PLZ,,,explain with an example code? help,me plz....  Hi friend, package javacode; import java.io.*; import java.io.File; import
Insert Image In DB through Servlet - JSP-Servlet
: package javacode; import java.sql.*; import java.sql.Connection; import...; ResultSet res = null; StringBuffer query=null; String filename = "javacode/rose.jpg...=new FileOutputStream("javacode/rose.jpg"); byte b[]= new byte[size
Insert Image in DB through Servlet - JSP-Servlet
Thanks Rajanikant Hi vijay, This is Servlet code: package javacode...; StringBuffer query=null; String filename = "javacode/rose.jpg"; String...=x.available(); OutputStream out=new FileOutputStream("javacode/rose.jpg"); byte b
Problem in Servlet - Servlet Interview Questions
error.  Hi friend, package javacode; import java.io.*; import...); } } -------------------------------  package javacode; import javax.servlet.
web-design - JSP-Servlet
web-design  hi, i wanted to know what is web design.. is it the servlet that we write producing the output on the web page????????  Hi package javacode; import java.io.*; import javax.servlet.*; import
Hi ..I am Sakthi.. - Java Beginners
;Hi friend, package javacode; import javax.swing.*; import
dynamic drop down list box - Java Beginners
;servlets code package javacode; import java.io.*; import javax.servlet.
jsp/servlet - JSP-Servlet
javacode; import java.io.*; import java.io.PrintWriter; import java.sql.
Refresh code - Java Server Faces Questions
----   servlets code package javacode; import
Tutorials   
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized

Ads

 
Advertisement null

Ads