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 7167035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:30:43+00:00 2026-05-28T14:30:43+00:00

I made a class Tree (abstraction for mathematical expression). It have nested class ‘Vertex’

  • 0

I made a class Tree (abstraction for mathematical expression). It have nested class ‘Vertex’ and field ‘Vertex head’. Another class ‘BinaryTree’ extends Tree, but it have more possibilities since it’s Binary and they have different Vertex classes (I added to Vertex methods giveRight and giveLeft), that’s why I use inheritance of nested classes. But I have field from Tree head, and it doesn’t have methods giveRight and so on… Here is an example:

class Tree{
    class Vertex{
        //smth
    }
    Vertex head;
}

class BinaryTree extends Tree{
    class Vertex extends Tree.Vertex{
        //added methods...
    }
    //problem with head element, it is element of Tree.Vertex
}

Am I right with object-oriented part of this problem? Or should I delete head field from Tree and add it only to it’s subclasses.

Thank you.

  • 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-28T14:30:44+00:00Added an answer on May 28, 2026 at 2:30 pm

    The main problem is not the declared type of the head field, but its runtime type. If the subclass is the only one to create its own vertices, then it can assign a BinaryTree.Vertex to the head variable. You’ll have to cast if to BinaryTree.Vertex if you want to use its additional methods, though.

    To avoid the cast, you may make the Tree class generic:

    public class Tree<V extends Vertex> {
        protected V head;
    }
    
    public class BinaryTree extends Tree<BinaryVertex> {
    
    }
    

    See javadoc for more information about generics.

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

Sidebar

Related Questions

I have a class made up of several fields, and I have several constructors.
I have made a class which a form can inherit from and it handles
I have made an class which conforms to the NSCoding protocol and does all
I have to use a class/assembly made in C# .NET from native C++ application.
I had to populate a tree view for which i have made a custom
I have a Binary-Tree class template with several public and private member functions. When
I make use of a great class made by Jaisen: http://github.com/jmathai/twitter-async/tree/master . Recently twitter
I have python class trees, each made up of an abstract base class and
I have a large Quad Tree structure, made from generic lists ( List<T> )
I made a class from Linq to SQL Clasees with VS 2008 SP1 Framework

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.