11 Replies - 1390 Views - Last Post: 16 December 2008 - 08:40 PM Rate Topic: -----

#1 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Very confused with abstract classes and methods

Posted 15 December 2008 - 02:34 PM

Just as i was getting used to them, i tripped up. I will quickly explain what i have. I have a base astract class called Person which obviously defines a Person
public abstract class Person
{
	private String firstName; 
	private String lastName;
	private static String title;
	private String dateOfBirth;
	private String homeAddress;
	private String phoneNumber;
	
	public static final String MR = "Mr";
	public static final String MISS = "Miss";
	public static final String MS = "Ms";
	public static final String MRS = "Mrs";
	public static final String DR = "DR";
	public static final String PROF = "Prof";

	public Person(){ 
		homeAddress = "123 Default Street";
		phoneNumber = "000-0000";
	}

	public Person(String firstName, String lastName, String title, String dateOfBirth, 
				  String homeAddress, String phoneNumber){
					  
		this.firstName=firstName;
		this.lastName=lastName;
		this.title=title;
		this.dateOfBirth=dateOfBirth;
		this.homeAddress=homeAddress;
		this.phoneNumber=phoneNumber;
		
	}
	
	public String getFirstName(){
		return firstName;
	}

	public String getLastName(){
		return lastName;
	}

	public String getTitle(){
		return title;
	}

	public String getBirthDate(){
		return dateOfBirth;
	}

	public String getAddress(){
		return homeAddress;
	}

	public String getNumber(){
		return phoneNumber;
	}
	
	public void setNumber(String phoneNumber){
		this.phoneNumber=phoneNumber;
	}
	
	public String allDetails()
	{
		String persDetails = "First Name: " + firstName +"\n" +
							 "Last Name: " + lastName +"\n" +
							 "Title: " + title +"\n" +
							 "D.O.B: " + dateOfBirth +"\n" +
							 "Address: " + homeAddress +"\n" +
							 "Contact Number: " + phoneNumber + "\n";
		return persDetails;
	}
	
	public String toString()
	{
		String persDetails = "First Name: " + firstName +"\n" +
							 "Last Name: " + lastName +"\n";
		return persDetails;
	}
}


I then have a abstract subclass of this called Borrower
public abstract class Borrower extends Person{
	
  public LibraryItem [] itemsBorrowed;
  private double currentFine;
  private int barCode;
 
  public LibraryItem [] getItemsBorrowed()
  {
	  return itemsBorrowed;
  }

  public double getCurrentFine()
  {
	  return currentFine;
  }
	
  public int getBarCode()
  {
	  return barCode;
  }

  public void incrementFine(double addFine)
  {
	  currentFine = currentFine+addFine;
  }
  
  public void decrementFine(double reduceFine)
  {
	  currentFine = currentFine-reduceFine;
  }
  
  public abstract void addItem();
  
  public void removeItem(LibraryItem[] item, int index)
  {
	for(int i =0; i <item.length; i ++)
	{
	item[index]= null;
	}
  }
  
  public String toString()
  {
		String borDetails =  "Bar code: " + barCode +"\n" +
							 "Items borrowed: " + itemsBorrowed +"\n";
		return borDetails;
  }
}


Now the Borrower class has an abstract method getItem(), which is implimented in Borrowers sub class, StudentBorrower
import java.util.Scanner;

public class StudentBorrower extends Borrower
{
	private final int maxItems = 7;
	private final double maxFine = 5.00;
	private String studentId;
	
	public void setId(String studentId)
	{
		this.studentId=studentId;
	}
	
	public int getMaxItems()
	{
		return maxItems;
	}
	
	public double getMaxFine()
	{
		return maxFine;
	}
	
	public String getID()
	{
		return studentId;
	}
	
	public void addItem()
	{
		if(maxItems>7)
		{
			System.out.println("The maximum number of items allowed is 7" + "\n" + "You currently have " + maxItems +
							   "out at the moment");
		}
		
		else if(maxFine>5.00)
		{
			System.out.println("The maximum fine allowed is 5.00" + "\n" + "You currently have " + maxFine +
							   "worth of fines");
		}
		
		else
		{	
		
		}
	}
}


Now this addItem method is supposed to add an LibraryItem which is another abstract class.
public abstract class LibraryItem{
	
	private String itemCode;
	private Date dueDate;
	private String homeLibrary;
	private String title;

	public LibraryItem(String itemCode, Date dueDate, String homeLibrary, String title)
	{
		this.itemCode=itemCode;
		this.dueDate=dueDate;
		this.homeLibrary=homeLibrary;
		this.title=title;
	}
	

	public void setDate(Date dueDate)
	{
		this.dueDate=dueDate;
	}
	
	public void setLibrary(String homeLibrary)
	{
		this.homeLibrary=homeLibrary;
	}
	
	public String getCode()
	{
		return itemCode;
	}
	
	public Date getDate()
	{
		return dueDate;
	}
	
	public String getLibrary()
	{
		return homeLibrary;
	}
	
	public String getTitle()
	{
		return title;
	}
	
	public String toString()
	{
		String libDetails =  "Item code: " + itemCode +"\n" +
							 "Item title: " + title +"\n";
		return libDetails;
	}
}



But the LibraryItem class is not extended by my StudentBorrower class, so how can i access its constructor in order to create an Item? Obviously i cant do LibraryItem item = new LibraryItem(......) as it cannot be instantiated. I cant do StudentBorrower item = new StudentBorrower(...) as there is no extension between them. So is there any way the constructor can be accessed?
cheers

