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

Thread: Problem with inheritance??

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with inheritance??

    Codes:

    public class Customers
    {
        int accID;
        String name;
        String address;
        String dob;
        String phone;
        double accBalance;
     
       public Customers(int accID, String name, String address, String dob, String phone, double accBalance)
        {
            this.accID=accID;
            this.name=name;
            this.address=address;
            this.dob=dob;
            this.phone=phone;
            this.accBalance=accBalance;
     
        }
     
        public Customers()
        {
        }
     
        public void set_accID(int new_accID)
        {
            accID=new_accID;
        }
     
        public int get_accID()
        {
            return accID;
        }
     
        public void set_name(String new_name)
        {
            name=new_name;
        }
     
        public String get_name()
        {
            return name;
        }
     
     
        public void set_addr(String new_addr)
        {
            address=new_addr;
        }
     
        public String get_addr()
        {
            return address;
        }
        public void set_dob(String new_dob)
        {
            name=new_dob;
        }
     
        public String get_dob()
        {
            return dob;
        }
     
        public void set_phone(String new_phone)
        {
            phone=new_phone;
        }
     
        public String get_phone()
        {
            return phone;
        }
     
        public void set_accBal(double new_bal)
        {
            accBalance=new_bal;
        }
     
        public double get_accBal()
        {
            return accBalance;
        }
     
        public void print()
        {
            System.out.println("Account no: " + accID);
            System.out.println("Customer Name: "+name);
            System.out.println("Address: "+address);
            System.out.println("Date of Birth: "+dob);
            System.out.println("Phone: " +phone);
    		System.out.print("Avaliable Balance: ");
    		System.out.printf("%1$.2f", accBalance);
    		System.out.println("");
     
     
     
        }
     
     
     
    }

    public class Account1 extends Customers	//Saving account
    {
    	private String accType;
    	private double rate = 0.0;
     
    	public Account1(int accID, String name, String address, String dob, String phone, double accBalance,String accType)
    	{
    		super(accID,name,address,dob, phone, accBalance);
    		this.accType=accType;
     
     
    	}
     
    	public void set_accType(String new_type)
    	{
    		accType=new_type;
    	}
     
    	public String get_accType()
    	{
    		return accType;
    	}
     
    	public void setRate(double new_rate)
    	{
    		rate=new_rate;
    	}
     
    	public double get_rate()
    	{
    		return rate;
    	}
     
    	public void print()
        {
            super.print();
            System.out.println("Account Type: "+accType);
            System.out.println("Interest Rate: "+rate);
     
        }
     
     
    }

     
     Customers[] custTxt = new Customers[30];
     
    custTxt[index]=new Account1(accID,name.toUpperCase(),address,dob,phone,accBal,accType);

    when i do a custTxt[i].get_accType();

    it show an error;

    java:212: cannot find symbol
    symbol  : method get_accType()
    location: class Customers
                        smallBuff.write("Account type: "+custTxt[i].get_accType());

    need help on how to show this error...


  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: Problem with inheritance??

    Inheritance works down, not up.

    get_accType() is a member of Account1, therefore cannot be access from a Customer because not all customers necessarily have a method called get_accType(). There are two options:

    1. Cast the object to Account1. You should only use this if you know you're going to have an Account1 object.
    2. Move get_accType() into the Customers class (either as a fully defined method, or as an abstract method).

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with inheritance??

    sorry to post this question here...

    Codes for BankDB
    public Customers[] readFile(String filename)throws FileNotFoundException
     
    		{
    		scStream = new Scanner(new File(filename));
    		int record =0;
     
    		boolean flag=true;
    		String bData="";
    		String name="",accID1="",accBal1="",phone="",address="",dob="",accType="",rate1="";
    		double accBal=0.0, rate=0.0;
    		int accID=0;
    		try
    		{
    			if (scStream == null )
    			{
    				System.out.println("NOT FILE OPENED");
    				return null;
    				}
    			else
    			{
    					while (scStream.hasNext())
    					{
     
    						bData=scStream.nextLine();
    						if(bData.startsWith("Account Id"))
    						{
    							accID1=bData.substring((2+ (bData.indexOf("= "))));
    							accID=Integer.parseInt(accID1);
    							flag = checkAcc(accID);
    							if (flag==false)
    							{
    								builder.append("Acc ID:"+accID+" has already being created\n");
    							}
     
    						}	
    						else if(bData.startsWith("Name"))
    						{
    							name=bData.substring((2+ bData.indexOf("= ")));
    							if (name.length()>20)
    							{	
    								flag =false;
    								builder.append("The name: "+name+" exceed the 20 character words limit\n");
     
    							}
     
     
    						}
    						else if(bData.startsWith("Address"))
    						{
    							address=bData.substring((2+ bData.indexOf("= ")));
     
    						if (address.length()>80)
    							{
    								flag =false;
    								builder.append("the address: "+address+" exceeed the 80 characters limit\n");
    							}
     
    						}	
    						else if(bData.startsWith("DOB"))
    						{
    							dob=bData.substring((2+ bData.indexOf("= ")));
     
    						if (dob.length()>10)
    							{
    								flag =false;
    								builder.append("the DOB: "+dob+"exceed the 10 characters limit\n");
    							}
     
    						}	
    						else if(bData.startsWith("Phone Number"))
    						{
    							phone=bData.substring((2+ bData.indexOf("= ")));
     
    						if (phone.length()>8)
    							{
    								flag =false;
    								builder.append("the phone: "+phone+"exceed the 8 characters limit\n");
    							}
     
    						}	
    						else if(bData.startsWith("Account Balance"))
    						{
    							accBal1=bData.substring((2+ bData.indexOf("= ")));
    							accBal=Double.parseDouble(accBal1);
     
     
    						}
    						else if(bData.startsWith("Account Type"))
    						{
    							accType=bData.substring((2+bData.indexOf("= ")));
     
    							if (accType.equalsIgnoreCase("Saving"))
    							{
     
    								if (flag==true) 
    								{
    									custTemp[index]=new Account1(accID,name.toUpperCase(),address,dob,phone,accBal,accType);
    									index++;
    									record++;
    								}
    								else
    								{
    									System.out.println(builder.toString());
    								}
    							}
    							else if (accType.equalsIgnoreCase("checking"))
    							{
    								if (flag==true)
    								{
    									custTemp [index]= new Account2(accID, name.toUpperCase(),address, dob,phone, accBal,accType);
    									index++;
    									record++;
    								}
    								else
    								{
    									System.out.println(builder.toString());
    								}
    							}
    						}	
    						else if (bData.startsWith("Fixed Daily Interest"))
    						{
    							rate1=bData.substring((2+bData.indexOf("= ")));
    							rate =Double.parseDouble(rate1);
    							if (flag==true)
    							{
    									custTemp [index]= new Account3(accID, name.toUpperCase(),address, dob,phone, accBal,accType,rate);
    									index++;
    									record++;
    							}
    							else
    							{
    							System.out.println(builder.toString());
    							}
    						}						
    				}
    			}
     
    		}
    		catch (Exception e)
    		{
    			System.out.println("Error reading file...." +e.getMessage());
    		}
    		System.out.println(record + " records read");
     
     
     
    		return custTemp;
    	}
    Main class codes frag
    public static void main(String[] args) 
        {
     
    			double new_interest=0.0;
     
    			BankDB custData = new BankDB();
    			Scanner input = new Scanner (System.in);
    			int accID=0;
    			int index=0;
      			String fileName="";
      			char userInput='0';
      			while (true)
      			{
      				System.out.println("First Bank Customer Query System..");
      				System.out.println("==================================");
      				System.out.println("(1)	Input Data from text file");
      				System.out.println("(2)	Print out Customer Info onto console");
      				System.out.println("(3)	Write Customer Info to a text file");
      				System.out.println("(4)	Delete data");
      				System.out.println("(5) Update account");
      				System.out.println("(Q)	Quit the system");
      				System.out.println("Your choice:");
      				userInput=input.next().charAt(0);
      				try
      				{
      				switch(userInput)
      				{
      					case '1':
      						String[] tempNames=new String [30];
     
      						System.out.println("Please enter filename:");
    						fileName= input.next();
    						custTemp=custData.readFile(fileName);
    						for(int i=0; i<custTemp.length;i++)
    						{
    							tempNames[i]=custTemp[i].get_name();
     
    						}
    						Arrays.sort(tempNames);
    						custTxt.clear();
    						for(int j=0;j<tempNames.length;j++)
    						{
    							for (int i=0; i<custTemp.length;i++)
    							{
    								if(tempNames[j].equalsIgnoreCase(custTemp[i].get_name()))
    								{
    									custTxt.add(j,custTemp[i]);
    								}
    							}	
    						}
     
     
    						break;

    what these codes is supposed to do is get some data from a text file and put them into an user define type array, sort them into alphabetical order and lastly insert them into an ArrayList...

    my logic error faced is that when i do
    System.out.println(custTxt.get(1).print());
    the contain is empty.. but if i do this instead:
    System.out.println(custTemp[1].print());
    there are values printed out.. need help on how to solve this...

    thx

Similar Threads

  1. Inheritance and Overriding help!
    By Knserbrave in forum Object Oriented Programming
    Replies: 4
    Last Post: February 24th, 2011, 01:46 PM
  2. Inheritance questions....
    By smellyhole85 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 26th, 2010, 06:11 PM
  3. inheritance help
    By justin3492 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 07:45 PM
  4. inheritance
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2010, 09:23 PM
  5. Problem with OOP - Inheritance
    By connex in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2009, 11:11 PM