inheritance

Does a subclass constructor require all arguments of superclass constructor?

空扰寡人 提交于 2019-12-28 19:37:05
问题 I have two classes, Staff and AdvancedStaff , which extends the former. Staff has this constructor: public Staff (String number, String title, String name, String role, char level) { staffNumber = number; staffTitle = title; staffName = name; staffRole = role; payScaleLevel = level; } I'll note that all instance variables have been set to private. While, Advanced Staff has this constructor: public AdvancedStaff (String number, String title, String name) { super(number, title, name); role =

Why can I call base template class method from derived class

痴心易碎 提交于 2019-12-28 17:56:41
问题 I decided to test one of the examples in "Effective C++" and I'm not getting the result I expected. So, apparently this (simplified) code shouldn't compile: template <class T> struct A { void f(){} }; template <class T> struct B : public A <T> { void f2() { f(); } // calling base function - will not compile }; Here's the explanation (class names changed for simplicity) : The code above won't compile, at least not with conformant compilers. Such compilers will complain that f doesn't exist. We

why can't we assign weaker privilege in subclass

半城伤御伤魂 提交于 2019-12-28 13:24:32
问题 I have a class which has a method whose access specifier by default is public. Now, I would like to extend this class in a subclass and I want to override this method to have access specifier "private". When compiling this code, I am getting a compilation error: "attempting to assign weaker access privileges". Could somebody please explain to me what is wrong with assigning weaker privileges in a subclass? Here is the code that caused the compilation error: class Superclass { void foo() {

find size of derived class object using base class pointer

爱⌒轻易说出口 提交于 2019-12-28 13:20:10
问题 Is it possible to find the size of a derived class object using a base class pointer, when you don't know the derived type. Thank you. 回答1: There's no direct way, but you can write a virtual size() method child classes can implement. An intermediary templates class can automate the leg work. struct base { virtual size_t size() const =0; virtual ~base() { } }; template<typename T> struct intermediate : base { virtual size_t size() const { return sizeof(T); } }; struct derived : intermediate

javascript class inherit from Function class

半腔热情 提交于 2019-12-28 12:05:12
问题 I like that in javascript, I can create a function, and then add further methods and attributes to that function myInstance = function() {return 5} myInstance.attr = 10 I would like to create a class to generate these objects. I assume I have to inherit from the Function base class. In other words, I would like to: var myInstance = new myFunctionClass() var x = myInstance() // x == 5 But I don't know how to create the myFunctionClass. I have tried the following, but it does not work: var

Inheritance with JAX-RS

别来无恙 提交于 2019-12-28 12:00:14
问题 I am using JAX-RS for my web services. I have common functionality and would like to use inheritance. I am providing simple CRUD operations. I have defined an interface like so: public interface ICRUD { @POST @Consumes("application/json") @Produces("application/json") @Path("create") public String createREST(String transferObject); @GET @Consumes("application/json") @Produces("application/json") @Path("retrieve/{id}") public String retrieveREST(@PathParam("id") String id); @POST @Consumes(

Accessing private instance variables of parent from child class?

时间秒杀一切 提交于 2019-12-28 11:52:07
问题 Let's say we have a class foo which has a private instance variable bar . Now let us have another class, baz , which extends foo . Can non-static methods in baz access foo 's variable bar if there is no accessor method defined in foo ? I'm working in Java, by the way. 回答1: No, not according to the java language specification, 3rd edition: 6.6.8 Example: private Fields, Methods, and Constructors A private class member or constructor is accessible only within the body of the top level class (§7

WebAPI controller inheritance and attribute routing

浪子不回头ぞ 提交于 2019-12-28 06:45:42
问题 I have few controllers that inherit from the same base class. Among the different actions that they don't share with each other, they do have a few that are completely identical. I would like to have these on my base class because they all work completely the same it's just that they're accessed through different routes. How should I define these actions with several different routes? My inherited classes also have a RoutePrefixAttribute set on them so each of them is pointing to a different

PostgreSQL foreign key not existing, issue of inheritance?

≡放荡痞女 提交于 2019-12-28 06:32:32
问题 I am struggling with foreign keys in my DB, possibly it has something to do with inheritance? So here's the basic setup: -- table address CREATE TABLE address ( pk_address serial NOT NULL, fk_gadmid_0 integer NOT NULL, -- this table already exists, no problem here street character varying(100), zip character varying(10), city character varying(50), public boolean, CONSTRAINT address_primarykey PRIMARY KEY (pk_address), CONSTRAINT gadmid_0_primarykey FOREIGN KEY (fk_gadmid_0) REFERENCES adm0

Is it possible to add JPA annotation to superclass instance variables?

北城余情 提交于 2019-12-28 06:17:31
问题 I am creating entities that are the same for two different tables. In order do table mappings etc. different for the two entities but only have the rest of the code in one place - an abstract superclass. The best thing would be to be able to annotate generic stuff such as column names (since the will be identical) in the super class but that does not work because JPA annotations are not inherited by child classes. Here is an example: public abstract class MyAbstractEntity { @Column(name=