In javaWrite the definition of a class Person that contains

In java,Write the definition of a class “Person” that contains a “name” field of type String and an “age” field of type int. Then define two classes, “Dancer” and “DonutMaker”,that extend “Person” such that “Dancer” has an additional field called “company” of type String, and “DonutMaker” has an additional field called “shop” of type String.

Solution

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

public class Person {
  
private String name;
private int age;
  
public Person(String name,int age)
{
this.age = age;
this.name = name;
}
  
public String toString()
{
return \"Name : \" + this.name + \", Age : \"+this.age;
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

public class Dancer extends Person {
  
private String company;
  
public Dancer(String name,int age,String company)
{
super(name,age);
this.company = company;
}
  
public String toString()
{
return super.toString()+\", company : \" + this.company;
}
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

public class DonutMaker extends Person {
  
private String shop;
  
public DonutMaker(String name,int age,String shop)
{
super(name,age);
this.shop = shop;
}
  
public String toString()
{
return super.toString()+\", shop : \" + this.shop;
}
  
}

In java,Write the definition of a class “Person” that contains a “name” field of type String and an “age” field of type int. Then define two classes, “Dancer” a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site