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: A Question Regarding Abstract Classes & Packages

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A Question Regarding Abstract Classes & Packages

    Hello all,

    I'm just playing around with Java and trying to understand how abstract classes and methods work along with packages. I began writing some simple classes to understand how abstract classes and methods work :

    Person Abstract Class :
    public abstract class Person {
    	protected String name;
    	abstract void setName(String name);
    	abstract void displayName();	
    }

    Dave Class implementing Person :
    public class Dave extends Person {
     
    	public Dave(){
    		name = "Dave";
    	}
     
    	public void setName(String name){
    		this.name = name;
    	}
     
    	public void displayName(){
    		System.out.println("My name is " + name);
    	}
    }

    And finally DaveTest class:
    public class DaveTest {
     
    	public static void main (String args[]){
    		Person p = new Dave();
    		p.displayName();
    	}
    }

    Placing all three files in the same directory, they compile and execute. (See Dave1.zip for the files)

    Now I wanted to put Person and Dave classes into a package of their own, so altered them to the following :
    Person
    package person;
    public abstract class Person {
    	protected String name;
    	abstract void setName(String name);
    	abstract void displayName();	
    }

    Dave
    package person;
     
     
    public class Dave extends Person {
     
    	public Dave(){
    		name = "Dave";
    	}
     
    	public void setName(String name){
    		this.name = name;
    	}
     
    	public void displayName(){
    		System.out.println("My name is " + name);
    	}
    }

    DaveTest2
    import person.Dave;
    import person.Person;
     
    public class DaveTest2 {
     
    	public static void main (String args[]){
    		Person p = new Dave();
    		p.displayName();
    	}
    }

    I put Person and Dave in a directory 'person' off the directory where DaveTest2 is located (see Dave2.zip for the location) but now when i try to compile DaveTest2, the compiler complains that displayName() method is not public ! :

    DaveTest2.java:9: displayName() is not public in person.Person; cannot be accessed from outside package
    p.displayName();
    ^
    1 error
    So I don't understand why DaveTest2 will not compile, all I have done is placed the classes into a package ! The only way round the error is to cast the object as (Dave) where it has been implemented but this seems ridiculous. What if i don't know the subclass I'm dealing with but I do know that it will have a displayName() implementation. I can't see the point of using abstract methods then if they are not available outside of a package.

    Could someone possibly explain this or the error of my ways ?
    Attached Files Attached Files


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: A Question Regarding Abstract Classes & Packages

    The reason this is happening is because of Polymorphism. p was declared a person, so no matter what type of object it is holding (say, a Dave object) it will think it's a person. In the person class you have the default method protection for displayName(), which means any class outside of the person package won't be able to use that method.

    [b]public[/b] abstract void displayName();

    As a side note on polymorphism, you can try this and it will compile (and possibly run) just fine:

    ((Dave) p).displayName();

    Note however, p could be any object that inherits from Person, and doing this ruins the reason for having polymorphism, as well as will cause the program to crash if you tried to put say a Scott object into p.
    Last edited by helloworld922; December 10th, 2009 at 11:15 AM.

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A Question Regarding Abstract Classes & Packages

    thanks for that. I had actually tried to make the method displayName() public in the abstract class but the compiler complained that it was an illegal usage of the key word (when use in conjunction with abstract).

    I tried it again and it compiled ! Weird !

    But now I have another problem.

    If i try compiling the implementing class Dave.java alone, the compiler complains it cannot find Person

    javac Dave.java

    Dave.java:2: cannot find symbol
    symbol: class Person
    public class Dave extends Person {
    ^
    Dave.java:5: cannot find symbol
    symbol : variable name
    location: class person.Dave
    name = "Dave1";
    ^
    Dave.java:9: cannot find symbol
    symbol : variable name
    location: class person.Dave
    this.name = name;
    ^
    Dave.java:13: cannot find symbol
    symbol : variable name
    location: class person.Dave
    But if I do javac *.java everything compiles ok !! That doesn't seem right to me !

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: A Question Regarding Abstract Classes & Packages

    When you have classes in different packages that need to be compiled together, you need to understand how Java handles packages. By default, the Java compiler treats packages as relative to where it is being run from - that is, it expects the packages it encounters to be in subdirectories of where it is running. When you run it from the 'person' package directory, it finds Dave.java because you told it to compile Dave.java. When it encounters person.Person in the Dave.java code, it looks for a subdirectory called 'person' containing class Person.java. It doesn't find it because it is already running in the person directory.

    There are two ways to fix this - either run the compiler from the 'package root' directory - the directory that contains all the packages, or use the classpath option to tell javac where to find the package root directory.

    For example, running javac from the package root directory:

    javac person\Dave.java

    In practice, for non-trivial applications, the classpath option is generally used, because it means you can run javac from anywhere and it will know where to find the other classes:

    c:\>javac -classpath c:\projects\exercise1\src c:\projects\exercise1\src\person\Dave.java

    Above, the compiler is running from the root of drive c: with a classpath to the package root (bolded) and it is being asked to compile the Dave.java file. It will now use the classpath to find all the files referred to by Dave.java. You can add other package root directories to the classpath by separating them with a semicolon ';'.

Similar Threads

  1. importing packages..
    By chronoz13 in forum Java IDEs
    Replies: 4
    Last Post: November 23rd, 2009, 05:49 PM
  2. connecting two classes?
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 1st, 2009, 03:15 PM
  3. [SOLVED] Error "TitleChanger is abstract; cannot be instantiated"
    By Uzual in forum AWT / Java Swing
    Replies: 2
    Last Post: May 26th, 2009, 11:23 AM
  4. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 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