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: Please Help stuck on Inheritances and abstract classes.

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please Help stuck on Inheritances and abstract classes.

    I am not asking you to do my homework. I am legitlly stuck on this and cannot figure out how to fix it. It seems that i cannot get the packages to see each other. Thanks for any Help.

    Using Jgrasp


    public abstract class Book
    {
    	private String title;
    	private double price;
     
    	public abstract void setPrice();
    	public double getPrice()
            {
                return price;
            }
     
    	public void setTitle(String bk_title)
            {
                title = bk_title;
            }
    	public String getTitle()
            {
                return title;
            }
     
    }


    import Book.java;
     
    public class Fiction extends Book
    {
    	Fiction fic = new Fiction();
    	public void setPrice(double bk_price)
    	{
    		price = bk_price;
    	}
     
    	fic.getPrice()
    	{
    		return price;
    	}
    }

    public class Nonfiction extends Book
    {
    Nonfiction nonfic = new Nonfiction();
    nonfic.setPrice(double)
    {
    price = 37.99;
    }
     
    nonfic.getPrice()
    {
    return price;
    }
    }

    import Book.java;
    import Fiction.java;
    import NonFiction.java;
     
    public class UseBook {
    public static void main(String[]args)
    {
     
    System.out.println("price for fiction: $" + price);
    System.out.println("price for nonfiction: $" + bk_price);
    }
    }


    Here are the errors:

    UseBook.java:1: error: package Book does not exist
    import Book.java;
    ^
    UseBook.java:2: error: package Fiction does not exist
    import Fiction.java;
    ^
    UseBook.java:3: error: package NonFiction does not exist
    import NonFiction.java;
    ^
    UseBook.java:9: error: cannot find symbol
    System.out.println("price for fiction: $" + price);
    ^
    symbol: variable price
    location: class UseBook
    UseBook.java:10: error: cannot find symbol
    System.out.println("price for nonfiction: $" + bk_price);
    ^
    symbol: variable bk_price
    location: class UseBook
    5 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Please Help stuck on Inheritances and abstract classes.

    See Using Package Members (The Java™ Tutorials > Learning the Java Language > Packages)
    If all classes are in the same package, you do not need to explicitly import the classes. And further, the import is by class name, not by file source name (MyClass rather than MyClass.java)

Similar Threads

  1. what is the use of interfaces and abstract classes?
    By sagar474 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 18th, 2011, 02:34 PM
  2. Interfaces Vs Abstract classes
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: April 20th, 2011, 07:49 AM
  3. Issue with abstract classes
    By zaphod2003 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 7th, 2011, 02:25 AM
  4. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 06:15 AM
  5. Java program with abstract class along with two subclasses
    By crazydeo in forum Collections and Generics
    Replies: 2
    Last Post: June 10th, 2008, 11:45 AM