Is This A Good Question/Topic? 0
  • +

Replies To: Very confused with abstract classes and methods

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Very confused with abstract classes and methods

Posted 15 December 2008 - 02:43 PM

ok, well in abstract classes, abstract methods dont have any implementation.you just right the method header followed by a semicolon..also abstract classes can only be subclassed

check this out, this should help you
http://java.sun.com/...I/abstract.html
Was This Post Helpful? 0
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Very confused with abstract classes and methods

Posted 15 December 2008 - 02:52 PM

I understand about abstract classes and methods, i think i only have 1 abstract method and it is implimented in its subclass, and obviously my Person base abstract class will be a subclass of javas Object class. What i am trying to find out is whether it is possible to access a constructor in an abstract class, from a class which isnt a subclass of this abstract class.
Basically, my StudentBorrower class needs to create a LibraryItem object, but they are not subclassed.
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Very confused with abstract classes and methods

Posted 15 December 2008 - 03:16 PM

ahhh, i totally forgot about that....i'll look it up

This post has been edited by mostyfriedman: 15 December 2008 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Very confused with abstract classes and methods

Posted 15 December 2008 - 06:47 PM

I have done a bit of research, and it looks like my StudentBorrower class would need to be a subclass of LibraryItem in order to create a LibraryItem object. I dont know if anyone else would have an opion on this. Maybe i have read the wrong information?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Very confused with abstract classes and methods

Posted 15 December 2008 - 07:11 PM

How would a LibraryItem extends a Student ?
You are missing something here

Abstract class just says that you cannot instantiate it
It can have non abstract methods with code or abstract methods without code that should be overload
Was This Post Helpful? 0
  • +
  • -

#7 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Very confused with abstract classes and methods

Posted 15 December 2008 - 07:19 PM

The problem is that a StudentBorrower borrows a LibraryItem. There is no extending between the two classes but somehow an instance would surely have to be made in order to create a LibraryItem for the StudentBorrower. But LibraryItem is an abstract class, which means i cannot instantiate it (unless subclassed?)
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Very confused with abstract classes and methods

Posted 15 December 2008 - 08:23 PM

View Postnick2price, on 15 Dec, 2008 - 06:19 PM, said:

The problem is that a StudentBorrower borrows a LibraryItem. There is no extending between the two classes but somehow an instance would surely have to be made in order to create a LibraryItem for the StudentBorrower. But LibraryItem is an abstract class, which means i cannot instantiate it (unless subclassed?)

So if LibraryItem is an abstract class (your design/architecture) you need a Book class that extends LibraryItem and use it
Was This Post Helpful? 0
  • +
  • -

#9 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Very confused with abstract classes and methods

Posted 16 December 2008 - 02:03 PM

oh, ok, i get you. So if i have a book class which extends a LibraryItem, if the book class is not abstract, can it be instantiated, even if it impliments abstract methods?
Was This Post Helpful? 0
  • +
  • -

#10 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Very confused with abstract classes and methods

Posted 16 December 2008 - 02:13 PM

Correct.

That's what they're for. You can extends them so you can change the implementation for specific inheritance situations.

You can then instantiate an object of the inheriting class and use the abstract methods you implemented in that class.
Was This Post Helpful? 0
  • +
  • -

#11 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Very confused with abstract classes and methods

Posted 16 December 2008 - 04:02 PM

Ok, this is becoming pretty straight forward. I just wanted to quickly clarify somthing to make sure i am correct. So i have an abstract class called Person which has a constructor which creates a Person Object.
I have a subclass of Person called Borrower which also has a constructor that is passed values. This class is not abstract.
I then have a concrete class call StudentBorrower which does not extend any of the above two classes. In order for me to create a Person object in this class, i have to instantiate the Borrower class dont i and not the abstract Person class? e.g.
Borrower.Person(...) (Not sure if thats the correct syntax to access the Person constructor)
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Very confused with abstract classes and methods

Posted 16 December 2008 - 08:40 PM

View Postnick2price, on 16 Dec, 2008 - 03:02 PM, said:

Ok, this is becoming pretty straight forward. I just wanted to quickly clarify somthing to make sure i am correct. So i have an abstract class called Person which has a constructor which creates a Person Object.

So far so good

View Postnick2price, on 16 Dec, 2008 - 03:02 PM, said:

I have a subclass of Person called Borrower which also has a constructor that is passed values. This class is not abstract.

If it does not have a constructor it will use the ones of its father Person


View Postnick2price, on 16 Dec, 2008 - 03:02 PM, said:

I then have a concrete class call StudentBorrower which does not extend any of the above two classes. In order for me to create a Person object in this class, i have to instantiate the Borrower class dont i and not the abstract Person class?

Even if you wanted you CANNOT instantiate an abstract class so no way to build a new Person()

View Postnick2price, on 16 Dec, 2008 - 03:02 PM, said:

e.g.
Borrower.Person(...) (Not sure if thats the correct syntax to access the Person constructor)

Borrower() thats it !. Borrower will automatically call the Person() constructor that will call the Object() constructor from which all Java classes are sons.

If you look in the API for JButton (as an example)

Class JButton
java.lang.Object
  java.awt.Component
	  java.awt.Container
		  javax.swing.JComponent
			  javax.swing.AbstractButton
				  javax.swing.JButton



when you code JButton x = new JButton("My Button");

the constructor of JButton is called which calls the constructor or AbstractButton
which calls the constructor of JComponent
which calls the constructor of Container
which calls the constructor of Component
which calls the constructor of Object
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1