Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Super confused, need help with tutorial. I cant sample run this because I keep getting a error and don't know why. Lab/tutorial on Java inheritance.
Super confused, need help with tutorial. I cant sample run this because I keep getting a error and don't know why.
Lab/tutorial on Java inheritance. Start a new Project Lab 14ABCInheritance in IntelliJ. CopyAndPaste the three classes below into the project. 1) After the three java classes, write a JavaClass Lab 14ABC.java with main() to create an int a, double b, and String c using defaults. . Aint a = new Aint ( ) ; / / default constructors . Bdouble b = new Bdouble () ; . CString c = new CString () ; . System . out . println ( "a=" + a + ", b=" + b + ", c=" + c ) ; // a=0, b=0.0, c="Hello World!" 2) Then continue main() to printIn() Mr Reed's favorite number and his name "Mr. Reed" using the classes A, B, C. . a . setA ( 5 ) ; // favorite number is an int . b. setB ( 5.0 ) ; / / favorite number is a double; . c. setC ( "Mr. Reed" ) ; / / name . System . out . println ( "a=" + a + ", b=" + b + ", C=" + c ) ; // a=5, b=5.0, c="Mr. Reed" 2) Now make the three objects -- Aint, Bdouble, CString -- into one object. Use inheritance so you do not have to rewrite code. Use the Java keyword extends. (It really should be inherits.), . Bdouble.java. public class Bdouble extends Aint { / / inherits class Aint . CString.java. public class Cstring extends Bdouble { // inherits class Bdouble Aint is the super class, Bdouble is the subclass, CString is the next subclass. Think of the classes as parent, child, grandchild. The family lineage example is the level of inheritance.. Comment out the method main(), i.e. do not delete code, and rewrite the main() with one object c. . Cstring c = new Cstring () ; o c. setA ( 5 ) ; / / inherited method. Do you see why? o c. setB ( 5.0 ) ; / / inherited. Because it is in the super class. o c. setC ( "Mr. Reed" ) ; o System . out .println( c ) ; // toString () is only instance variable CString, not inherited instance variables o System . out . printin ( c. getA() + ", " + c. getB () + " + c ) ; / / one object c 3) Finally, make the toString() more useful with inheritance. It should show the data of the super classes. This is tricky. Note the super can only go back to the direct class, the immediate parent class. . Bdouble.java. toString () { / / direct super class tostring (), i. e. Aint . toString () return super . tostring () + " " + b; . CString.java. tostring () { / / direct super class tostring (), i. e. Bdouble . toString () return super . tostring () + " " + c; . Now run main(). You should be able to comment out the last printin() with the getters because the toString()s have inheritance with super.to String().\f\fStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started