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

Thread: inheritance - why its not working?

  1. #1
    Junior Member
    Join Date
    Sep 2018
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default inheritance - why its not working?

    Hi,
    I wrote a class named box With a variable named volume.
    This is the first class, very simple:


    package inheritance_Ex_2;
     
    public class Box {
    	private String Label;
    	private double volume;
     
    	public Box(String label) {
    		super();
    		Label = label;
    		this.volume = volume;
    	}
     
    	public void Volume(double volume) {
    		this.volume = volume;


    Then I wrote a class that inherite from Box and Sets a value to volume:


    package inheritance_Ex_2;
     
    public class PlasticBox extends Box {
    	private int length;
    	private int wigth;
    	private int hight;
     
    	public PlasticBox(String label, int length, int wigth, int hight) {
    		super(label);
    		this.length = length;
    		this.wigth = wigth;
    		this.hight = hight;
    	}
     
    	public void setVolume () {
    		Volume(getLength() * getWigth() * getHight());
    	}

    But when I write main class and try to use the setVolume function its not working:


     
    package inheritance_Ex_2;
     
    public class BoxProgram {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Box plastic1 = new PlasticBox("grapes", 4, 4, 4);
     
    		plastic1.setVolume();
     
    		plastic1.BoxData();
     
    	}
     
    }




    its write me that that there is no "setVolume" in Box class.
    its right, there is no "setVolume" in Box class but there is "setVolume" in PlasticBox class.
    I suppose that the problam is in this line:

    Box plastic1 = new PlasticBox("grapes", 4, 4, 4);


    and when I chage it to - PlasticBox plastic1 = new PlasticBox("grapes", 4, 4, 4);
    it fix the problem but I dont understand why.



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

    Default Re: inheritance - why its not working?

    It tells you that because the setVolume() class is in the subclass, not the inherited class of Box. When you assign a instance of a subclass to its parent class, the only methods visible are those of the parent class.

    Regards,
    Jim

Similar Threads

  1. Inheritance[HELP!]
    By cjasonpogi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2013, 02:56 AM
  2. Replies: 7
    Last Post: April 25th, 2013, 01:12 PM
  3. 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
  4. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM