12 Replies - 4273 Views - Last Post: 01 March 2010 - 05:56 PM Rate Topic: -----

#1 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Calling a method of the super class by using

Posted 27 February 2010 - 06:40 PM

I have overwritten Object's toString() method with my own that are customized for my application. What I want to do is use Product's toString() method, if possible, by using an object of class Book. So my question is if this is possible, how would I accomplish it.

Thanks,
red
/* Product Class
*
*
*/

public class Product
{
	private String language;
	private String description;
	private double price;
	protected static int count = 0;
	
	public Product()
	{
		language = null;
		description = null;
		price = 0;
	}
	public Product(String language, String description, double price)
	{
		this.language = language;
		this.description = description;
		this.price = price;
	}
	
	public void setLanguage(String language)
	{
		this.language = language;
	}
	public void setDescription(String description)
	{
		this.description = description;
	}
	public void setPrice(double price)
	{
		this.price = price;
	}
	public String getLanguage()
	{
		return language;
	}
	public String getDescription()
	{
		return description;
	}
	public double getPrice()
	{
		return price;
	}
	public static int getCount()
	{
		return count;
	}
	public String toString()
	{
		String message = "Language: " + language + "\n" +
						 "Description: " + description + "\n" +
						 "Price: " + price + "\n";
		return message;
	}
}


/*Book Class
*
*
*/

public class Book extends Product
{
	
	private String author;
	private String genre;
	private int NumberOfBooks;
	
	public Book()
	{
		super(); // Call constructor of Product Superclass
		author = null;
		genre = null;
		NumberOfBooks = 0;
		count++;
	}
	public Book(String author, String genre, int NumberOfBooks)
	{
		super();
		this.author = author;
		this.genre = genre;
		this.NumberOfBooks = NumberOfBooks;
		count++;
	}
	
	public void setAuthor(String author)
	{
		this.author = author;
	}
	public void setGenre(String genre)
	{
		this.genre = genre;
	}
	public void setNumberOfBooks(int NumberOfBooks)
	{
		this.NumberOfBooks = NumberOfBooks;
	}
	public String getAuthor()
	{
		return author;
	}
	public String getGenre()
	{
		return genre;
	}
	public double getNumberOfBooks()
	{
		return NumberOfBooks;
	}
	public String toString()
	{
		String message = "Author: " + author + "\n" +
						 "Genre: " + genre + "\n" +
						 "Number of books: " + NumberOfBooks + "\n";
						 
		return message;
	}
}


/* BookApplication
*
*
*/

public class BookApplication
{
	static public void main(String[] args)
	{
		Book book = new Book();

		
		book.setLanguage("Java");
		book.setDescription("A book on the basics of the Java programming language");
		book.setGenre("Non-fiction");
		book.setAuthor("Bob Stevens");
		book.setNumberOfBooks(5);
		book.setPrice(20.00);
		int productCount = book.getCount();
		
		
		System.out.println(productCount);
		System.out.println(book.toString());
		
	}
	
}


This post has been edited by redhotfire0: 27 February 2010 - 06:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Calling a method of the super class by using

#2 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Calling a method of the super class by using

Posted 27 February 2010 - 06:48 PM

I'm not amazing at this or anything like some other people on this site, but would making a Product item and calling its toString method work?
Was This Post Helpful? 0
  • +
  • -

#3 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: Calling a method of the super class by using

Posted 27 February 2010 - 06:51 PM

View Postzim1985, on 27 February 2010 - 05:48 PM, said:

I'm not amazing at this or anything like some other people on this site, but would making a Product item and calling its toString method work?

Yes, to my belief it is similar to constructors of classes. You can have a default one that just initializes all the instance variables to, lets say null for strings, and 0 for integers. Then, you can overwrite it with another that takes parameters.

P.S. : You also could just run it and see :P
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Calling a method of the super class by using

Posted 27 February 2010 - 07:09 PM

if Book extends Product, you can say that Book is a subclass of Product, or that Product is the superclass of Book.
if you override the toString method of Product in the Book class, and want to add the code from the Product class, use the super keyword as:
super.toString();

in your case:

public String toString() 
        { 
                String message = super.toString() +
                                                 "\nAuthor: " + author + "\n" + 
                                                 "Genre: " + genre + "\n" + 
                                                 "Number of books: " + NumberOfBooks + "\n"; 
                                                  
                return message; 
        } 




here are 3 basic classes for example, you can run it and see how "super" works
public class Parent(){

public String toString(){
String parent = "Parent's toString";
return parent;
}
}

public class Child extends Parent {

public void toString(){
String child = super.toString + "\nChild's toString";
return child;
}
}

public class TestClass {

public static void main(String[] args){
Parent p = new Parent();
Child c = new Child();

//parent's toString
System.out.println(p);
//child's toString
System.out.println(c);
}
}

Was This Post Helpful? 1
  • +
  • -

#5 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: Calling a method of the super class by using

Posted 27 February 2010 - 07:32 PM

View Postjapanir, on 27 February 2010 - 06:09 PM, said:

if Book extends Product, you can say that Book is a subclass of Product, or that Product is the superclass of Book.
if you override the toString method of Product in the Book class, and want to add the code from the Product class, use the super keyword as:
super.toString();

in your case:

public String toString() 
        { 
                String message = super.toString() +
                                                 "\nAuthor: " + author + "\n" + 
                                                 "Genre: " + genre + "\n" + 
                                                 "Number of books: " + NumberOfBooks + "\n"; 
                                                  
                return message; 
        } 




Should have thought of that after using it in class Book's constructor. Thank you very much. Another question now is when I attempt to use super.toString(),
 System.out.println(super.toString()) 
I get a compiler error saying non-static variable super cannot be referenced for a static context. To my knowledge, no variables or methods are set to be static other than getCount(), so this error is confusing.

This post has been edited by redhotfire0: 27 February 2010 - 07:38 PM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10562
  • View blog
  • Posts: 39,085
  • Joined: 27-December 08

Re: Calling a method of the super class by using

Posted 27 February 2010 - 10:37 PM

Can you show us the entire code? As right now, I don't know where you are inovking this statement, but I'm guessing it is in the main() method. If that's the case, you probably want to print the Object, not super.toString(). Also, super and this both refer to instance elements of a class, not the static elements. So super refers to the instance fields and methods in the super class, and this refers to instance data and methods in the current class.
Was This Post Helpful? 0
  • +
  • -

#7 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: Calling a method of the super class by using

Posted 28 February 2010 - 08:03 AM

View Postmacosxnerd101, on 27 February 2010 - 09:37 PM, said:

Can you show us the entire code? As right now, I don't know where you are invoking this statement, but I'm guessing it is in the main() method. If that's the case, you probably want to print the Object, not super.toString(). Also, super and this both refer to instance elements of a class, not the static elements. So super refers to the instance fields and methods in the super class, and this refers to instance data and methods in the current class.


I understand the terminology you are using, but I don't know how to implement it. I am not trying to access any static elements, I have not declared any. Also, but if super revokes the superclass, and if I am trying to use the superclass's method of toString(), then super.toString() should work; an assumption.

public class BookApplication
{
	static public void main(String[] args)
	{
		Book book = new Book();
		
		
		book.setLanguage("Java");
		book.setDescription("A book on the basics of the Java programming language");
		book.setGenre("Non-fiction");
		book.setAuthor("Bob Smith");
		book.setNumberOfBooks(5);
		book.setPrice(20.00);
		int productCount = book.getCount();
		
		
		System.out.println(productCount);
		System.out.println(super.toString()); // Here 
		System.out.println(book.toString());
		
	}
	
}


Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Calling a method of the super class by using

Posted 28 February 2010 - 08:20 AM

Well...that's just...not right...
main is a static method, so if you call ANY non-static methods directly from main, you will have issues. Additionally, your call to super references Object, as that is what BookApplication extends.

// See static!
static public void main(String[] args)
        {
                Book book = new Book();
                
                
                book.setLanguage("Java");
                book.setDescription("A book on the basics of the Java programming language");
                book.setGenre("Non-fiction");
                book.setAuthor("Bob Smith");
                book.setNumberOfBooks(5);
                book.setPrice(20.00);
                int productCount = book.getCount();
                
                
                System.out.println(productCount);

                // Accessing BookApplication's superclass, Object...,
                // Not Book's superclass. And you need to call it 
                // Through an object, not a static method.
                System.out.println(super.toString()); // Here 

                // You can actually simplify this:
                // System.out.println(book);
                System.out.println(book.toString());
                
        }



By calling Book's toString() method, you are also calling Book's superclasses's toString() method (if you're using macosxnerd101's code).
Was This Post Helpful? 0
  • +
  • -

#9 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: Calling a method of the super class by using

Posted 28 February 2010 - 10:30 AM

Alright then, it was my misunderstanding of how super() was affected by where it was being called. As said by Dogstopper, if I were to call it in BookApplication, Object would be the superclass, but if I were to call it in class Book, it would the Product class. Thanks very much for all the support.
Was This Post Helpful? 0
  • +
  • -

#10 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: Calling a method of the super class by using

Posted 28 February 2010 - 07:11 PM

View Postredhotfire0, on 28 February 2010 - 11:30 AM, said:

Alright then, it was my misunderstanding of how super() was affected by where it was being called. As said by Dogstopper, if I were to call it in BookApplication, Object would be the superclass, but if I were to call it in class Book, it would the Product class. Thanks very much for all the support.

Wrong diagnostic !
The super.toSting() applies to an object
When you do

// Accessing BookApplication's superclass, Object...,
// Not Book's superclass. And you need to call it
// Through an object, not a static method.
System.out.println(super.toString());

you are in the middle of your main() method which is static and your toString() does not apply to any object
Was This Post Helpful? 0
  • +
  • -

#11 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Calling a method of the super class by using

Posted 01 March 2010 - 05:45 PM

I was thinking about this. Could you make a method in the Book class that calls the Product toString() method? Then call that in your main method?

I'm confused on this super.toString() and how you know what's calling what where...
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10562
  • View blog
  • Posts: 39,085
  • Joined: 27-December 08

Re: Calling a method of the super class by using

Posted 01 March 2010 - 05:48 PM

So to illustrate, you have these two classes- Person and Student, and Student extends Person. In each class, there is a toString() method. Since Student extends Person, the Student toString() method overrides the Person toString() method inherited. So if you want to incorporate Person toString() in Student toString(), you have to invoke super.toString() in Student, which invokes the Person toString() method. Like so:
class Person{
   public String toString(){
       return "I\'m a person";
   }
}

class Student extends Person{
 
   //returns I'm a person and a student.
   //b/c invokes Person toString() method as well
   public String toString(){
     return super.toString() + " and a student.";
   }
}


Was This Post Helpful? 1
  • +
  • -

#13 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Calling a method of the super class by using

Posted 01 March 2010 - 05:56 PM

View Postmacosxnerd101, on 01 March 2010 - 03:48 PM, said:

So to illustrate, you have these two classes- Person and Student, and Student extends Person. In each class, there is a toString() method. Since Student extends Person, the Student toString() method overrides the Person toString() method inherited. So if you want to incorporate Person toString() in Student toString(), you have to invoke super.toString() in Student, which invokes the Person toString() method. Like so:
class Person{
   public String toString(){
       return "I\'m a person";
   }
}

class Student extends Person{
 
   //returns I'm a person and a student.
   //b/c invokes Person toString() method as well
   public String toString(){
     return super.toString() + " and a student.";
   }
}


Ah ha. So you did override the Student's toString() to use the Person's toString(). Thank you. I understand now :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1