superclass

constructor of subclass in Java

泪湿孤枕 提交于 2019-11-26 16:48:28
问题 When compiling this program, I get error- class Person { Person(int a) { } } class Employee extends Person { Employee(int b) { } } public class A1{ public static void main(String[] args){ } } Error- Cannot find Constructor Person(). Why defining Person() is necessary? 回答1: When creating an Employee you're creating a Person at the same time. To make sure the Person is properly constructed, the compiler adds an implicit call to super() in the Employee constructor: class Employee extends Person

Call subclass's method from its superclass

落花浮王杯 提交于 2019-11-26 16:45:15
问题 I have two classes, named Parent and Child , as below. Parent is the superclass of Child I can call a method of the superclass from its subclass by using the keyword super . Is it possible to call a method of subclass from its superclass? Child.h #import <Foundation/Foundation.h> #import "Parent.h" @interface Child : Parent { } - (void) methodOfChild; @end Child.m #import "Child.h" @implementation Child - (void) methodOfChild { NSLog(@"I'm child"); } @end Parent.h: #import <Foundation

Use of Java [Interfaces / Abstract classes] [duplicate]

谁说我不能喝 提交于 2019-11-26 16:22:35
This question already has an answer here: Interface vs Abstract Class (general OO) 34 answers Lately i decided to take a look at Java so i am still pretty new to it and also to the approach of OO programming, so i wanted to get some things straight before learning more, (i guess it's never to soon to start with good practices). I am programming a little 2D game for now but i think my question applies to any non trivial project. For the simplicity I'll provide examples from my game. I have different kinds of zombies, but they all have the same attributes (x, y, health, attack etc) so i wrote an

Why call super() in a constructor?

﹥>﹥吖頭↗ 提交于 2019-11-26 15:18:20
I'm dealing with a class which extends JFrame . It's not my code and it makes a call to super before it begins constructing the GUI. I'm wondering why this is done since I've always just accessed the methods of the superclass without having to call super(); There is an implicit call to super() with no arguments for all classes that have a parent - which is every user defined class in Java - so calling it explicitly is usually not required. However, you may use the call to super() with arguments if the parent's constructor takes parameters, and you wish to specify them. Moreover, if the parent

Why aren&#39;t superclass __init__ methods automatically invoked?

断了今生、忘了曾经 提交于 2019-11-26 14:05:17
Why did the Python designers decide that subclasses' __init__() methods don't automatically call the __init__() methods of their superclasses, as in some other languages? Is the Pythonic and recommended idiom really like the following? class Superclass(object): def __init__(self): print 'Do something' class Subclass(Superclass): def __init__(self): super(Subclass, self).__init__() print 'Do something else' The crucial distinction between Python's __init__ and those other languages constructors is that __init__ is not a constructor: it's an initializer (the actual constructor (if any, but, see

Java. Implicit super constructor Employee() is undefined. Must explicitly invoke another constructor [duplicate]

拟墨画扇 提交于 2019-11-26 13:57:21
问题 This question already has an answer here: Java error: Implicit super constructor is undefined for default constructor 10 answers Hello I'm new to Java, I'm getting this error in my production worker class. My Production worker constructor says explicitly invoke another constructor. I don't know what to do?. import java.util.Date; public class Employee { private String name, number; private Date date; public Employee(String name, String number, Date date) { setName(name); setNumber(number);

method must call super() error in Netbeans

倖福魔咒の 提交于 2019-11-26 12:48:32
问题 Recently I\'ve made a Netbeans project and I am using SVN along with it. I am seeing duplicate class error, and in the console it says java.lang.VerifyError: (class: pie/chart/explorer/PieChartExplorer, method: <init> signature: ()V) Constructor must call super() or this() Could not find the main class: pie.chart.explorer.PieChartExplorer. Program will exit. Exception in thread \"main\" Java Result: 1 Here is PieChartExplorer.java: package pie.chart.explorer; import java.awt.*; import java

Why is constructor of super class invoked when we declare the object of sub class? (Java)

独自空忆成欢 提交于 2019-11-26 12:29:22
问题 Consider this code: class Test { Test() { System.out.println(\"In constructor of Superclass\"); } int adds(int n1, int n2) { return(n1+n2); } void print(int sum) { System.out.println(\"the sums are \" + sum); } } class Test1 extends Test { Test1(int n1, int n2) { System.out.println(\"In constructor of Subclass\"); int sum = this.adds(n1,n2); this.print(sum); } public static void main(String[] args) { Test1 a=new Test1(13,12); Test c=new Test1(15,14); } } If we have a constructor in super

super() raises “TypeError: must be type, not classobj” for new-style class

Deadly 提交于 2019-11-26 11:59:33
The following use of super() raises a TypeError: why? >>> from HTMLParser import HTMLParser >>> class TextParser(HTMLParser): ... def __init__(self): ... super(TextParser, self).__init__() ... self.all_data = [] ... >>> TextParser() (...) TypeError: must be type, not classobj There is a similar question on StackOverflow: Python super() raises TypeError , where the error is explained by the fact that the user class is not a new-style class. However, the class above is a new-style class, as it inherits from object : >>> isinstance(HTMLParser(), object) True What am I missing? How can I use super

why java polymorphism not work in my example

风流意气都作罢 提交于 2019-11-26 11:24:53
I have these 4 java clases: 1 public class Rect { double width; double height; String color; public Rect( ) { width=0; height=0; color="transparent"; } public Rect( double w,double h) { width=w; height=h; color="transparent"; } double area() { return width*height; } } 2 public class PRect extends Rect{ double depth; public PRect(double w, double h ,double d) { width=w; height=h; depth=d; } double area() { return width*height*depth; } } 3 public class CRect extends Rect{ String color; public CRect(double w, double h ,String c) { width=w; height=h; color=c; } double area() { return width*height;