JAVA***************** In this assignment you will design a program that creates
ID: 3854107 • Letter: J
Question
JAVA*****************
In this assignment you will design a program that creates a class, then extends the class using Inheritance. You may pick any base class, such as car, or television. Then you must extend the class at least once.
import java.io.PrintStream;
public class Sub_class
extends Super_class
{
int num = 10;
public void display()
{
System.out.println("This is the display method of subclass");
}
public void my_method()
{
Sub_class sub = new Sub_class();
sub.display();
super.display();
System.out.println("value of the variable named num in sub class:" + sub.num);
System.out.println("value of the variable named num in super class:" + this.num);
}
public static void main(String[] args)
{
Sub_class obj = new Sub_class();
obj.my_method();
}
}
Explanation / Answer
Okay, so let';s start, we need to make a super class as well and add a display method to the parent class too.
Then we can call the display method of parent class and sub class both.
So the code after correction will be: -
import java.io.PrintStream;
class Cars
{
public void display()
{
System.out.println("This is the display method of Superclass");
}
}
public class Mercedes extends Cars
{
int num = 10;
public void display()
{
System.out.println("This is the display method of Subclass");
}
public void m1()
{
Mercedes sub = new Mercedes();
sub.display();
super.display();
System.out.println("value of the variable named num in sub class:" + sub.num);
System.out.println("value of the variable named num in super class:" + this.num);
}
public static void main(String[] args)
{
Mercedes obj = new Mercedes();
obj.m1();
}
}
Save the file as Mercedes.java, and then compile the file using the command javac Mercedes,java, to execute the file use the command java Mercedes.
This is all for the question.
Thank You for using Chegg...!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.