Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4007468
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:38:09+00:00 2026-05-20T08:38:09+00:00

I have two classes public class PrepaidPackage { private String name; private String serviceClassID;

  • 0

I have two classes

public class PrepaidPackage {

    private String name;
    private String serviceClassID;
    private boolean isTranferable;

    public boolean isTranferable() {
        return isTranferable;
    }
    public void setTranferable(boolean isTranferable) {
        this.isTranferable = isTranferable;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getServiceClassID() {
        return serviceClassID;
    }
    public void setServiceClassID(String serviceClassID) {
        this.serviceClassID = serviceClassID;
    }
}

other class is

public class PostpaidPackage {
    private String name;
    private boolean isTranferable;
    public boolean isTranferable() {
        return isTranferable;
    }
    public void setTranferable(boolean isTranferable) {
        this.isTranferable = isTranferable;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

I want to create a factory class which on base of package type create relevant class. But if you look at above clasess they dont have same type of methods and variables. So please guide how create interface or abstract class for above class?

Now factory will return class name Package. Would i able to call methods which are not present in other class.

Updates

Please suggest if i break my package into two classes like

public abstract class MyPackage {
    public abstract PackageSpec getSpec();
    public abstract PackagePrepaidDetails getDetail();
}

Now common attributes will be in PackageSpec and prepaid stuff in packageDetails.

Its kind of abstract factory pattern.

public class PrepaidPackage extends MyPackage{
    PackageSpec spec;
    public Spec getSpec() {
      spec = new PackageSpec();
      spec.setTranferable(true)
      spec.setName("abc");
      return spec;
    }
    public PackagePrepaidDetails getDetails() {
      details = new PackagePrepaidDetails ();
      details.setServiceClassID(123)
      return details;
    }
}

public class PostpaidPackage extends MyPackage{
    PackageSpec spec;
    public Spec getSpec() {
        spec = new PackageSpec();
        spec.setTranferable(true)
        spec.setName("abc");
        return spec;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:38:10+00:00Added an answer on May 20, 2026 at 8:38 am

    I recomment you to have an interface if you don’t have already. You do not neccessarily need it, but it is a good practice if they are so similar:

    public interface Package {
        public boolean isTranferable();
        public void setTranferable(boolean isTranferable);
        public String getName();
        public void setName(String name);
    }
    

    Then in your calling code, you have a Package from your factory and:

    Package p = myFactory.nextPackage();    // or something
    if (p instanceof PrepaidPackage) {
        PrepaidPackage prepaid = (PrefpaidPackage)p;
        // and do the thing you want
    } else if (p instanceof PostpaidPackage) {
        PostpaidPackage postpaid = (PostpaidPackage)p;
        // amd do the other things
    }
    

    Thing you are recommended to llok into is the instanceof operator and type casting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes: Public Class Subscribing Private _subscribingObjects As IList(Of String) Public Sub
I have two classes: public class MyBase { public virtual void DoMe() { }
I have two classes: public class RichMan { public string ID {get;set;} List<Car> _cars=new
I have two classes: public class DocumentViewModel { public virtual string DocumentNumber { get;
Let's say I have the following two classes: public class Person { public string
I have two classes: public class Article { private ISet<IdentNumber> identNumbers = new HashedSet<IdentNumber>();
I have two classes: public class Person { public string FirstName { get {
Let's say I have the following two classes: public class TestResults { public string
I have two classes: Role and CustomRole public class CustomRole { public string RoleName
I have two classes: namespace Something { [Serializable] public class Spec { public string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.