Results 1 to 3 of 3
  1. #1
    kingfisher is offline Member
    Join Date
    Oct 2015
    Posts
    7
    Rep Power
    0

    Default Error in basic example

    I have tried inheritance example. But it's not working. Help please. I created 3 different classes
    class 1:
    public class caseShenzheng {
    protected int iphone;
    protected int samsung;
    protected int tablet;
    public void cases(int a, int b, int c){
    iphone=a;
    samsung=b;
    tablet=c;
    }

    }

    ----------------------------------------------
    class2:
    public class priceShenzheng extends caseShenzheng {
    protected final double priceIphone=1.5;
    protected final double priceSamsung=2.5;
    protected final double priceTablet=3.5;


    public double prices(){

    return ((double)(iphone*priceIphone)+(double)(samsung*pri ceSamsung)+(double)(tablet*priceTablet));

    }

    }
    -------------------------------------------------
    class 3:

    public class inheritance1 {
    public static void main(String[] args){
    caseShenzheng ca = new caseShenzheng();
    priceShenzheng pr = new priceShenzheng();
    ca.cases(10, 10, 10);
    pr.prices();
    System.out.println(pr.prices());
    }

    }



    Am getting output as 0.0, which is not expected.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Error in basic example

    ca and pr are two different objects. Their state is not shared between the two. Use pr.cases(10,10,10) instead.
    And remove the line pr.prices() as it doesn't do anything.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    kingfisher is offline Member
    Join Date
    Oct 2015
    Posts
    7
    Rep Power
    0

    Default Re: Error in basic example

    Quote Originally Posted by jim829 View Post
    ca and pr are two different objects. Their state is not shared between the two. Use pr.cases(10,10,10) instead.
    And remove the line pr.prices() as it doesn't do anything.

    Regards,
    Jim
    Hey jim,

    That helped. Thank you for taking time to help me.

Similar Threads

  1. Please help me! Basic java coding error!
    By Skater1024 in forum New To Java
    Replies: 13
    Last Post: 11-09-2015, 08:04 PM
  2. Error when running a basic server setup
    By willemjav in forum Networking
    Replies: 8
    Last Post: 09-10-2013, 10:30 PM
  3. Really basic error: cannot find symbol
    By gjwcarsaig in forum New To Java
    Replies: 10
    Last Post: 07-31-2011, 09:50 AM
  4. Compilation Error in basic java class
    By ruchir123 in forum New To Java
    Replies: 7
    Last Post: 11-21-2010, 11:23 PM
  5. Basic Java applet error, cant figure it out!
    By alacn in forum Java Applets
    Replies: 3
    Last Post: 08-03-2010, 06:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •