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

Thread: is it possible to extends java class with another class from different package?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default is it possible to extends java class with another class from different package?

    Hi, i m making a national database program for practice.

    in general package i have a class with gen infos like name dob etc.

    now i want to create different packages for different departments like medical, insurance, license etc. so that i can include the gen infos in all the departments.

    is it possible to make different packages for all departments and then extend it with the class from different packagee?

    import java.util.Scanner;
    public class general {
     
    	String name;
    	String fname;
    	int dob;
    	char sex;
    	public general()
    	{
    		this("no","no",0,'n');
    	}
     
    	public general(String string, String string2, int i, char c) {
    		// TODO Auto-generated constructor stub
    	}
     
    	protected void addgeninfo()
    	{
    		Scanner inp = new Scanner(System.in);
     
    		System.out.println("Please Enter your name:");
    		name=inp.next();
     
    		System.out.println("Please Enter your Father's name:");
    		fname=inp.next();
     
    		System.out.println("Please Enter your dob(format:DDMMYYYY):");
    		dob=inp.nextInt();
     
    		String temp;
    		System.out.println("Please Enter your sex(m/f):");
    		temp=inp.next();
    		sex =temp.charAt(0);
     
    	}
     
    	public static void main(String[] asgc)
    	{
    		//int totnationals=10;
    		System.out.println("Welcome to National Database");
    		general nationals = new general();
     
    		nationals.addgeninfo();
     
     
     
     
    	}
    }

    gv2sv.jpg
    Last edited by fredsilvester93; July 18th, 2012 at 08:14 PM.


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: is it possible to extends java class with another class from different package?

    So if I am understanding you correctly, you are asking if you can create a class that inherits from another class that is located in a different package. The answer is "yes" as long as that other class has public visibility and has a constructor with public visibility and is not immutable, then this should work fine.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: is it possible to extends java class with another class from different package?

    when i try to extend to general in different package it gives error

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: is it possible to extends java class with another class from different package?

    Quote Originally Posted by fredsilvester93 View Post
    when i try to extend to general in different package it gives error
    We can't read minds, and so if you need our help, you'll need to tell us more. Likely the error is not due to classes being in different classes, but honestly given what you've posted, I have no idea what your error is. Specifically please show the offending code and the full error message.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: is it possible to extends java class with another class from different package?

    my one package name is default package. another package name is nationalidentityinformation. can chck the image i have posted above.

  6. #6
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: is it possible to extends java class with another class from different package?

    I would recommend that you not use the default package (meaning that it is in no package at all), that you put all classes in appropriate packages, and then give it a try again. Please let us know if this fixes things.

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: is it possible to extends java class with another class from different package?

    well.. it helped after i made class public thanks alot mate

Similar Threads

  1. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM
  2. Replies: 4
    Last Post: December 6th, 2011, 02:24 PM
  3. Replies: 1
    Last Post: June 13th, 2011, 07:02 PM
  4. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM
  5. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 10:40 PM