overriding
Hi javamates
i have this code not working
i simply am trying overriding of methods
i want printinfo method to work
in my mian method
h3elp me out
heres the code
class Employee12{
String name;
int empid;
float salary;
public Employee12(String n,int id,float sal){
name = n;
empid = id;
salary = sal;
}
public void printinfo(){
System.out.println(name);
System.out.println(empid);
System.out.println(salary);
}
class Salesemployee extends Employee12{
int commission;
public void printinfo(){
System.out.println(name);
System.out.println(empid);
System.out.println(salary);
System.out.println(commission);
}
}
class Devemployee extends Employee12{
String languages;
public Devemployee(String n,int id,float sal,String lang){
super(n,id,sal);
languages = lang;
}
public void printinfo(){
System.out.println(name);
System.out.println(empid);
System.out.println(salary);
System.out.println(languages);
}
}
}
class Testemployee12{
public static void main(String args[]){
Devemployee nag = new Devemployee();
nag.name = "nag";
nag.empid = 10;
nag.salary = 10000;
nag.commission = 20;
nag.languages ="JAVA";
nag.printinfo();
}
}
Hello,
Your code is not correct. When you post code can you use code tags as described here
http://forum.java.sun.com/help.jspa?sec=formatting
Below is an example of a Fruit class which has two implementations, Orange and Apple.
public class FruitTest {
public static void main(String[] args) {
//Declared as "Type" Fruit - The super class
Fruit f1 = new Apple("Apple");
Fruit f2 = new Orange("Orange");
//The value to print is chosen at runtime depending on the right hand value (Apple or Orange)
f1.printType(); //Prints Apple
f2.printType(); //Prints Orange
}
}
abstract class Fruit {
String type;
public Fruit(String type) {
this.type=type;
}
abstract void printType();
}
class Apple extends Fruit {
public Apple(String type) {
super(type);
}
@Override
void printType() {
System.out.println(type);
}
}
class Orange extends Fruit {
public Orange(String type) {
super(type);
}
@Override
void printType() {
System.out.println(type);
}
}
public class Employee12 {
private String name;
private int empid;
private float salary;
public Employee12(){
}
public Employee12(String n, int id, float sal) {
this.name = n;
this.empid = id;
this.salary = sal;
}
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public void printinfo() {
System.out.println(name);
System.out.println(empid);
System.out.println(salary);
}
class Salesemployee extends Employee12 {
private int commission;
public Salesemployee(){
super();
}
public void printinfo() {
System.out.println(name);
System.out.println(empid);
System.out.println(salary);
System.out.println(commission);
}
public int getCommission() {
return commission;
}
public void setCommission(int commission) {
this.commission = commission;
}
}
class Devemployee extends Employee12 {
private String languages;
public Devemployee(){}
public Devemployee(String n, int id, float sal, String lang) {
super(n, id, sal);
languages = lang;
}
public String getLanguages() {
return languages;
}
public void setLanguages(String languages) {
this.languages = languages;
}
public void printinfo() {
System.out.println(this.getName());
System.out.println(this.getEmpid());
System.out.println(this.getSalary());
System.out.println(this.getLanguages());
}
}
public void test(){
Devemployee nag = new Devemployee();
nag.setName("nag");
nag.setEmpid(10);
nag.setSalary(10000);
//nag.setCommission(20); //no commision attribute for this class. Do you mean Salesemployee object
nag.setLanguages("JAVA");
nag.printinfo();
}
public static void main(String args[]) {
Employee12 emp12 = new Employee12();
emp12.test();
}
}
Thank you for simply plopping your unworking homework assignment attempt onto a virtual helpdesk, and going into great detail about the problem ("... i have this code not working ... heres the code").
We'll get right on it.
lol, i think i did this same homework in my AP computer science class when i was in high school. Oh the memories.
> lol, i think i did this same homework in my AP
> computer science class when i was in high school. Oh
> the memories.
But not my memories. They involve using a slide rule :-)
Here's your HW and I'm in for my dukes :
class Employee12{
String name;
int empid;
float salary;
public Employee12(){}
public Employee12(String n,int id,float sal){
name = n;
empid = id;
salary = sal;
}
public void printinfo(){
System.out.println(name);
System.out.println(empid);
System.out.println(salary);
}
}
class Salesemployee extends Employee12 {
int commission;
public Salesemployee(){
}
public void printinfo(){
System.out.println(name);
System.out.println(empid);
System.out.println(salary);
System.out.println(commission);
}
}
class Devemployee extends Employee12 {
int commission;
String languages;
public Devemployee(String n,int id,float sal){
super(n,id,sal);
}
public void printinfo(){
System.out.println(name);
System.out.println(empid);
System.out.println(salary);
System.out.println(languages);
}
}
public class Testemployee12{
public static void main(String args[]){
Devemployee nag = new Devemployee("nag",10,10000);
nag.commission = 20;
nag.languages ="JAVA";
nag.printinfo();
}
}
What's with all the code duplication?
public void printinfo() {
super.printinfo();
// remove duplicate code
// System.out.println(name);
// System.out.println(empid);
// System.out.println(salary);
System.out.println(commission);
}