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

  • SEARCH
  • Home
  • 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 6783615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:53:13+00:00 2026-05-26T16:53:13+00:00

I am new to Java coming from a PHP background so sorry if this

  • 0

I am new to Java coming from a PHP background so sorry if this maybe obvious. I’m trying to implement a binary tree class and I’ve created an ADT like so:

public abstract class BTree<T> {
    private T value;
    private BTree<T> leftChild;
    private BTree<T> rightChild;
    private BTree<T> parent;
    public BTree<T> getLeftChild() { return this.leftChild; }
    ....
} 

Then I have another class that extends this like so:

public class BIntTree extends BTree<Integer> {
}

However I want to be able to within BIntTree to have a method where I can call this.getLeftChild(); and get an instance of BIntTree back rather than a instance of BTree<Integer>

Is this possible with some way of defining the generic class / method or do I have to explicitly type cast it after I used this.getLeftChild() or even override the superclass method?

My current solution is to explicitly typecast it in the BIntTree method with BIntTree b=(BIntTree) this.getLeftChild(); which seems untidy to me.

Also I’m not so sure what would happen if I had that type casting defined and getLeftChild() returned null, would an exception be thrown? If so how do I cure this given that null is also a valid value if exist?

  • 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-26T16:53:14+00:00Added an answer on May 26, 2026 at 4:53 pm

    It is possible to have a self-typed class, but you have to wonder why you want it. Why do you need to know that it is a BIntTree rather than a BTree<Integer>? What have you gained with your use of generics?

    Anyway, you could do something like this:

    public abstract class BTree<V, T extends BTree<V, T>> {
        public T getLeft() {...}
        public T getRight() {...}
        public V getValue() {...}
    }
    

    Then your BIntTree would be

    public class BIntTree extends BTree<Integer, BIntTree> {
        //...
    }
    

    Edit

    With regards to your question about casting null, there is really nothing preventing you from testing the behaviour yourself. But to answer your question, it is safe to cast null to any type.

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

Sidebar

Related Questions

I am new to java (coming from c#.net background) and was trying the above
Note: I'm relatively new to Objective-C and am coming from Java and PHP. Could
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering
I'm having a heap of trouble with Java coming from a PHP background. I've
Coming from Java background I am guessing this is expected. I would really love
First off my background: I'm new to Java coming over from Ruby. If that
I am new to C++ coding, coming from Java and C# background. I'm puzzled
I'm quite new to C# (coming from a Java background) and I'm working on
I am learning Scala (coming from a background of mostly Java). I am trying
Coming from a Java background it is new for me to deal with the

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.