JAVA In this assignment you will design a program that creates a class, then ext
ID: 3853523 • 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
class car{
int cost=400000;
String color="Red";
}
class Car_SubClass extends car{
int tax=50000;
public static void main(String args[]){
Car_SubClass p=new Car_SubClass();
System.out.println("Cost of the car is :"+p.cost);
System.out.println("Color of the car is :"+p.color);
System.out.println("Total cost of the car including tax is :"+ (p.cost+p.tax));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.