Posts

Showing posts with the label inheritance

Java: How to create a public class that can be subclassed within package, while preventing outsiders from inheriting?

Java: How to create a public class that can be subclassed within package, while preventing outsiders from inheriting? Suppose I am writing a package. I have the following inheritance hierarchy, all within my package: public class Container { public void size() { System.out.println(10); } } public final class Bucket extends Container {} public final class Bag extends Container {} public class Item { Container container; // is aware of what container it's in. Will be initialized on construction. public Container getContainer() { return container; } } I want to prevent others from subclassing Container . But I can't make it final because Bucket and Bag are subclasses. And I have to make it public or else outsiders cannot call getContainer() . Is there a way to do this in Java? Otherwise, what do people do in this type of situation? Container final Bucket Bag public getContainer() So to clarify what you may be aski...

Unable to access base class method in derived class

Unable to access base class method in derived class I have a base class as follows : class template<typename aGamePieceBase> class GamePiece : public aGamePieceBase { public: // Each GamePiece contains a definition of the base class from which it was derived typedef aGamePieceBase gamePieceBase // Returns this piece's sprite virtual sf::Sprite* getSprite() { return m_pSprite; } protected: sf::Sprite* m_pSprite; } And a derived class : class class Horse : public GameBoard::GamePiece< GameBoard::GamePieceBase > { public: virtual sf::Sprite* getSprite() override { // Do stuff } }; However I am presented with this error on the deceleration of Horse::getSprite Member function declared with override does not override a base class member Horse::getSprite Why is getSprite unavailable in Horse? getSprite GamePieceBase is as follows : GamePieceBase class GamePieceBase : public GameEventHandler { public: // Called wh...

How does Java handle potentially ambiguous method calls?

How does Java handle potentially ambiguous method calls? Playing around with ambiguous methods calls that have multiple parameters, I noticed that quite often it was not actually ambiguous when I expected it to be, and this led to some strange behavior that I couldn't quite understand. For example, with the following inheritance structure: public static class A { } public static class B extends A { } public static class C extends B { } And running the method test() . test() public static void test() { test(new C(), new C(), new C()); } For some reason these two methods are ambiguous public static void test(A x, A xx, B xxx) { System.out.println("TEST 1"); } public static void test(A x, C xx, A xxx) { System.out.println("TEST 2"); } However swapping the last two arguments in the second method makes that one take priority. public static void test(A x, A xx, B xxx) { System.out.println("TEST 1"); } public static void test(A x, A xx, C xxx)...