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

Thread: My second adventure of working with inheritance

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

    Default My second adventure of working with inheritance

    Hi, I'm working on another inheritance project, this time with blade objects.

    Line 9 of the following child class:
    package blade.business;
    //This is a child class of the Blade class, dedicated to knives for camping and survival.
    public class Survival extends Blade{
     
    	private boolean serrated;
    	private String listOfToolsIncluded;
     
    	public Survival() {
    		super();
    		serrated = false;
    		listOfToolsIncluded = "";		
    	}
     
    	public void setSerrated(boolean serrated) {
    		this.serrated = serrated;
    	}
    	public boolean getSerrated() {
    		return serrated;
    	}
     
    	public void setListOfToolsIncluded(String listOfToolsIncluded) {
    		this.listOfToolsIncluded = listOfToolsIncluded;
    	}
    	public String getListOfToolsIncluded() {
    		return listOfToolsIncluded;
    	}
    }
    gives the error, "The constructor Blade() is undefined" when I don't include both of the following constructors in the Blade parent class:

    //default constructor
    	public Blade() {
    		name = "";
    		modelNumber = 0;
    		bladeColor = "";
    		powerType = "";
    		price = 0.0;
    		bladeType = "";
    		brand = "";
    		length = 0;
    		description = "";
    	}
     
    	//overload constructor
    	public Blade(String name, int modelNumber, String bladeColor, String powerType, double price, 
    				String bladeType, String brand, int length,	String description) {
    		System.out.println("Creating a blade object.");
    		this.name = name;
    		this.modelNumber = modelNumber;
    		this.bladeColor = bladeColor;
    		this.powerType = powerType;
    		this.price = price;
    		this.bladeType = bladeType;
    		this.brand = brand;
    		this.length = length;
    		this.description = description;
    	}

    I thought that in Java, the application/program automatically creates a default constructor if the programmer doesn't define one.
    I would think, therefore, that the super() class instance in the Survival child class constructor could use the overload constructor and be fine with it.
    So why does that error occur then?

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

    Default Re: My second adventure of working with inheritance

    The default constructor is provided if you don't add any other constructor. Then you must provide one if you want it. This is because you may want to force a constructor to be used if you are required to pass values. Otherwise, that would not be possible (or at best, cumbersome) if the default was created automatically.

    Also, don't put those values in the default constructor. Just initialize them to their default values when you declare them.

    Finally, when you have many values in a constructor it gets confusing. So consider using the Builder design method for creating a class instance. Here's an example:

    public class BuilderDesignDemo {
     
       public static void main(String[] args) {
     
             // Create an instance without worrying about the position of values within the constructor.
            //  Nor does one have to create multiple constructors for different sets of arguments.
     
    	  Car car = new Car().setLicense("ABC1234").setMake("Toyota").setModel(
    	        "Corolla").setYear(1988);
     
    	  System.out.println(car);
       }
    }
     
    class Car {
       private String make	  = "N/A";
       private String model	  = "N/A";
       private int	  year	  = 0;
       private String vin	  = "N/A";
       private String license = "N/A";
     
       Car() {
       }
     
       public String getMake() {
    	  return make;
       }
     
       public Car setMake(String make) {
    	  this.make = make;
    	  return this;
       }
     
       public String getModel() {
    	  return model;
       }
     
       public Car setModel(String model) {
    	  this.model = model;
    	  return this;
       }
     
       public int getYear() {
    	  return year;
       }
     
       public Car setYear(int year) {
    	  this.year = year;
    	  return this;
       }
     
       public String getVin() {
    	  return vin;
       }
     
       public Car setVin(String vin) {
    	  this.vin = vin;
    	  return this;
       }
     
       public String getLicense() {
    	  return license;
       }
     
       public Car setLicense(String license) {
    	  this.license = license;
    	  return this;
       }
     
       @Override
       public String toString() {
    	  return "Car [make=" + make + ", model=" + model + ", year=" + year
    	        + ", vin=" + vin + ", license=" + license + "]";
       }
     
    }
    Regards,
    Jim

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

    Default Re: My second adventure of working with inheritance

    New problem.

    The same kind of error, "The method setBladeType(String) is undefined for the type Construction" on lines 12 - 32 occurs in my database class, but only for the Construction child object, and I have no idea why. All 4 other objects don't have any errors:
    package blade.db;
    import blade.business.*;
     
    public class StaticBladeDB {
     
    	public static Blade getBlade(int modelNumber) {
     
    		Blade blade = new Blade();
    			blade.setModelNumber(modelNumber);
    			if(modelNumber == 1111 || modelNumber == 2222) {
    				Construction c = new Construction();
    				c.setModelNumber(modelNumber);
    				if(modelNumber == 1111) {
    					c.setName("Circular Enforcer");
    					c.setBladeColor("purple");
    					c.setPowerType("Electric");
    					c.setPrice(44.00);
    					c.setBladeType("Circular");
    					c.setBrand("DEWALT");
    					c.setBladeLength(10);
    					c.setDescription("20 times more RPM than a standard table saw, and diamond blades for extra durability.");
    					c.setDiamondEdged(true);
    				}
    				else if(modelNumber == 2222) {
    					c.setName("Long Nose King");
    					c.setBladeColor("green");
    					c.setPowerType("Battery");
    					c.setPrice(57.89);
    					c.setBladeType("Chained");
    					c.setBrand("Craftsman");
    					c.setBladeLength(18);
    					c.setDescription("Most energy efficient battery in its class.");
    					c.setDiamondEdged(false);
    				}
    			}
    			else if(modelNumber == 3333 || modelNumber == 4444) {
    				Kitchen k = new Kitchen();
    				k.setModelNumber(modelNumber);
    				if(modelNumber == 3333) {
    					k.setName("Fruit Master");
    					k.setBladeColor("iceblue");
    					k.setPowerType("Hand");
    					k.setPrice(59.54);
    					k.setBladeType("Knife");
    					k.setBrand("Henckels");
    					k.setBladeLength(12);
    					k.setDescription("Cut through any fruit, even coconut shells.");
    					k.setPurpose("Fruit preparation");
    				}
    				else if(modelNumber == 4444) {
    					k.setName("Meat Lovers 2000");
    					k.setBladeColor("HotPink");
    					k.setPowerType("Hand");
    					k.setPrice(89.90);
    					k.setBladeType("Knife");
    					k.setBrand("Chicago Cutlery");
    					k.setBladeLength(14);
    					k.setDescription("Smooth enought to cut 1/16th of an inch thick meat slices.");
    					k.setPurpose("slicing meat");
    				}
    			}
    			else if(modelNumber == 5555 || modelNumber == 6666) {
    				Survival s = new Survival();
    				s.setModelNumber(modelNumber);
    				if(modelNumber == 5555) {
    					s.setName("Critter Carver");
    					s.setBladeColor("Crimson");
    					s.setPowerType("Hand");
    					s.setPrice(47.56);
    					s.setBladeType("Knife");
    					s.setBrand("TAC Force");
    					s.setBladeLength(4);
    					s.setDescription("Made for gutting animals");
    					s.setSerrated(false);
    					s.setListOfToolsIncluded("Single blade only.");
    					s.setDeviceWeight(0.89);
    				}
    				else if(modelNumber == 6666) {
    					s.setName("All_In_One_Jimmy");
    					s.setBladeColor("Lime");
    					s.setPowerType("Hand");
    					s.setPrice(56.56);
    					s.setBladeType("Knife");
    					s.setBrand("MTECH USA");
    					s.setBladeLength(3);
    					s.setDescription("Open any food container, and enhanced blade tip lets you throw into a bear's skull.");
    					s.setSerrated(false);
    					s.setListOfToolsIncluded("Can opener, bottle opener, screw driver, mini blade, full sized blade.");
    					s.setDeviceWeight(0.89);
    				}
    			}
    			else if(modelNumber == 7777 || modelNumber == 8888) {
    				Weapon w = new Weapon();
    				w.setModelNumber(modelNumber);
    				if(modelNumber == 7777) {
    					w.setName("Steel Angel");
    					w.setBladeColor("Crimson");
    					w.setPowerType("Hand");
    					w.setPrice(47.56);
    					w.setBladeType("Sword");
    					w.setBrand("Cold Steel");
    					w.setBladeLength(31);
    					w.setDescription("Beautiful adamantium stainless steel Katana, with a 10-inch silicon wraped oak handle for extra grip.");
    					w.setDoubleEdged(false);
    					w.setMaterials("Adamantium stainless steel alloy blade, oak handle wrapped in silicon.");
    					w.setDeviceWeight(2.7);
    				}
    				else if(modelNumber == 8888) {
    					w.setName("Mr. Battle Saw");
    					w.setBladeColor("Crimson");
    					w.setPowerType("Gas");
    					w.setPrice(47.56);
    					w.setBladeType("Chained");
    					w.setBrand("Blades USA");
    					w.setBladeLength(30);
    					w.setDescription("Lightest gas powered saw in existence, designed to cut through thick metal armor in under 3 seconds.");
    					w.setDoubleEdged(true);
    					w.setMaterials("Black steel, diamond toothed blades.");
    					w.setDeviceWeight(11);
    				}
    			}
    			else if(modelNumber == 9999 || modelNumber == 1234) {
    				Yardwork y = new Yardwork();
    				y.setModelNumber(modelNumber);
    				if(modelNumber == 9999) {
    					y.setName("Lord Firewood");
    					y.setBladeColor("Battery");
    					y.setPowerType("Hand");
    					y.setPrice(47.56);
    					y.setBladeType("Chained");
    					y.setBrand("Greenworks");
    					y.setBladeLength(22);
    					y.setDescription("Fantastic black steel and up to 1000 full-blade-length-cuts per battery charge.");
    					y.setSawToothCount(6);
    					y.setDeviceWeight(5.8);					
    				}
    				else if(modelNumber == 1234) {
    					y.setName("Yard Infinity");
    					y.setBladeColor("Electric");
    					y.setPowerType("Hand");
    					y.setPrice(47.56);
    					y.setBladeType("Chained");
    					y.setBrand("Remington");
    					y.setBladeLength(13);
    					y.setDescription("Electric saw that will get the job done.");
    					y.setSawToothCount(6);
    					y.setDeviceWeight(21.8);
    				}
    			}
    	}
    }

    Here's the Construction child class:
    package blade.business;
    //This is a child class of the Blade class, dedicated to construction tools.
    public class Construction {
     
    	private boolean diamondEdged;
     
    	public Construction() {
    		super();
    		diamondEdged = false;
    	}
     
    	public void setDiamondEdged(boolean diamondEdged) {
    		this.diamondEdged = diamondEdged;
    	}
    	public boolean getDiamondEdged() {
    		return diamondEdged;
    	}
    }

    And here is the Blade parent class:
    package blade.business;
     
    import java.text.NumberFormat;
     
    //This is the superclass.
    //Subclasses include Survival, Weapon, Yardwork, Kitchen, and Workshop.
    //Later on, we will also add a ShoppingCart class.
    public class Blade {
     
    	private String name;
    	private int modelNumber;
    	private String bladeColor;
    	private String powerType;//electric, gas, battery, hand
    	private double price;
    	private String bladeType;//circular, sword, knife, chained, [don't forget boolean diamondBladed]
    	private String brand;
    	//[Survival: MTECH_USA, TAC_Force]
    	//[Weapon: Cold_Steel, BladesUSA]
    	//[Yardwork: Greenworks, Remington]
    	//[Kitchen: Chicago_Cutlery, Henckels]
    	//[Workshop: Craftsman, DEWALT]
    	private int length;
    	private String description;//override possibly needed "this diamond blade designed to sever solid wood as well as vines!"
     
    	//default constructor
    	public Blade() {
    		name = "";
    		modelNumber = 0;
    		bladeColor = "";
    		powerType = "";
    		price = 0.0;
    		bladeType = "";
    		brand = "";
    		length = 0;
    		description = "";
    	}
     
    	//overload constructor
    	public Blade(String name, int modelNumber, String bladeColor, String powerType, double price, 
    				String bladeType, String brand, int length,	String description) {
    		System.out.println("Creating a blade object.");
    		this.name = name;
    		this.modelNumber = modelNumber;
    		this.bladeColor = bladeColor;
    		this.powerType = powerType;
    		this.price = price;
    		this.bladeType = bladeType;
    		this.brand = brand;
    		this.length = length;
    		this.description = description;
    	}
     
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getName() {
    		return name;
    	}
     
    	public void setModelNumber(int modelNumber) {
    		this.modelNumber = modelNumber;
    	}
    	public int getModelNumber() {
    		return modelNumber;
    	}
     
    	public void setBladeColor(String bladeColor) {
    		this.bladeColor = bladeColor;
    	}
    	public String getBladeColor() {
    		return bladeColor;
    	}
     
    	public void setPowerType(String powerType) {
    		this.powerType = powerType;
    	}
    	public String getPowerType() {
    		return powerType;
    	}
     
    	public void setPrice(double price) {
    		this.price = price;
    	}
    	public double getPrice() {
    		return price;
    	}
     
    	public void setBladeType(String bladeType) {
    		this.bladeType = bladeType;
    	}
    	public String getBladeType() {
    		return bladeType;
    	}
     
    	public void setBrand(String brand) {
    		this.brand = brand;
    	}
    	public String getBrand() {
    		return brand;
    	}
     
    	public void setBladeLength(int length) {
    		this.length =  length;
    	}
    	public int getBladeLength() {
    		return length;
    	}
     
    	public void setDescription(String description) {
    		this.description = description;
    	}
    	public String getDescription() {
    		return description;
    	}
     
    	public String getValueFormatted() {
    		NumberFormat currency = NumberFormat.getCurrencyInstance();
    		String valueFormatted = currency.format(price);
    		return valueFormatted;
    	}
     
    	//Since the purpose of a bladed device may change (i.e. a chainsaw designed for murdering
    	//instead of yardwork), we should provide override methods just in case:
    	@Override
    	public String toString() {
    		return description;
    	}
    }

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

    Default Re: My second adventure of working with inheritance

    Your Construction class didn't extend Blade so none of the methods were inherited.

    Regards,
    Jim

  5. The Following User Says Thank You to jim829 For This Useful Post:

    SamJava_the_Hut (April 23rd, 2019)

Similar Threads

  1. My first adventure for working with inheritance
    By SamJava_the_Hut in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 22nd, 2019, 08:38 AM
  2. 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
  3. Type Adventure
    By c2burger in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 3rd, 2013, 08:39 AM
  4. 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
  5. 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