Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Bean --- AVOID duplicate SETTER / GETTER  RSS feed

 
Vince Hon
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a web application (jsp, servlet) that allow user
to insert, modify, delete a record in database with table
PRODUCT.
The table PRODUCT will present different type of products, video, audio and
book
[PRODUCT]
----------
id
name
----------
I will write VideoProduct.java, AudioProduct.java, BookProduct.java
to handle the insert, modify, delete business logic......
class VideoProduct(){
private String id;
private String name;
private String type;
// other attributes that only Video Product will have
/** getter and setter methods ... ***/
}
class AudioProduct(){
private String id;
private String name;
private String type;
// other attributes that only Audio Product will have
/** getter and setter methods ... ***/
}
class BookProduct(){
private String id;
private String name;
private String type;
// other attributes that only book will have
/** getter and setter methods ... ***/
}
In the above case, I have to write the "SAME" getter and setter methods in all *.java.
Since those product will share many common attributes (id and name), my question is that:
1. How can I using inheritance to avoid writting the duplicate getter and setter methods ?
2. Since the attribute should be "private" in a bean, can I still use inheritance ?
Thanks
Vince
 
Rashmi Banthia
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this what you are looking for : - (Yes, you can use inheritance even if you want the attributes to be private)
package A;
class GenericBase
{
protected String id ;
protected String name ;
protected String type ;
//public common getters and setters
}
package B;
import A.GenericBase;
class VideoProduct() extends GenericBase
{
//protected members will be private in this class
}

Regards,
Rashmi
 
Vince Hon
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Rashmi Jain

The one you provide can solve my problem, however, I would like to know is it a "good practise" to do this by using protected member in super class ?
Is there any "risk" ?
 
Nguyen Phuc Hai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the attribute of class VideoProduct can not be use by external class, you can use keyword final. This keyword does not allow other class inherent the VideoProduct. But I propose the other methods that help you do need modify the code if customer require more attributes in future.
You create the CustomProduct
package phuchai.study;
import java.util.Properties;
public class CustomProduct{
public static int VIDEO = 1;
public static int RADIO = 2;
public static int FOOD = 3;
....
private String id; //common property of all kinds of product
private String name;
private int productType = -1; //You add attribute to know the type of product (RADIO, VIDEO, ...)
private Properties properties (You can use Map for general purpose)
public CustomProduct(int _productType){
this.productType = _productType;
}
//getter and setter
...
//
public int getProductType(){
return this.productType;
}
//Note: you should not implement setProductType method. And Product type should be assigned only constructor method
}
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36406
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maulin,
It's generally better to have private fields than protected ones. Since you have the public getters and setters, your subclasses can call those instead of accessing the fields directly. That way, the superclass is free to change the data representation without affecting all the subclasses.
If you don't have public getters/setters, you can make those methods protected. Then, the fields are private and the subclasses still have access.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!