Reputation: 11
We discussed about inheritance in java this morning but it seems that I have an error in my code and my professor could not help me because he is busy, can you just help my to point my error?
package inheritance;
class Inheritance {
void accelerate()
{
System.out.println("Drive");
}
void turn()
{
System.out.println("Turn!");
}
}
class n2wheels extends Inheritance {
void fast()
{
System.out.println("Swift Vehicle");
}
}
class n4wheels extends Inheritance {
void normal()
{
System.out.println("Average Vehicle");
}
}
class multiwheel extends Inheritance {
void slow()
{
System.out.println("Heavy Vehicle");
}
public static void main(String[] args)
{
Inheritance try1 = new Inheritance();
try1.normal();
}
}
Upvotes: 0
Views: 454
Reputation: 212
You are not able to call this method because it doesn't exist in Inheritance class and present in n4wheels class. I don't know what you are trying to achieve so I am providing you possible solutions below.
Solution 1: If you want to call normal() in Inheritance class only then declare it in the same class.
class Inheritance {
void accelerate() {
System.out.println("Drive");
}
void turn() {
System.out.println("Turn!");
}
void normal() {
// do something
}
}
Solution 2: If you want to call normal() method of n4wheels class directly then:
n4wheels try1 = new n4wheels();
try1.normal();
Solution 3: If you want to perform polymorphism then, you have to declare normal() method in Inheritance class as mentioned in 'solution 1' and then,
Inheritance try1 = new n4wheels();
try1.normal();
Upvotes: 0
Reputation: 12524
there is no normal
method in your Inheritance
class.
do at least:
class Inheritance {
void accelerate() {
System.out.println("Drive");
}
void turn() {
System.out.println("Turn!");
}
void normal(){}
}
or:
n4wheels try1 = new n4wheels();
try1.normal();
as a side node: please start class names uppercase. N4Wheels
, MultiWheels
etc...
Upvotes: 1