Question

JAVA CODE

Create a Java class with two subclasses (children). Each class should have at least one attribute and a method. Parent class should have the attribute/method that is common to both children and children should have the attribute/method that is unique to what they represent. Write the main method to demonstrate the correct functionality of the code and to demonstrate that the children inherit from the parent class. 

0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

//This is ParentClass.java class 
public class ParentClass {
//This is the attribute of parent class
int p=10;

//This is the printValue() method, it is available in ParentClass
public void printValue() {
 System.out.println("This is the parent class, the value of p is :: "+p);
}//printValue()

}//ParentClass(end of the class)

 

 

 

//This is FirstChildClass.java class that extends ParentClass
public class FirstChildClass extends ParentClass{

//This is the attribute of FirstChildClass class
int c1=20;

//This is the printValue() method, it is available in FirstChildClass
public void printValue() {
 System.out.println("This is the FirstChild class, the value of c1 is :: "+c1);
}//printValue()

}//FirstChildClass(end of class)
 

 

 

 

//This is SecondChildClass.java class that extends ParentClass
public class SecondChildClass extends ParentClass{

//This is the attribute of SecondChildClass class
int c2=30;

//This is the printValue() method, it is available in SecondChildClass
public  void printValue() {
 System.out.println("This is the SecondChild class, the value of c2 is :: "+c2);
}//printValue()
 
}//SecondChildClass(end of class)
 

 

 

 

 

//This is TestParentChildRelation.java class
//This class is responsible to test ParentClass, FirstChildClass and SecondChildClass
public class TestParentChildRelation {

// This is main() method, program execution start here
public static void main(String[] args) {
 //This is the object of Parent class
 ParentClass pc=new ParentClass();
 pc.printValue();
 
 //This is the object of FirstChildClass, that hold ParentClass reference variable(This is possible only inheritance)
 ParentClass pcc1=new FirstChildClass();
 pcc1.printValue();

 //This is the object of SecondChildClass, that hold ParentClass reference variable(This is possible only inheritance)
 ParentClass pcc2=new SecondChildClass();
 pcc2.printValue();
 
}// main() method

}// TestParentChildRelation(end of class)

 

answered by: anonymous
Add a comment
Know the answer?
Add Answer to:
JAVA CODE
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • I want to write a superclass with one private attribute and one method in JAVA The super class wi...

    I want to write a superclass with one private attribute and one method in JAVA The super class will later derive into 2 subclasses each with its own private attributes and each with a method that OVERRIDES the superclass method with each subclasses with one method ( that will do something) unique to that subclass. ALL classes must have constructors initializing its attributes and getter/setter methods. Then, in the main method,  keep initializing objects of type superclasses based on user's...

  • Use Java and please try and show how to do each section. You are creating a 'virtual pet' program...

    Use Java and please try and show how to do each section. You are creating a 'virtual pet' program. The pet object will have a number of attributes, representing the state of the pet. You will need to create some entity to represent attributes in general, and you will also need to create some specific attributes. You will then create a generic pet class (or interface) which has these specific attributes. Finally you will make at least one subclass of...

  • JAVA CODE Provide Java code for a simple class of your choice. Be sure to include...

    JAVA CODE Provide Java code for a simple class of your choice. Be sure to include at least one constructor, two methods and two fields. The fields should be private. Create a test class to constuct and call the methods of your class. Describe your class and demonstrate your code functions properly.

  • Question 3 (CLOS: 3.5) Marks: 4+4 = 8 (a) Write a code in java to make...

    Question 3 (CLOS: 3.5) Marks: 4+4 = 8 (a) Write a code in java to make a class called Animal. The class has data attributes called name. Make one constructor which initializes value of name attribute (b) Make a class called Cat and make it children of Animal class. Declare a variable of the class called age. Make a constructor of this class which initializes both age and name of the class. Make a main method in the class and...

  • Create a class Parent that has a method speaks() which shall display “Parents speaks” . Create...

    Create a class Parent that has a method speaks() which shall display “Parents speaks” . Create another class named Child that has a method sing() which shall display “ children also sing well”. Now, the Child class shall inherit the parent class. Write the Demo class to demonstrate the capabilities of the child class. (that is, create object of the Child class and call both the methods.)

  • Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in thr...

    Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in three dimensions labeled x, y and z. You will then use the class to perform some calculations on an array of these points. You need to draw a UML diagram for the class (Point3D) and then implement the class The Point3D class will have the...

  • UML Class Diagram with Inheritance Objectives Use UML Correctly indicate inheritance Demonstrate permissions Understand inheritance relationships...

    UML Class Diagram with Inheritance Objectives Use UML Correctly indicate inheritance Demonstrate permissions Understand inheritance relationships Labwork Please read all of the directions carefully. You will create a UML class diagram reflecting the class hierarchy for a fictional program that manages university personnel as constructed according to the graph displayed below. You will need to think about the attributes and behaviors that are unique to each class, and also those attributes and behaviors that are common amongst the subclasses and...

  • THIS IS IN THE JAVA SCRIPT CODE ALSO PLEASE SEND ME THE CODE TYPED THANK YOU....

    THIS IS IN THE JAVA SCRIPT CODE ALSO PLEASE SEND ME THE CODE TYPED THANK YOU. Program 1 Write an inheritance hierarchy of three-dimensional shapes. Make a top-level shape interface that has methods for getting information such as the volume and the surface area of a three-dimensional shape. Then make classes and subclasses that implement various shapes such as cube, cylinders and spheres. Place common behavior in superclasses whenever possible, and use abstract classes as appropriate. Add methods to the...

  • Java — write code to represent the following classes in the order asked. Use comparable —>compateTo(),...

    Java — write code to represent the following classes in the order asked. Use comparable —>compateTo(), toString() and create a constrcutor. Follow the question outline please 62. 6S points) Writehfl Jaoe for a clss below that represents an aple where the following things Apples have a weight and a brand and cannot be created without these. are important to represent: Apples havs-natural ordering by weight, brand is not important). When an apple is printed, it should display all the properties...

  • What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that...

    What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that derives from a superclas:s ■ Demon "Declare a variable of the superclass type and assign it an instance of the subclass type strate polymorphic behavior Access the public members of the superclass type Notice how the overridden versions of the subclass type are called Notice how the subclass specific members are inaccessible "Create an array of superclass type and use a foreach loop to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT