hey i was leaning inheritance and tried to implement it in Java.This is my base class vehicl.javapublic class vehicle{ private int topSpeed; private int cubicCap; private String modelName; private boolean hasAlloy; public void vehicle(int topSpeed,int cubicCap,String modelName,boolean hasAlloy){ this.topSpeed=topSpeed; this.cubicCap=cubicCap; this.modelName=modelName; this.hasAlloy=hasAlloy; } public void getInfo(){ System.out.print("The car model is "+modelName+" and the Top Speed is "+topSpeed+ "\nCapacity : "+cubicCap+"Alloy :"+hasAlloy+"."); } }
I also have a derived class called car.java.What i wanted to do in the derived class was that i wanted to override the constructor as well as the getInfo() method from the base class.I wanted to add an additional attribute to the car "numberSeats" and display tat too when the object to car class calls the getInfo() method .It showed an error "identifier required" when i tried to compile car.java program.
import java.util.Scanner; public class car extends vehicle{ //int numberSeats; //System.out.println("Enter the number of Seats"); Scanner numberSeats=new Scanner(System.in); numberSeats=numberSeats.nextInt(); //System.out.println(numberSeats.nextInt()); public void car(int numberSeats){ this.numberSeats=numberSeats; } public void getInfo(){ System.out.print("The car model is "+modelName+" and the Top Speed is "+topSpeed+ "\nCapacity : "+cubicCap+"Alloy :"+hasAlloy+".\n"+ "Seating Capacity :"+this.numberSeats); } }
The main program Inheritance.java also threw a few errors .public class Inheritence{ public static void main(String []args){ vehicle objectVehicle=new vehicle(500,1000,"polo",false);//Shows error that it requires no formal arguments. objectVehicle.vehicle(250,1000,"vento",true);//Works fine objectVehicle.getInfo(); //car objectCar=new car(); //objectCar.getinfo(); } }
Could you explain the errors that i get when i tried to compile car.java without using super keyword or without defining the constructor from the Car class ?