[Solved] Inheritence – Mock Exam

Which of the assignment statements in someMethod are valid? All of them. x, this.x and super.x all point to protected int x in class A which is visible to the subclass B. this.getX() and super.getX() both call public int getX() in class A which is visible to the subclass B. answer, x and the return … Read more

[Solved] Java: How to access implementation of a method of Sub class from Super class

Consider following: public interface Animal { bool isVertebrate(); bool isDomesticable(); } and an abstract class that implements this interface: abstract class Cat implements Animal { public bool isVertebrate() {return true;} public void dealWithCat(){ if (isDomesticable()){ … } } But implementation for isDomesticable is delegated to subclass: public class Tiger extends Cat { public bool isDomesticable(){ … Read more

[Solved] What is more Efficient? Implementation in overloaded function or by checking object type in Base class function

For many reasons – code maintainability, extensibility, concision, reliability, minimising the amount of code that needs to be recompiled/redistributed to pick up changes in some library code it uses – you should almost always use virtual functions rather that writing your own switching mechanisms. If you need to ask about it on stackoverflow, I’d go … Read more

[Solved] How do I override a parent class’s method? [closed]

If your method has the same name that the parent’s, parameters and return type, you’re overriding it. Also you can add @Override annotation on the top of your method. Always check parent’s method is not final nor private. For instance public class Parent{ public void method(String param){ //Do stuff } private void notExtendable(String param){ } … Read more

[Solved] Java – interface -instsnceof

JLS 15.20.2 states: If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error (ยง15.16), then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true. In this case (Intf) obj is not a compile time error … Read more

[Solved] Cannot access an Object Reference instantiated by a sibling class

So, blah defines an instance field called label but does not initialise it public abstract class Blah { protected JLabel label; } BlahChildOne initialises the label from Blah public class BlahChildOne extends Blah { public BlahChildOne() { label = new JLabel(); } } BlahChildTwo does not initialises the label from Blah, so it is still … Read more

[Solved] Pointer will randomly print it’s address instead of it’s pointed value, C++

void setvalores(int vi, int fu, int ve) { velocidad = &ve; vida = &vi; fuerza = &fu; }; The pointers to vi, fu, and ve get invalidated when the function returns. You’re not seeing addresses being printed, but simply garbage. Your entire design doesn’t and shouldn’t need to use pointers though. 8 solved Pointer will … Read more

[Solved] Polymorphism vs Inheritance. Diffrence?

Here’s a version of your first example, that actually uses polymorphism: #include <iostream> #include <string> class shape { public: void setValues(int height_, int width_) { height = height_; width = width_; } virtual int area() = 0; // This is needed for polymorphism to work virtual std::string name() = 0; protected: int height; int width; … Read more