In JAVA In this assignment you will design a program that creates a class, then
ID: 3854633 • Letter: I
Question
In 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
Hi i will create two base classes called Car and Television (i provided the comments also)and i can pick the base class, such as car, or television in below classes C and D
i run the program and provided the output for the program
program:
import java.util.*;
import java.lang.*;
import java.io.*;
//base class 1 (car)
class car
{
public void methodcar()
{
System.out.println("method of Class car");
}
} base class 2 (television)
//
class television extends car
{
public void methodtelevision()
{
System.out.println("method of Class television");
}
}
class C extends car
{
public void methodC()
{
System.out.println("method of Class C");
}
}
class D extends car
{
public void methodD()
{
System.out.println("method of Class D");
}
}
class MyClass
{
public void methodtelevision()
{
System.out.println("method of Class television");
}
public static void main(String args[])
{
television obj1 = new television();
C obj2 = new C();
D obj3 = new D();
obj1.methodcar();
obj2.methodcar();
obj3.methodcar();
}
}
output:
Success time: 0.04 memory: 4386816 signal:0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.