Skip to content
Advertisement

Tag: inheritance

Java inheritance using super keyword

I am using inheritance along with super function in my program ,but when I am extending my class it showing error message “There is no default constructor in ‘cc’. ” . This error message is coming after 1st subclass is extended and trying to make 2nd subclass. Here is the code Answer dd inherits cc, so it’ll have to call

Extending the Graphics class in Java

I need to extend the Graphics class in order to @Override some methods including drawRect(int,int,int,int) and drawRoundRect(int,int,int,int,int,int). However, I have no idea how to do that. This is what I’ve got so far: I get an error on class declaration line saying: myGraphics is not abstract and does not override abstract method dispose() in java.awt.Graphics I also get an error

Are static variables inherited

I have read at 1000’s of locations that Static variables are not inherited. But then how this code works fine? Parent.java Child.java This code prints “Parent”. Also read at few locations concept of data hiding. Parent.java Child.java Now the output is “Child”. So does this mean that static variables are inherited but they follow the concept of data-hiding? Answer Please

Java: how to have an array of subclass types?

Say I have a super class “Animal” and subclasses “Cat”, Dog, Bird”. Is there a way to have an array of subclass type rather than class instances with which I’ll be able to instantiate instances of each possible subclass? To simplify, I want this: How can I do that? Edit: I don’t want an array of instances of these subclasses,

Most efficient way to cast List to List

I have a List<SubClass> that I want to treat as a List<BaseClass>. It seems like it shouldn’t be a problem since casting a SubClass to a BaseClass is a snap, but my compiler complains that the cast is impossible. So, what’s the best way to get a reference to the same objects as a List<BaseClass>? Right now I’m just making

Calling super super class method

Let’s say I have three classes A, B and C. B extends A C extends B All have a public void foo() method defined. Now from C’s foo() method I want to invoke A’s foo() method (NOT its parent B’s method but the super super class A’s method). I tried super.super.foo();, but it’s invalid syntax. How can I achieve this?

Advertisement