Write an application named UseChildren that creates and disp
Write an application named UseChildren that creates and displays at least two Child objects—one Male and one Female. Child is an abstract class and Male and Female are subclasses. The Child class contains fields that hold the name, gender, and age of a child. The Child class constructor requires a name and a gender. The Child class also contains two abstract methods named setAge() and display(). The Male and Female subclass constructors require only a name; they pass the name and appropriate gender to the Child. The subclass constructors also prompt the user for an age using the setAge() method, and then display the Child’s data using the display() method. Save the files as Child.java, Male.java, Female.java, and UseChildren.java.
Solution
//content of UseChildren.java
public class UseChildren.java{
public static void main(String []args){
Child c=new Male(\"Anshu\"); //calling male constructor passing Anshu as a name
Child d=new Female(\"Anurag\"); //calling female constructor passing Anurag as a name
}
}
//end of UseChildren.java
//content of Child.java
//abstract class child
public abstract class Child{
String name; //name variable
String gender; //gender variable
int age; //age variable
//constructor
Child(String name,String gender){
this.name=name;
this.gender=gender;
}
//abstract methods
abstract void setAge();
abstract void display();
}
//end of Child.java
//content of Male.java
import java.util.Scanner;
//male class extending child class
public class Male extends Child{
String name; //name variable
String gender; //gender variable
int age; //age variable
//constructor passing name only
Male(String name){
super(name,\"male\"); //calling parent class constructor
this.name=name;
this.gender=\"male\";
this.setAge(); //calling setAge method
this.display(); //calling display method
}
//setAge method implementation
void setAge(){
int age;
Scanner input=new Scanner(System.in);
//prompting the user to enter the age
System.out.println(\"Enter the age of the child :\");
this.age=input.nextInt();
}
//display method implementation
void display(){
System.out.println(\"Child is a Male, his name is : \"+name+\" and age is : \"+age);
}
}
//end of Male.java
//content of Female.java
import java.util.Scanner;
//female class extending child class
public class Female extends Child{
String name; //name variable
String gender; //gender variable
int age; //age variable
//constructor passing name only
Female(String name){
super(name,\"female\"); //calling parent class constructor
this.name=name;
this.gender=\"female\";
this.setAge(); //calling setAge method
this.display(); //calling display method
}
//setAge method implementation
void setAge(){
int age;
Scanner input=new Scanner(System.in);
//prompting the user to enter the age
System.out.println(\"Enter the age of the child :\");
this.age=input.nextInt();
}
//display method implementation
void display(){
System.out.println(\"Child is a Female, her name is : \"+name+\" and age is : \"+age);
}
}
//end of Female.java
//OUTPUT*********
Enter the age of the child :
25
Child is a Male, his name is : Anshu and age is : 25
Enter the age of the child :
22
Child is a Female, her name is : Anurag and age is : 22
//OUTPUT*********
//This code has been tested on eclipse,please ask in case of any doubt,Thanks.

