superclass

why does initializing subclasses require calling the super class's same init function?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 23:22:52
I have heard that when you have a subclass, you are supposed to initialize the superclass with the same init function from within the subclass's init. What I mean is that the subclass's init should call [super init] and the subclass's initWithFrame should call [super initWithFrame]. Why is this? Why does calling the super's init from a subclass's initWithFrame result in an infinite loop? If this is required, then does this mean I can't create a new init function within a subclass such as initWithPoint and have that call super's init or initWithFrame simply because the super class doesn't have

Private members in Java inheritance

旧街凉风 提交于 2019-11-27 20:52:51
I was told that for a Java subclass it can inherit all members of its superclass. So does this mean even private members? I know it can inherit protected members. Can someone explain this to me. I am now totally confused. sgokhales No, the private member are not inherited because the scope of a private member is only limited to the class in which it is defined. Only the public and protected member are inherited. From the Java Documentation , Private Members in a Superclass A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected

Getting the name of a sub-class from within a super-class

僤鯓⒐⒋嵵緔 提交于 2019-11-27 20:49:56
问题 Let's say I have a base class named Entity . In that class, I have a static method to retrieve the class name: class Entity { public static String getClass() { return Entity.class.getClass(); } } Now I have another class extend that. class User extends Entity { } I want to get the class name of User: System.out.println(User.getClass()); My goal is to see "com.packagename.User" output to the console, but instead I'm going to end up with "com.packagename.Entity" since the Entity class is being

Difference between super() and calling superclass directly

泪湿孤枕 提交于 2019-11-27 19:42:38
In Python 2.7 and 3, I use the following method to call a super-class's function: class C(B): def __init__(self): B.__init__(self) I see it's also possible to replace B.__init__(self) with super(B, self).__init__() and in python3 super().__init__() . Are there any advantages or disadvantages to doing this either way? It makes more sense to call it from B directly for me at least, but maybe there's a good reason where super() can only be used when using metaclasses (which I generally avoid). poke For single inheritance, super() is just a fancier way to refer to the base type. That way, you make

Call subclass's method from its superclass

*爱你&永不变心* 提交于 2019-11-27 14:46:48
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/Foundation.h> @interface Parent : NSObject { } - (void) methodOfParent; @end Parent.m: #import "Parent.h"

constructor of subclass in Java

百般思念 提交于 2019-11-27 14:32:39
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? 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 { Employee(int id) { super(); // implicitly added by the compiler. } } Since Person does not have a no

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

痞子三分冷 提交于 2019-11-27 13:09:31
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); setDate(date); } public void setName(String n) { name = n; } public void setNumber(String n) { number = n

Why is super class constructor always called [duplicate]

走远了吗. 提交于 2019-11-27 09:12:36
This question already has an answer here: Why do this() and super() have to be the first statement in a constructor? 19 answers I have the following 2 classes public class classA { classA() { System.out.println("A"); } } class classB extends classA { classB() { System.out.println("B"); } } and then running 1 classA c = new classB(); or 2 classB c = new classB(); always gives A B Why is this happening? At first glance, in either scenario, I would assume that only the classB constructor would be called and thus the only output would be B but this is clearly wrong. Aniket Thakur That is how Java

super keyword without extends to the super class

故事扮演 提交于 2019-11-27 07:00:59
问题 There is a code of simple program. In constructor, super() is called without extends to the super class, I can not understand what will does this in this situation? public class Student { private String name; private int rollNum; Student(String name,int rollNum){ super(); //I can not understand why super keyword here. this.name=name; this.rollNum=rollNum; } public static void main(String[] args) { Student s1 = new Student("A",1); Student s2 = new Student("A",1); System.out.println(s1.equals

method must call super() error in Netbeans

孤人 提交于 2019-11-27 05:34:44
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.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class