superclass

extending superclass and ClassCastException

左心房为你撑大大i 提交于 2019-12-22 08:37:11
问题 I have a superclass, which two methods i want to override. Here's my code: public class MyCustomClass extends SomeSuperClass { protected MyCustomClass(params) { super(params); } @Override public void method1() { super.method1(); /* here goes my code */ } @Override public void method2() { super.method2(); /* here goes my another code */ } I have some constructor, that passes SomeSuperClass object as a parameter, and what i do next: MyCustomClass object; /* now i have object of type

create an object before the super call in java

£可爱£侵袭症+ 提交于 2019-12-22 05:29:20
问题 Considering that simple java code which would not work: public class Bar extends AbstractBar{ private final Foo foo = new Foo(bar); public Bar(){ super(foo); } } I need to create an object before the super() call because I need to push it in the mother class. I don't want to use an initialization block and I don't want to do something like: super(new Foo(bar)) in my constructor.. How can I send data to a mother class before the super call ? 回答1: If Foo has to be stored in a field, you can do

Strange error regarding instance variables & superclass

ぃ、小莉子 提交于 2019-12-22 04:54:46
问题 I've got some code where my classes inherit from a superclass, and everything has been working fine till now. I'm getting an error whenever I try to use any of the superclass variables, saying that they are undeclared (first use in this function). It's only happening in one of my subclasses, & it looks exactly the same as the others. I'm wondering if there's anything obvious which I should know about (being quite new to Objective-C). The basic code is like - @interface mySuperClass :

How to call both super(…) and this(…) in case of overloaded constructors?

*爱你&永不变心* 提交于 2019-12-22 03:15:15
问题 I've never needed to do this before but since both have to be the 'first' line in the constructor how should one tackle it? What's the best refactoring for a situation like this? Here's a sample: public class Agreement extends Postable { public Agreement(User user, Data dataCovered) { super(user); this(user,dataCovered,null); } public Agreement(User user,Data dataCovered, Price price) { super(user); if(price!=null) this.price = price; this.dataCovered = dataCovered; } ... } The call to super

Can I mock a superclass's constructor with Mockito/Powermock?

不羁的心 提交于 2019-12-21 12:13:33
问题 Is it possible using Mockito and optionally Powermock to mock a superclass S such that any calls to the superclass to S (including calls to the S() constructor) are mocked? So using the below example, if I replace S with MockS using Mockito, will the call to super() use the constructor in MockS ? class S { S() { // Format user's hard drive, call 911, and initiate self-destruct } } class T extends S { T() { super(); } } class Test { @Mock private S mockS; new T(); // T's call to super() should

General 'map' function for Scala tuples?

馋奶兔 提交于 2019-12-21 10:12:16
问题 I would like to map the elements of a Scala tuple (or triple, ...) using a single function returning type R. The result should be a tuple (or triple, ...) with elements of type R. OK, if the elements of the tuple are from the same type, the mapping is not a problem: scala> implicit def t2mapper[A](t: (A,A)) = new { def map[R](f: A => R) = (f(t._1),f(t._2)) } t2mapper: [A](t: (A, A))java.lang.Object{def map[R](f: (A) => R): (R, R)} scala> (1,2) map (_ + 1) res0: (Int, Int) = (2,3) But is it

Maximum recursion depth error in Python when calling super's init. [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-21 08:08:22
问题 This question already has an answer here : How to avoid infinite recursion with super()? (1 answer) Closed 11 months ago . I have a class hierarchy A <- B <- C, in B, I need some processing in the constructor, so I came up with this code from this post: Understanding Python super() with __init__() methods #!/usr/bin/python class A(object): def __init__(self, v, v2): self.v = v self.v2 = v2 class B(A): def __init__(self, v, v2): # Do some processing super(self.__class__, self).__init__(v, v2)

scope of private constructor in Nested Class

廉价感情. 提交于 2019-12-20 11:10:27
问题 This is more of a puzzle than question. I have the following code: public class PrivateBaseConstructor { public static class BaseClass { private BaseClass() { } } public static class DerivedClass extends BaseClass { public DerivedClass() { super(); // 1* } } } Here the call for super(); at 1* is allowed event though the base class constructor is private . If we write the classes as separate classes in same package: BClass.java public class BClass { private BClass() { } } DClass.java public

Why Object class is Superclass in java [closed]

大城市里の小女人 提交于 2019-12-20 08:04:31
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . Why object Class is a super class in java yesterday i had one interview and the interviewer asked me the questions. 回答1: Because the Object class, in the java.lang package, sits at the top of the class hierarchy tree. 回答2: Because it is just a definition. Actually sec 4.3.2

What do you call the superclass and subclass when initializing? [duplicate]

若如初见. 提交于 2019-12-20 07:57:09
问题 This question already has answers here : What does it mean to “program to an interface”? (31 answers) Closed 3 years ago . List<String> list = new ArrayList<String>(); What is List and ArrayList when identifying the parts besides super class and subclass? Is List the reference and ArrayList the class? Would they be called something else if they were the same like: ArrayList<String> list = new ArrayList<String>(); 回答1: List is an interface. ArrayList is an implementation of it. Neither is a