Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help with Inheritance in Java.

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help with Inheritance in Java.

    hey i was leaning inheritance and tried to implement it in Java.This is my base class vehicl.java
    public 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 ?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Help with Inheritance in Java.

    It showed an error "identifier required"
    Please copy the full text of the error messages and paste it here. It has important info about the error.

    BTW Java naming conventions are that classnames begin with UPPERCASE letters.

    i wanted to override the constructor as well as the getInfo()
    Be sure to add the @Override annotation before all methods you override so the compiler can find errors.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with Inheritance in Java.

    I'll keep the naming convention in mind the next time

    The errors from car.java
    D:\java>javac car.java
    car.java:4: error: <identifier> expected
    System.out.println("Enter the number of Seats");
    ^
    car.java:4: error: illegal start of type
    System.out.println("Enter the number of Seats");
    ^
    car.java:6: error: <identifier> expected
    numberSeats=numberSeats.nextInt();

    Errors in Inheritance.java
    D:\java>javac inheritance.java
    inheritance.java:3: error: constructor vehicle in class vehicle cannot be applie
    d to given types;
    vehicle objectVehicle=new vehicle(500,1000,"polo",false);//Shows
    error that it requires no formal arguments.
    ^
    required: no arguments
    found: int,int,String,boolean
    reason: actual and formal argument lists differ in length
    inheritance.java:7: error: cannot find symbol
    objectCar.getinfo();
    ^
    symbol: method getinfo()
    location: variable objectCar of type car
    2 errors

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Help with Inheritance in Java.

    car.java:4: error: <identifier> expected
    Most executeable statements must be INSIDE a constructor or method.
    inheritance.java:3: error: constructor vehicle in class vehicle cannot be applie
    d to given types;
    vehicle objectVehicle=new vehicle(500,1000,"polo",false);//Shows
    error that it requires no formal arguments.
    ^
    required: no arguments
    found: int,int,String,boolean
    reason: actual and formal argument lists differ in length
    The code tried to pass arguments to a constructor that has no arguments.
    Add a constructor to the class that takes the arguments that you want to pass. What you've coded is a void method, not a constructor.
    inheritance.java:7: error: cannot find symbol
    objectCar.getinfo();
    ^
    symbol: method getinfo()
    location: variable objectCar of type car
    Check the spelling.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with Inheritance in Java.

    Thanks a lot.Is it mandatory for all the executable statements to be inside a constructor or method ? unlike in C/C++ ? I did put all the executable statements inside the constructor for car class.
    import java.util.Scanner;
    public class car extends vehicle{
     
    	public void car(){
    		System.out.println("Enter the number of Seats");
    		Scanner numberSeats=new Scanner(System.in);
    		this.numberSeats=numberSeats.nextInt();
    		}
     
    	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);
    					}
    		}

    Still there was two errors
    D:\java>javac car.java
    car.java:7: error: cannot find symbol
    this.numberSeats=numberSeats.nextInt();
    ^
    symbol: variable numberSeats
    car.java:13: error: cannot find symbol
    "Seating Capacity :"+this.numbe
    rSeats);
    ^
    symbol: variable numberSeats
    2 errors

    D:\java>javac car.java
    car.java:7: error: cannot find symbol this.numberSeats=numberSeats.nextInt();
    ^
    symbol: variable numberSeats
    car.java:13: error: cannot find symbol "Seating Capacity :"+this.numberSeats);
    ^
    symbol: variable numberSeats
    2 errors

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Help with Inheritance in Java.

    error: cannot find symbol
    The compiler can not find a definition for the variable named in the error message.
    Check:
    does it exist
    is it in scope
    is it spelled correctly

    Variables can be defined outside of methods or constructors
    and assigned values from inside of methods and constructors

    NOTE: Constructors are NOT void. Methods use void.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java - Inheritance help!
    By deeevo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2013, 04:46 AM
  2. java - inheritance
    By Shreyas123 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 10th, 2013, 02:06 AM
  3. java inheritance
    By farheen_zafar1 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 24th, 2013, 01:58 PM
  4. [SOLVED] Java inheritance
    By maple1100 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 9th, 2013, 09:42 PM
  5. Java Inheritance Help
    By danielparry in forum Java Theory & Questions
    Replies: 3
    Last Post: March 17th, 2011, 03:20 PM