#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Inheriting members problem using netbeans.


    I am going through the book "learn java for android development 2nd edition" using netbeans as my IDE. The section i am at is dealing with inheritance from one class to another. My issue is, how do i get "Car" class to be accessed inside this vehicle class. The error on Car shows "class Car is public, should be declared in a file named Car.java"
    Specific Code:

    public class Car extends Vehicle
    {
    private int numWheels;

    Car(String make, String model, int year, int numWheels)
    {
    super(make, model, year);
    this.numWheels = numWheels;
    }

    Attempted solutions:
    1) tried importing the car package to vehicle project
    2) imported the car.jar to the vehicle package
    3) copied the car.java file to the source packages tree and ran a refactor

    I did search through other public information sites before i came here, i am kinda stuck. Any tips would be greatly appreciated.

    Thanks
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    class Car is public, should be declared in a file named Car.java"
    Rename the java file with the public class Car definition to: Car.java as described in the error message.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0
    I have two packages one is Car and other is Vehicle, both packages have a .java file associated with them.

    The Car package is a public class file, the Vehicle package is a class file which is trying to call the constructors in the Car class file, the problem i am having is accessing the car.java file.

    I tired to add import car; or import Car; neither seem to work, an error shows up '.' expected. When i add .*; to the import file it says Package car/Car does not exist. What a i missing here?
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Please make small programs and post the code you are talking about. Be sure to give the names of the java files and the folders that the files are in.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0
    Removed the encapsulations, thanks for the help.

    Vehicle.java
    folder = NetBeansProjects\Vehicle\build\classes\vehicle\
    Error(s)
    1) when i try to use
    Code:
    class Car extends Vehicle
    Car shows an error "class Car is public, should be declared in a file named Car.java"
    2) If i add
    Code:
     import Car.*;
    to try to import the Car class i get the error "package Car does not exist, Unused Import"
    Fix: If i have the IDE change the class name from Vehicle.java to Car.java, it fixes the errors but does not help with the problem. I am trying to access the Car class from inside the Vehicle class file.
    Code:
    package vehicle;
    
    class Vehicle {
    
        private String make;
        private String model;
        private int year;
        
        Vehicle(String make, String model, int year)
        {
            this.make = make;
            this.model = model;
            this.year = year;
        }
        
    }
        public class Car extends Vehicle
        {
            private int numWheels;
            
            Car(String make, String model, int year, int numWheels)
            {
                super(make, model, year);
                this.numWheels = numWheels;
            }
        public static void main(String[] args) {
            Car car = new Car("Ford", "Fiesta", 2009, 4);
            System.out.println("Make = " + car.getMake());
            System.out.println("Model = " + car.getModel());
            System.out.println("Year = " + car.getYear());
            System.out.println("Number of wheels = " + car.numWheels);
        }
    }
    Car.java folder = NetBeansProjects\Car\build\classes\car\
    Just kept the constructors.

    Code:
    package car;
    
    public class Car 
    {
        String make;
        String model;
        int numDoors;
        static int counter;
        
        Car(String make, String model)
        {
            this(make, model, 4);
            System.out.println("first constructor activated");
        }
        
        Car(String make, String model, int numDoors)
        {
            this.make = make;
            this.model = model;
            this.numDoors = numDoors;
            System.out.println("second constructor activated");
        }
        
       public static void main(String[] args) 
        {
        }
    }
    edit: added folder names, code tags, and exact errors, and added a possible solution.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What error messages are you getting? Copy the full text and paste it here.

    Please edit the post and wrap the code in code tag.

    If your problem is with configuring your IDE, I can't help you. I use a much simpler IDE that requires me to setup the classpath etc when compiling.
    Last edited by NormR; November 23rd, 2013 at 07:22 AM.

IMN logo majestic logo threadwatch logo seochat tools logo