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

Thread: Is it possible to declare 2 different classes in one variable?

  1. #1
    Junior Member
    Join Date
    May 2021
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Post Is it possible to declare 2 different classes in one variable?

    there are 3 classes in two of which are the same methods (sqlite3 and mysql databases), and in the third check.
    Is it possible to declare them in 1 variable by type:

    Mysql or Sqlite sql;

    And in the check for the selected type of base, declare the same variable:

    if(mysql) sql = new Mysql(); else sql = new Sqlite()

    It possible?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,107
    Thanks
    53
    Thanked 2,618 Times in 2,568 Posts

    Default Re: Is it possible to declare 2 different classes in one variable?

    Are you talking about when one class extends another?
    For example if ClassB extends ClassA
    then a variable declared to hold a reference to a ClassA object can contain a reference to a ClassB object.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Mikyc (May 5th, 2021)

  4. #3
    Junior Member
    Join Date
    May 2021
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Is it possible to declare 2 different classes in one variable?

    I mean that I have a settings file from where I get the data, and if the user puts, for example, mysql there, then it will work with the Mysql class, and if sqlite, then Sqliite. the question is how to declare a common variable to a class

    Example:

    Mysql sql;
    Sqlite sql; #How can i declare these classes in 1 variable (sql)?

    if(mysql) sql = new Mysql(); else sql = new Sqlite() #And here they are given importance depending on the configuration

    sql.UpdateData(name); #Then the data is sent to the class with the processing of the database (specified in the configuration)

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,107
    Thanks
    53
    Thanked 2,618 Times in 2,568 Posts

    Default Re: Is it possible to declare 2 different classes in one variable?

    Each variable is declared as one data type. For classes, since all classes extend the Object class, then a variable declared as an Object can be assigned a reference to any class. Primitive variables can not do that.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Mikyc (May 5th, 2021)

  7. #5
    Junior Member
    Join Date
    May 2021
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Is it possible to declare 2 different classes in one variable?

    ok i have declared a variable of type Object but

    Object sql;
    if (mysql) sql = new Mysql(); else sql = new Sqlite();
    sql.Update(name);

    ERROR: Cannot resolve method 'Update' in 'Object'

    how i can fix it? (Sorry)

    or did I do something wrong?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,107
    Thanks
    53
    Thanked 2,618 Times in 2,568 Posts

    Default Re: Is it possible to declare 2 different classes in one variable?

    The compiler does not see any Update method in the Object object.
    You can use casting to tell the compiler what class is contained in the Object object.
    ((SomeClass)theObject).theMethod(); // call theMethod method in the SomeClass object referenced by theObject.

    Do the two classes you are working with implement an interface? Then use the interface as the data type.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Mikyc (May 5th, 2021)

Similar Threads

  1. Can't declare Variable in new method
    By JohnEliot in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 20th, 2013, 06:14 AM
  2. Suggestion to declare variable that is declared
    By zlloyd1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 28th, 2013, 06:32 PM
  3. Help with making classes as a variable type!
    By Kranti1992 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 22nd, 2012, 01:05 AM
  4. Netbeans and inner classes/variable access
    By jmorr212 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 17th, 2010, 10:22 PM
  5. How to share variable values amongst different classes?
    By igniteflow in forum Object Oriented Programming
    Replies: 8
    Last Post: August 20th, 2009, 08:53 AM