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 14 of 14

Thread: My first adventure for working with inheritance

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default My first adventure for working with inheritance

    Hi, I'm trying to learn inheritance. Here is the code of my business tier package class so far:

    /**
     * This is the Parent class for all vehicle objects.
     * Child classes will be SUV, Truck, and Sedan.
     * An override of a model could be something like Toyota Corolla LE and Toyota Corolla S.
     */
     
    package vehicle.business;
    import java.lang.Object;
     
    public class Vehicle {
     
    	//instance variables
    	private String make;
    	private int year;
    	private String model;//override possibly needed
    	private double price;//override possibly needed
     
    	//constructor
    	public Vehicle() {
    		make = "";
    		year = 0;
    		model = "";
    		price = 0.00;
    	}
    	public void setMake() {
    		this.make = make;
    	}
    	public String getMake() {
    		return make;
    	}
     
    	public void setYear() {
    		this.year = year;
    	}
    	public int getYear() {
    		return year;
    	}
     
    	public void setModel() {
    		this.model = model;
    	}
    	public String getModel() {
    		return model;
    	}
     
    	public void setPrice() {
    		this.price = price;
    	}
    	public double getPrice() {
    		return price;
    	}
     
    	//Since the model name and price might vary depending on the vehicle type (this is where
    	//the child classes come in, i.e. SUV, Truck, Sedan), we should provide override methods 
    	//just in case:
    	@Override
    	public String toString() {
    		return model;
    	}
     
    	@Override
    	public String toString() {
    		return price;
    	}
    }

    The Object class is the superclass for all Java classes; the methods of the Object class are available from every object. I assumed that meant the Object class was a special class whose methods could be used as needed, but I got the "Duplicate method toString() in type Vehicle" error on lines 57 and 62 when I tried to provide the toString overriding method for more than one instance variable.

    Am I only limited to one toString method? What if I needed my program to provide overriding for more than one string type attribute of an object?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: My first adventure for working with inheritance

    Am I only limited to one toString method?
    Yes. If more than one were allowed, how would the JVM know which one to use if there was more than one with the same name?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: My first adventure for working with inheritance

    The toString method is used to print out descriptive information about the object with out having to explicitly call a method to do so. If you want to include more info to display then incorporate it into the String that is returned by toString.

    Also, you might want to consider using a constructor to populate your fields with passed values.

    Regards,
    Jim

  4. #4
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: My first adventure for working with inheritance

    Quote Originally Posted by jim829 View Post
    Also, you might want to consider using a constructor to populate your fields with passed values.
    Are you talking about an overload constructor:
    /**
     * This is the Parent class for all vehicle objects.
     * Child classes will be SUV, Truck, and Sedan.
     * An override of a model could be something like Toyota Corolla LE and Toyota Corolla S.
     */
     
    package vehicle.business;
    import java.lang.Object;
     
    public class Vehicle {
     
    	//instance variables
    	private int serialNumber;
    	private String make;
    	private int year;
    	private String model;//override possibly needed
    	private double price;//override possibly needed
     
    	//constructor
    	public Vehicle() {
    		serialNumber = 0;
    		make = "";
    		year = 0;
    		model = "";
    		price = 0.00;
    	}
     
    	//Is this what's called an overload constructor?
    	public Vehicle(int serialNumber, String make, int year, String model, double price) {
    		System.out.println("Constructing a Vehicle object.");
    		this.serialNumber = serialNumber;
    		this.make = make;
    		this.year = year;
    		this.model = model;
    		this.price = price;
    	}
     
    	public void setSerialNumber() {
    		this.serialNumber = serialNumber;
    	}
    	public int getSerialNumber() {
    		return serialNumber;
    	}
     
    	public void setMake() {
    		this.make = make;
    	}
    	public String getMake() {
    		return make;
    	}
     
    	public void setYear() {
    		this.year = year;
    	}
    	public int getYear() {
    		return year;
    	}
     
    	public void setModel() {
    		this.model = model;
    	}
    	public String getModel() {
    		return model;
    	}
     
    	public void setPrice() {
    		this.price = price;
    	}
    	public double getPrice() {
    		return price;
    	}
     
    	//Since the model name and price might vary depending on the vehicle type (this is where
    	//the child classes come in, i.e. SUV, Truck, Sedan), we should provide override methods 
    	//just in case:
    	@Override
    	public String toString() {
    		return model;
    	}	
    }

    Also, here are my child classes:
    package vehicle.business;
     
    public class SUV extends Vehicle {
     
    	private int seatCount;
    	private String suvModelVersion;
     
    	public SUV() {
    		super();
    		seatCount = 0;
    		suvModelVersion = "";
    	}
     
    	public void setSeatCount() {
    		this.seatCount = seatCount;
    	}
    	public int getSeatCount() {
    		return seatCount;
    	}
     
    	public void setSUVModelVersion() {
    		this.suvModelVersion = suvModelVersion;
    	}
    	public String getSUVModelVersion() {
    		return suvModelVersion;
    	}
     
    	@Override
    	public String toString() {
    		return super.toString() +
    				" version " + suvModelVersion; 
    	}
    }
    package vehicle.business;
     
    public class Truck extends Vehicle {
     
    	private String cabType;//chassis cab, crew cab, or double cab
    	private int bedLength;
    	private int towingCapacity;
    	private int payloadCapacity;
    	private String truckModelVersion;
     
    	public Truck() {
    		super();
    		cabType = "";
    		bedLength = 0;
    		towingCapacity = 0;
    		payloadCapacity = 0;
    		truckModelVersion = "";
    	}
     
    	public void setCabType() {
    		this.cabType = cabType;
    	}
    	public String getCabType() {
    		return cabType;
    	}
     
    	public void setBedLength() {
    		this.bedLength = bedLength;
    	}
    	public int getBedLength() {
    		return bedLength;
    	}
     
    	public void setTowingCapacity() {
    		this.towingCapacity = towingCapacity;
    	}
    	public int getTowingCapacity() {
    		return towingCapacity;
    	}
     
    	public void setPayloadCapacity() {
    		this.payloadCapacity = payloadCapacity;
    	}
    	public int getPayloadCapacity() {
    		return payloadCapacity;
    	}
     
    	public void setTruckModelVersion() {
    		this.truckModelVersion = truckModelVersion;
    	}
    	public String getTruckModelVersion() {
    		return truckModelVersion;
    	}
     
    	@Override
    	public String toString() {
    		return super.toString() +
    				" version " + truckModelVersion;
    	}
    }
    package vehicle.business;
     
    public class Sedan extends Vehicle {
     
    	private String doorType;//four-door or two-door
    	private int cylinders;
    	private String sedanModelVersion;
     
    	public Sedan() {
    		super();
    		doorType = "";
    		cylinders = 0;
    		sedanModelVersion = "";
    	}
     
    	public void setDoorType() {
    		this.doorType = doorType;
    	}
    	public String getDoorType() {
    		return doorType;
    	}
     
    	public void setCylinders() {
    		this.cylinders = cylinders;
    	}
    	public int getCylinders() {
    		return cylinders;
    	}
     
    	public void setSedanModelVersion() {
    		this.sedanModelVersion = sedanModelVersion;
    	}
    	public String getSedanModelVersion() {
    		return sedanModelVersion;
    	}
     
    	@Override
    	public String toString() {
    		return super.toString() +
    				" version " + sedanModelVersion;
    	}
    }

    My current problem lies in the static database class:
    package vehicle.db;
    import vehicle.business.Vehicle;
    import vehicle.business.Sedan;
    import vehicle.business.Truck;
    import vehicle.business.SUV;
     
    public class StaticVehicleDB {
     
    	public static Vehicle getVehicle(int serialNumber) {
    		Vehicle v = null;
     
    		if(serialNumber == 1111 || serialNumber == 2222) {
    			SUV suv = new SUV();
    			if(serialNumber == 1111) {
    				suv.setSerialNumber(serialNumber);
    				suv.setMake("Chevy");
    				suv.setYear(2003);
    				suv.setModel("Suburban");
    				suv.setPrice(50800.99);
    			}else if(serialNumber == 2222) {
    				suv.setSerialNumber(serialNumber);
    				suv.setMake("Dodge");
    				suv.setYear(2001);
    				suv.setModel("Durango");
    				suv.setPrice(53600.99);
    			}
    			v = suv;//set product equal to the SUV object (this is polymorphism, right?)
    		}
     
    		else if(serialNumber == 3333 || serialNumber == 4444) {
    			Truck truck = new Truck();
    			if(serialNumber == 3333) {
    				truck.setSerialNumber(serialNumber);
    				truck.setMake("Ford");
    				truck.setYear(2004);
    				truck.setModel("F-150");
    				truck.setPrice(56800.99);
    			}else if(serialNumber == 4444) {
    				truck.setSerialNumber(serialNumber);
    				truck.setMake("Dodge");
    				truck.setYear(2011);
    				truck.setModel("Ram");
    				truck.setPrice(58888.99);
    			}
    			v = truck;
    		}
     
    		else if(serialNumber == 5555 || serialNumber == 6666) {
    			Sedan sedan = new Sedan();
    			if(serialNumber == 5555) {
    				sedan.setSerialNumber(serialNumber);
    				sedan.setMake("Subaru");
    				sedan.setYear(2006);
    				sedan.setModel("Impreza WRX");//trail blazer
    				sedan.setPrice(33800.99);
    			}else if(serialNumber == 6666) {
    				sedan.setSerialNumber(serialNumber);
    				sedan.setMake("Chevy");
    				sedan.setYear(1998);
    				sedan.setModel("Lumina");//trail blazer
    				sedan.setPrice(11338.99);
    			}
    			v = sedan;
    		}
    	}//end of getVehicle method
    }//end of StaticVehicleDB class
    The errors on all of my set method calls. For example, "The method setSerialNumber() in the type Vehicle is not applicable for the arguments (int)".

    What's wrong?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: My first adventure for working with inheritance

    "The method setSerialNumber() in the type Vehicle is not applicable for the arguments (int)".
    Check that the arguments in the calling code match those in the method's definition.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: My first adventure for working with inheritance

    Quote Originally Posted by Norm View Post
    Check that the arguments in the calling code match those in the method's definition.
    I don't understand why you said that. The instance variable values supplied in each function call:
    public static Vehicle getVehicle(int serialNumber) {
    		Vehicle v = null;
     
    		if(serialNumber == [COLOR="#FF0000"][B]1111 [/B][/COLOR]|| serialNumber == [B][COLOR="#FF0000"]2222[/COLOR][/B]) {
    			SUV suv = new SUV();
    			if(serialNumber == 1111) {
    				suv.setSerialNumber([COLOR="#FF0000"][B]serialNumber[/B][/COLOR]);
    				suv.setMake("[B][COLOR="#FF0000"]Chevy[/COLOR][/B]");
    				suv.setYear([COLOR="#FF0000"][B]2003[/B][/COLOR]);
    				suv.setModel("[B][COLOR="#FF0000"]Suburban[/COLOR][/B]");
    				suv.setPrice([COLOR="#FF0000"][B]50800.99[/B][/COLOR]);
    				suv.setSeatCount([COLOR="#FF0000"][B]8[/B][/COLOR]);
    				suv.getSUVModelVersion("imAnSUV");
    			}else if(serialNumber == 2222) {
    				suv.setSerialNumber([COLOR="#FF0000"][B]serialNumber[/B][/COLOR]);
    				suv.setMake("[B][COLOR="#FF0000"]Dodge[/COLOR][/B]");
    				suv.setYear([B][COLOR="#FF0000"]2001[/COLOR][/B]);
    				suv.setModel("[B][COLOR="#FF0000"]Durango[/COLOR][/B]");
    				suv.setPrice([COLOR="#FF0000"][B]53600.99[/B][/COLOR]);
    				suv.setSeatCount([B][COLOR="#FF0000"]5[/COLOR][/B]);
    				suv.getSUVModelVersion("imAnSUV");
    			}
    			v = suv;//set product equal to the SUV object (this is polymorphism, right?)
    		}
                    [COLOR="#00FF00"]return v;[/COLOR]
    match the datatypes in the instance variable, constructors, setters, and getters of both parent and child classes:
    /**
     * This is the Parent class for all vehicle objects.
     * Child classes will be SUV, Truck, and Sedan.
     * An override of a model could be something like Toyota Corolla LE and Toyota Corolla S.
     */
     
    package vehicle.business;
    import java.lang.Object;
     
    public class Vehicle {
     
    	//instance variables
    	private [COLOR="#FF0000"][B]int [/B][/COLOR]serialNumber;
    	private [COLOR="#FF0000"][B]String [/B][/COLOR]make;
    	private [COLOR="#FF0000"][B]int [/B][/COLOR]year;
    	private [B][COLOR="#FF0000"]String [/COLOR][/B]model;//override possibly needed
    	private [B][COLOR="#FF0000"]double [/COLOR][/B]price;//override possibly needed
     
    	//constructor
    	public Vehicle() {
    		serialNumber = 0;
    		make = "";
    		year = 0;
    		model = "";
    		price = 0.00;
    	}
     
    	//Is this what's called an overload constructor?
    	public Vehicle(int serialNumber, String make, int year, String model, double price) {
    		System.out.println("Constructing a Vehicle object.");
    		this.serialNumber = serialNumber;
    		this.make = make;
    		this.year = year;
    		this.model = model;
    		this.price = price;
    	}
     
    	public void setSerialNumber() {
    		this.serialNumber = serialNumber;
    	}
    	public int getSerialNumber() {
    		return serialNumber;
    	}
     
    	public void setMake() {
    		this.make = make;
    	}
    	public String getMake() {
    		return make;
    	}
     
    	public void setYear() {
    		this.year = year;
    	}
    	public int getYear() {
    		return year;
    	}
     
    	public void setModel() {
    		this.model = model;
    	}
    	public String getModel() {
    		return model;
    	}
     
    	public void setPrice() {
    		this.price = price;
    	}
    	public double getPrice() {
    		return price;
    	}
     
    	//Since the model name and price might vary depending on the vehicle type (this is where
    	//the child classes come in, i.e. SUV, Truck, Sedan), we should provide override methods 
    	//just in case:
    	@Override
    	public String toString() {
    		return model;
    	}	
    }
    package vehicle.business;
     
    public class SUV extends Vehicle {
     
    	private [COLOR="#FF0000"][B]int [/B][/COLOR]seatCount;
    	private [COLOR="#FF0000"][B]String [/B][/COLOR]suvModelVersion;
     
    	public SUV() {
    		super();
    		seatCount = 0;
    		suvModelVersion = "";
    	}
     
    	public void setSeatCount() {
    		this.seatCount = seatCount;
    	}
    	public int getSeatCount() {
    		return seatCount;
    	}
     
    	public void setSUVModelVersion() {
    		this.suvModelVersion = suvModelVersion;
    	}
    	public String getSUVModelVersion() {
    		return suvModelVersion;
    	}
     
    	@Override
    	public String toString() {
    		return super.toString() +
    				" version " + suvModelVersion; 
    	}
    }
    Last edited by SamJava_the_Hut; March 7th, 2019 at 02:22 AM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: My first adventure for working with inheritance

    The method's definition does NOT have any arguments. There is nothing inside the ()s:
        public void setSerialNumber() {
    The code that calls the method has the argument: serialNumber:
    				sedan.setSerialNumber(serialNumber);

    There must be a match in the arguments between the definition and the call to the method.

    Add an int argument to the method's definition so that the definition and call match.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: My first adventure for working with inheritance

    Quote Originally Posted by Norm View Post
    There must be a match in the arguments between the definition and the call to the method.

    Add an int argument to the method's definition so that the definition and call match.
    Ok, I finally did what you said, the warnings such as "The assignment to variable seatCount has no effect" in the SUV, Truck, and Sedan setter class methods have gone away.

    Unfortunately, the errors like "The method setSerialNumber() in the type Vehicle is not applicable for the arguments (int)" still exist in the StaticVehicleDB class though:
    package vehicle.db;
    import vehicle.business.Vehicle;
    import vehicle.business.Sedan;
    import vehicle.business.Truck;
    import vehicle.business.SUV;
     
    public class StaticVehicleDB {
     
    	//object type test:
    	Vehicle vehicle = new Vehicle();
    	/*
    	if(vehicle instanceof SUV) {
    		System.out.println("This is an SUV object.");
    	}
    	*/
    	public static Vehicle getVehicle(int serialNumber) {
    		//Vehicle v = null;
    		vehicle.setSerialNumber(serialNumber);
    		if(serialNumber == 1111 || serialNumber == 2222) {
    			SUV suv = new SUV();
    			suv.setSerialNumber(serialNumber);
    			if(serialNumber == 1111) {
    				//suv.setSerialNumber(serialNumber);
    				suv.setMake("Chevy");
    				suv.setYear(2003);
    				suv.setModel("Suburban");
    				suv.setPrice(50800.99);
    				suv.setSeatCount(8);
    				suv.getSUVModelVersion("imAnSUV");
    			}else if(serialNumber == 2222) {
    				//suv.setSerialNumber(serialNumber);
    				suv.setMake("Dodge");
    				suv.setYear(2001);
    				suv.setModel("Durango");
    				suv.setPrice(53600.99);
    				suv.setSeatCount(5);
    				suv.getSUVModelVersion("imAnSUV");
    			}
    			v = suv;//set product equal to the SUV object (this is polymorphism, right?)
    		}
     
    		else if(serialNumber == 3333 || serialNumber == 4444) {
    			Truck truck = new Truck();
    			if(serialNumber == 3333) {
    				truck.setSerialNumber(serialNumber);
    				truck.setMake("Ford");
    				truck.setYear(2004);
    				truck.setModel("F-150");
    				truck.setPrice(56800.99);
    			}else if(serialNumber == 4444) {
    				truck.setSerialNumber(serialNumber);
    				truck.setMake("Dodge");
    				truck.setYear(2011);
    				truck.setModel("Ram");
    				truck.setPrice(58888.99);
    			}
    			v = truck;
    		}
     
    		else if(serialNumber == 5555 || serialNumber == 6666) {
    			Sedan sedan = new Sedan();
    			if(serialNumber == 5555) {
    				sedan.setSerialNumber(serialNumber);
    				sedan.setMake("Subaru");
    				sedan.setYear(2006);
    				sedan.setModel("Impreza WRX");//trail blazer
    				sedan.setPrice(33800.99);
    			}else if(serialNumber == 6666) {
    				sedan.setSerialNumber(serialNumber);
    				sedan.setMake("Chevy");
    				sedan.setYear(1998);
    				sedan.setModel("Lumina");//trail blazer
    				sedan.setPrice(11338.99);
    			}
    			v = sedan;
    		}
    		return v;
    	}//end of getVehicle method
    }//end of StaticVehicleDB class

    Line 18 also has the error, "Cannot make a static reference to the non-static field vehicle", which is weird, since I made a Vehicle object instance on line 10.

    Lines 39, 57, 75, and 77 also say, "v cannot be resolved to a variable".

    Why is all of this happening? I thought child classes are supposed to inherit methods from the parent class. What am I missing for the sake of parent and child class polymorphism here?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: My first adventure for working with inheritance

    "Cannot make a static reference to the non-static field vehicle",
    A static method can not refer to a non-static field.
    Why is the getVehicle method static? Change it to non-static to allow it to reference the vehicle field.
    Or move the definition of vehicle inside of the method so it is in scope where used.
    Is an instance of the StaticVehicleDB ever created? What is the purpose of the class?

    "v cannot be resolved to a variable".
    Where is v defined? Make sure it is defined in the same scope where it is referenced.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: My first adventure for working with inheritance

    Quote Originally Posted by Norm View Post
    Why is the getVehicle method static? Change it to non-static to allow it to reference the vehicle field.
    Or move the definition of vehicle inside of the method so it is in scope where used.
    Well, I chose the latter, but I'm still getting the same error several times in the code (for example on line 12, "The method setSerialNumber() in the type Vehicle is not applicable for the arguments (int)"). After adding in the set methods to get the other parameters from the child classes, I have found that the set methods from the Vehicle parent class are still giving errors like line 12. The child class set method calls (except for the SUV child class ones) on lines 44-48, 55-59, 72-74, and 81-83, on the other hand, are not giving any errors. Here is the latest version of the StaticVehicleDB code:
    package vehicle.db;
    import vehicle.business.Vehicle;
    import vehicle.business.Sedan;
    import vehicle.business.Truck;
    import vehicle.business.SUV;
     
    public class StaticVehicleDB {
     
    	public static Vehicle getVehicle(int serialNumber) {
     
    	Vehicle v = new Vehicle();	
    		v.setSerialNumber(serialNumber);
    		if(serialNumber == 1111 || serialNumber == 2222) {
    			SUV suv = new SUV();
    			suv.setSerialNumber(serialNumber);
    			if(serialNumber == 1111) {
    				//suv.setSerialNumber(serialNumber);
    				suv.setMake("Chevy");
    				suv.setYear(2003);
    				suv.setModel("Suburban");
    				suv.setPrice(50800.99);
    				suv.setSeatCount(8);
    				suv.getSUVModelVersion("imAnSUV");
    			}else if(serialNumber == 2222) {
    				//suv.setSerialNumber(serialNumber);
    				suv.setMake("Dodge");
    				suv.setYear(2001);
    				suv.setModel("Durango");
    				suv.setPrice(53600.99);
    				suv.setSeatCount(5);
    				suv.getSUVModelVersion("imAnSUV");
    			}
    			v = suv;//set product equal to the SUV object (this is polymorphism, right?)
    		}
     
    		else if(serialNumber == 3333 || serialNumber == 4444) {
    			Truck truck = new Truck();
    			if(serialNumber == 3333) {
    				truck.setSerialNumber(serialNumber);
    				truck.setMake("Ford");
    				truck.setYear(2004);
    				truck.setModel("F-150");
    				truck.setPrice(56800.99);
    				truck.setCabType("Crew-Cab");
    				truck.setBedLength(7);
    				truck.setTowingCapacity(15000);
    				truck.setPayloadCapacity(17000);
    				truck.setTruckModelVersion("imATruck");
    			}else if(serialNumber == 4444) {
    				truck.setSerialNumber(serialNumber);
    				truck.setMake("Dodge");
    				truck.setYear(2011);
    				truck.setModel("Ram");
    				truck.setPrice(58888.99);
    				truck.setCabType("Double-Cab");
    				truck.setBedLength(7);
    				truck.setTowingCapacity(15000);
    				truck.setPayloadCapacity(17000);
    				truck.setTruckModelVersion("imATruck");
    			}
    			v = truck;
    		}
     
    		else if(serialNumber == 5555 || serialNumber == 6666) {
    			Sedan sedan = new Sedan();
    			if(serialNumber == 5555) {
    				sedan.setSerialNumber(serialNumber);
    				sedan.setMake("Subaru");
    				sedan.setYear(2006);
    				sedan.setModel("Impreza WRX");
    				sedan.setPrice(33800.99);
    				sedan.setDoorType("Two-Door");
    				sedan.setCylinders(4);
    				sedan.setSedanModelVersion("imASedan");
    			}else if(serialNumber == 6666) {
    				sedan.setSerialNumber(serialNumber);
    				sedan.setMake("Chevy");
    				sedan.setYear(1998);
    				sedan.setModel("Lumina");
    				sedan.setPrice(11338.99);
    				sedan.setDoorType("Four-Door");
    				sedan.setCylinders(6);
    				sedan.setSedanModelVersion("imASedan");
    			}
    			v = sedan;
    		}
    		return v;
    	}//end of getVehicle method
    }//end of StaticVehicleDB class

    The weird thing is that the child class set methods for the SUV class (called on lines 22, 23, 30, and 31) are still giving the same kind of error as line 12. What is up with that?

    Quote Originally Posted by Norm View Post
    Is an instance of the StaticVehicleDB ever created? What is the purpose of the class?
    The purpose of the class is just as it looks. I want the functionality of creating an object out of my child classes (Sedan, SUV, and Truck), depending on the serial number that will be entered by the user in the main method of the VehicleApp class.

    Quote Originally Posted by Norm View Post
    Where is v defined? Make sure it is defined in the same scope where it is referenced.
    That scope is fixed. The problem now is that the program doesn't understand what the parent class set methods are supposed to do in the StaticVehicleDB class.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: My first adventure for working with inheritance

    The method setSerialNumber() in the type Vehicle is not applicable for the arguments (int)
    The compiler can not find a definition for the setSerialNumber method in the Vehicle class that takes an int value. Change the definition of the setSerialNumber method in the Vehicle class to take an int parameter.
    See post#7

    Also look at the tutorial: https://docs.oracle.com/javase/tutor...arguments.html
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: My first adventure for working with inheritance

    The code finally does what I want! There's just one little problem. The program says, "Bye!" and quits before the user can input a y or n to choose to continue or not to continue:
    package vehicle.ui;
    import java.util.ArrayList;
    import java.util.Scanner;
    import vehicle.db.StaticVehicleDB;
    import vehicle.business.*;
     
    public class VehicleApp {
     
    	public static void main(String args[]) {
    		System.out.println("Welcome to the vehicle zone!");
    		System.out.println();
     
    		Scanner input = new Scanner(System.in);
    		String choice = "y";
    		while(choice.equalsIgnoreCase("y")) {
    			System.out.println("Enter a valid vehicle serial number.");
    			int serialNumber = input.nextInt();
    			Vehicle v = StaticVehicleDB.getVehicle(serialNumber);			
    			System.out.println();
     
    			if(v != null) {
    				if(v instanceof SUV) {
    					System.out.println("You entered " + serialNumber + ", which is an SUV object.");
    					SUV suv = (SUV) v;//cast the Vehicle object back to an SUV object
    					String message = 
    					"Vehicle maker: " + v.getMake() + "\n" +
    					"Vehicle year: " + v.getYear() + "\n" +
    					"Vehicle model: " + v.getModel() + "\n" +
    					"Vehicle price: " + v.getPrice() + "\n" +
    					"Number of seats: " + suv.getSeatCount() + "\n" +
    					"Model name for this vehicle type: " + suv.getSUVModelVersion() + "\n"; 
    					System.out.println(message);
    				}else if(v instanceof Truck) {
    					System.out.println("You entered " + serialNumber + ", which is a Truck object.");
    					Truck truck = (Truck) v;
    					String message = 
    					"Vehicle maker: " + v.getMake() + "\n" +
    					"Vehicle year: " + v.getYear() + "\n" +
    					"Vehicle model: " + v.getModel() + "\n" +
    					"Vehicle price: " + v.getPrice() + "\n" +
    					"Cab type: " + truck.getCabType() + "\n" +
    					"Cargo bed length: " + truck.getBedLength() + " feet" + "\n" +
    					"Towing capacity: " + truck.getTowingCapacity() + " lbs" + "\n" +
    					"Payload capacity: " + truck.getPayloadCapacity() + " lbs" + "\n" +
    					"Model name for this vehicle type: " + truck.getTruckModelVersion() + "\n";
     					System.out.println(message);
    				}else if(v instanceof Sedan) {
    					System.out.println("You entered " + serialNumber + ", which is a Sedan object.");
    					Sedan sd = (Sedan) v;
    					String message = 
    					"Vehicle maker: " + v.getMake() + "\n" +
    					"Vehicle year: " + v.getYear() + "\n" +
    					"Vehicle model: " + v.getModel() + "\n" +
    					"Vehicle price: " + v.getPrice() + "\n" +
    					"Door type: " + sd.getDoorType() + "\n" +
    					"Engine type: " + sd.getCylinders() + " cylinders" + "\n" +
    					"Model name for this vehicle type: " + sd.getSedanModelVersion() + "\n";
    					System.out.println(message);
    				}else {
    					System.out.println("The serial number you entered did not match any vehicles in the StaticVehicleDB database.");
    				}				
    			}//end of if(v != null)
     
    			System.out.println();
    			System.out.println("Do you wish to continue (y/n)?");
    			choice = input.nextLine();
    			System.out.println();
    		}//end of while(choice.equalsIgnoreCase("y"))
    		input.close();
    		System.out.println("Bye!");
    	}
    }

    Output sample:

    Welcome to the vehicle zone!

    Enter a valid vehicle serial number.
    2222

    You entered 2222, which is an SUV object.
    Vehicle maker: Dodge
    Vehicle year: 2001
    Vehicle model: Durango
    Vehicle price: 53600.99
    Number of seats: 5
    Model name for this vehicle type: imAnSUV


    Do you wish to continue (y/n)?

    Bye!

    The scope of my curly braces seem fine. Any idea for what's causing this?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: My first adventure for working with inheritance

    The program says, "Bye!" and quits before the user can input a y or n to choose to continue or not to continue:
    That is probably because of the issue in the Scanner class when the nextLine method is called after any of the next... methods.
    See: https://christprogramming.wordpress....on-mistakes-1/
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    SamJava_the_Hut (April 17th, 2019)

  15. #14
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: My first adventure for working with inheritance

    Am I only limited to one toString method?
    I didn't consider this at the time but you can control what String to return by the use of conditionals within the toString() method. I do not know if this is considered good practice but it is certainly doable.

    Regards,
    Jim

Similar Threads

  1. inheritance - why its not working?
    By Yehonatan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 12th, 2018, 05:15 PM
  2. Type Adventure
    By c2burger in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 3rd, 2013, 08:39 AM
  3. Text adventure game help
    By Death_The_Kid in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2013, 05:45 AM
  4. Inheritance not working!
    By u-will-neva-no in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 20th, 2012, 06:59 PM