Static Inheritance, Constructors --- Classes as objects

First I asked myself why constructors are not inherited.
Then I asked myself why Static Methods are not inherited.
And it all seems to come down to, why Classes are not objects in Java as they are in Smalltalk?
Is there any reason I'm missing?
I would love at least inheritable static methods.

That's OK, but that method would not be inherited, and would execute in the context of the superclass.
Picture this (a simple "template method"):
class SuperClass {
     public static void staticMethod() {          
          System.out.println("Static in SuperClass");
          other();          
     public static void other() {          
          System.out.println("other in SuperClass");
class SubClass extends SuperClass{
     public static void other () {
          System.out.println("other in SubClass");
Here, I just want to redefine other() in SubClass, but it calls the SuperClass version.
Let's test:
public class Test {
     public static void main(String[] args) throws DatabaseErrorException, TableUpdateException {
          SuperClass.staticMethod();
          SubClass.staticMethod();
Output:
Static in SuperClass
other in SuperClass
Static in SuperClass
other in SuperClass
First line calls static in SuperClass, which calls other (found in SuperClass and executed), the output from other is second line.
Then, the third line shows calling staticMehod in SubClass, which doesn't redefine it, and executes the SuperClass version, that's ok, but when staticMethod calls other, it doesn't look for it in SubClass, but it directly executes the one in SuperClass (which is shown in line 4).

Similar Messages

  • If java inherites Object , and i inherite another class  how possible ?

    hey !
    if my class B inherits A , and you know that all class inherits a class called Object , then that means B inherts two classes one A (user defined ) and another Object, then is it a violation of rule ? if now how java achieved this feature ? can you justify
    for sack of simplicity i wrote following code .
    class B extends A
    int r;
    B(int a)
    r=a;
    public String toString()
    return r +" ";
    }

    Java supports this:
       Object
          |
          A
          |
          B
          |
          Cbut not this:
       Object
       A     B
          C

  • Using static methods to instantiate an object

    Hi,
    I have a class A which has a static method which instantiates it. If I were to instantiate the class A twice with it's static method within class B then you would think class B would have two instances of class A, right?
    Example:
    class A
       static int i = 0;
       public A(int _i)
           i = _i;
       public static A newA(int _i)
            return new A(_i);
    class B
        public B()
            A.newA(5);
            A.newA(3);
            System.out.println(A.i);
        public static void main(String[] args)
            B b = new B();
    }Is there really two instances of class A or just one? I think it would be the same one because if you check the int i it would be 3 and not 5. Maybe static instantiated objects have the same reference within the JVM as long as you don't reference them yourself?
    -Bruce

    It
    seems like you are the only one having trouble
    understanding the question.Well I was only trying to help. If you don't want any help, its up to you, but if that's the case then I fail to see why you bothered posting in the first place...?
    Allow me to point out the confusing points in your posts:
    you would think class B would have two
    instances of class A, right?The above makes no sense. What do you mean by one class "having" instances of another class? Perhaps you mean that class B holds two static references to instances of Class A, but in your code that is not the case. Can you explain what you mean by this?
    Is there really two instances of class
    A or just one?You have created two instances.
    >I think it would be the same one because if you
    check the int i it would be 3 and not 5.
    I fail to see what that has to do with the number of instances that have been created...?
    Maybe static instantiated objects have the
    same reference within the JVM as long as you
    don't reference them yourself????? What is a "static instantiated object"? If you mean that it was created via some call to a static method, then how is it different to creating an object inside the main method, or any other method that was called by main? (i.e. any method)
    What do you mean by "the same reference within the JVM"? The same as what?
    What happened to the first call "A.newA(5)"?
    I think it was overidden by the second call
    "A.newA(3)".As i already asked, what do you mean by this?
    If you change the line for the constructor
    in A to i = i * i you will get "9" as the
    output, if you delete the call "A.newA(3)"
    you will get "25" as the output. Yes. What part of this is cauing a problem?

  • Inheriting inner classes

    Hi everybody,
    I can't seem to find a clear answer on this, and it's probably a silly simple question, but here goes (it needs a little setup first)--
    A base class X has two subclasses, Y and Z.
    Both Y and Z have static inner classes which contain a factory method that calls their constructors (that is, the methods call the constructors for class Y or class Z).
    Is it possible to have an inner class Q in base class X which can be inherited by classes Y and Z and used to call the constructors of Y and Z?
    Making a separate inner class for each subclass works fine, but they are very similar from class to class, and it would be nice to be able to do this to cut down on code.
    If this is possible, I have one more question:
    Is it possible to have an inner class R in class Y that extends inner class Q in class X?
    If anyone could give me some clarification on these questions, and, if possible, the syntax to answer them, I'd appreciate it.
    Thanks,
    Jezzica85

    jezzica85 wrote:
    Is it possible to have an inner class Q in base class X which can be inherited by classes Y and Z and used to call the constructors of Y and Z?No. If you write new Y() you have named the class you are instantiating. There's no simple way (short of reflection) to express "some other class".
    By the way, did you see my reflection example in your other thread?
    Making a separate inner class for each subclass works fine, but they are very similar from class to class, and it would be nice to be able to do this to cut down on code.You can move most common code up the hierarchy.
    >
    If this is possible, I have one more question:
    Is it possible to have an inner class R in class Y that extends inner class Q in class X?The simplest way to find out is to try it:
    class X {
        class Q {}
        static class S {}
    class Y extends X {
        class R extends Q {}
        static class T extends S{}
    }--BDLH                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Why aren't inherited constructors returned?

    Let's say I have an abstract class that defines a public constructor, and a different class that extends this abstract class. I'd like to use reflection to instantiate an object of the subclass using the constructor of the parent class. If this is possible to do in code, why is it not possible to do at run-time? (Or am I hopefully missing something, and there is in fact a way to do this?) If Class.getMethods() will return inherited methods, why won't Class.getConstructors() return inherited constructors?

    Let's say I have an abstract class that defines a
    public constructor, and a different class that extends
    this abstract class. I'd like to use reflection to
    instantiate an object of the subclass using the
    constructor of the parent class. If this is possible
    to do in code, It's not possible to do it "in code" (by which I assume you mean the "normal" way, without reflection). As the other poster said, constructors are not inherited.
    If you're specifically interested in reflection, the following may not be particularly applicable to your current problem. However, you'll need to understand these rules anyway...
    Here are the rules for constructors--"ctors" because I'm lazy. Also, because I'm lazy, "super(...)" and "this(...)" mean any super or this call, regardless of how many args it takes, including those that take no args.
    1) Every class has at least one ctor.
    1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().
    1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a public MyClass() {...} if you want one.
    1.3) Constructors are not inherited.
    2) The first statement in the body of any ctor is either a call to a superclass ctor super(...) or a call to another ctor of this class this(...) 2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor super() as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.
    2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.

  • Assigning object of one class to object of another class.

    Hi,
    How will i assign an object of one class to an object of another class?
    Ex:
    ClassX A=some_method();
    Now how will I assign the object A of class ClassX to the object 'B' of another class 'ClassY'.

    In java you can only assign a object reference of one class into object reference of another class if the first class is the Second class (in other words the first class is a subclass of second class).
    for example if this is a inheritance chart
    Car ==========>Mercedes
    "===========>Audi
    then you can use
    Audi a1 = new Audi();
    Car c1 = a1;
    or Mercedes m1 = new Mercedes();
    Car c1 = m1;
    but not
    a1 = m1;
    before assigning a variable into another variable of different class, use:
    if(variable1 instanceOf ToBeAssignedIn Class){
    variable2 = variable1;
    example:
    Audi a1;
    Car c1;
    if(a1 instanceOf Car){
    c1 = a1;
    Edited by: gaurav.suse on Apr 10, 2012 1:14 PM
    Edited by: gaurav.suse on Apr 10, 2012 1:15 PM

  • Copy constructor and temporary objects in C++

    I'd need some clarification on how constructor works when the argument is  a temporary object. I defined a dummy MyVect class with operator+ and copy constructor:
    class MyVect {
    public:
    MyVect() {
    cout << "Constructor" << endl;
    MyVect(const MyVect &vect){
    cout << "Copy Constructor" << endl;
    ~MyVect() {
    cout << "Destructor" << endl;
    MyVect operator+(const MyVect &v) const {
    cout << "operator+" << endl;
    MyVect result
    return result;
    Then I test it with this very simple code pattern:
    MyVect v1;
    MyVect v2;
    MyVect v3(v1 + v2);
    The output is:
    Constructor
    Constructor
    operator+
    Constructor
    Destructor
    Destructor
    Destructor
    The first two constructor calls are for v1 and v2, then operator+ for  v1+v2 is called, and  inside it there is a constructor call for result object. But then no copy constructor (nor constructor) for v3 is called. In my limited understanding of C++, I guessed that once the temporary object resulting from v1+v2 has been created, then it would be the argument of the copy constructor call. Instead it looks like the compiler transforms the temporary into v3, since v3 is correctly initialized as the sum of v1 and v2 (I tried with a more complicated version of MyVect to verify this). This behavior reminds me of the move semantics introduced with C++11, but I'm not using the -std=c++11 flag (and in fact if I try to define a move constructor the compilation fails).
    Can anyone please help me to understand what's happening under the hood? Thanks.
    Last edited by snack (2013-02-13 10:44:28)

    wikipedia wrote:The following cases may result in a call to a copy constructor:
    When an object is returned by value
    When an object is passed (to a function) by value as an argument
    When an object is thrown
    When an object is caught
    When an object is placed in a brace-enclosed initializer list
    These cases are collectively called copy-initialization and are equivalent to:[2] T x = a;
    It is however, not guaranteed that a copy constructor will be called in these cases, because the C++ Standard allows the compiler to optimize the copy away in certain cases, one example being the return value optimization (sometimes referred to as RVO).
    Last edited by Lord Bo (2013-02-13 13:58:47)

  • Need help w/ created classes and objects

    I am having a difficult time understanding what is wrong w/ my classes and objects. I've looked in two books and have messed around a bit. Here is what I am -attempting- to do.
    I want to make a class called CLOTHING. In this class i want objects such as shirt and pants (for now).
    these are the errors im getting:
    .\Shirt.java:6: invalid method declaration; return type required
         public Shirt(int size){
                   ^
    .\Shirt.java:3: class Clothing is public, should be declared in a file named Clothing.java
    public class Clothing {
           ^
    MyClassHW.java:10: cannot resolve symbol
    symbol  : constructor Shirt  (int,int)
    location: class Shirt
              myShirt = new Shirt(1,1);
                              ^
    3 errors------------------------------------------------------------
    This is the code for my Clothing class:
    import java.awt.*;
    public class Clothing {
         private int shirtSize;
         private void Shirt(Graphics s, int h, int v){
         Polygon shirts;
              shirts = new Polygon();
              shirts.addPoint(5+h,8+v); // 1
              shirts.addPoint(17+h,12+v); // 2
              shirts.addPoint(19+h,13+v); // 3
              shirts.addPoint(33+h,8+v); // 4
              shirts.addPoint(37+h,13+v); // 5
              shirts.addPoint(25+h,20+v); // 6
              shirts.addPoint(25+h,28+v); // 7
              shirts.addPoint(15+h,28+v); // 8
              shirts.addPoint(15+h,20+v); // 9
              shirts.addPoint(1+h,12+v); //10
              s.fillPolygon(shirts);
    }from what i understand each object is essentially a method...
    Here is the code for the java applet I'm making:
    import java.awt.event.*;
    import java.awt.*;
    import java.applet.*;
    public class MyClassHW extends Applet { // implements ActionListener, AdjustmentListener {
         Shirt myShirt;
         int size;
         public void init(){
              myShirt = new Shirt(1,1);
    // <applet code = "MyClassHW.class" height = 300 width=300> </applet>thank you for your time and help. as always, your efforts are appriciated.

    .\Shirt.java:6: invalid method declaration; return type required     public Shirt(int size){
    I guess, public Shirt(int size) is constructor in class shirt. You have one int in this constructor. Shirt(int size)
    Here you initialize your shirt with two int:
    MyClassHW.java:10: cannot resolve symbolsymbol : constructor Shirt (int,int)location: class Shirt          myShirt = new Shirt(1,1);
    Shirt(int size, int what?)
    It's why it show such message.
    But even if you fix it seems like you have very vague ideas about what you are doing.
    As far as I understood you need three classes.
    First applet Clothing:
    public class Clothing extends Applet{
    Shirt myShirt;
    Pants myPants;
    public void init() {
    myShirt = new Shirt(1); // small shirt
    myPants = new Pants(32, 40); // medium waist and long legs
    Second and third your classes Shirt and Pants:
    public class Shirt extends Object {
    int size;
    public Shirt(int s) {
    size = s;
    public int getSize () {
    return size;
    same for Pants, only you need two parameters for it or whatever you want.
    You could design it diffrently if you want derive your Shirts and Pants from Clothing and then to use them in your applet. But, indeeed, you desperately have to do some serious reading, where authors are accurate in they definitions and stuff, because it's kind of complicated.

  • Which of this is true about class and object?

    (a) A class is made of objects
    (b) A class is a collection of objects
    (c) A class supplies or delivers objects to the rest of the application
    (d) A class is used as a template to create objects
    (e) An object inherits its variables and their values from its class
    (f) An object inherits its variables and methods from its class
    (g) An object inherits its variables and methods from its class and its superclasses
    (h) The variables and methods of a class are defined in its objects
    (i) The variables and methods of an object are defined in its class
    (j) The variables, variable values and methods of an object are given in its class

    (a) A class is made of objects - true
    (b) A class is a collection of objects - true
    (c) A class supplies or delivers objects to the rest of the application - true
    (d) A class is used as a template to create objects - true
    (e) An object inherits its variables and their values from its class - false
    (f) An object inherits its variables and methods from its class - true
    A class is a not collection of objects. A class not only contains objects
    A class represents objects and others.

  • Class related object

    How can I create a class related object??
    In my code the class doens't find the log object(FileWriter)
    example:
    public class LogFile
    public LogFile()
    try
    File outputFile = new File("/Java/Iss/log/BatchQuery.log");
    FileWriter log = new FileWriter(outputFile);
    catch (IOException exp)
    System.out.println("Fatal error, can't write to log - "+exp.getMessage());
    private static LogFile instance = new LogFile();
    public static LogFile getInstance()
    if(instance==null)
    return instance = new LogFile();
    else
    return instance;
    public static synchronized void write(String toLog)
    log.write(toLog);
    log.write("\n");
    public static synchronized void close()
    log.close();
    }

    Hi, shouldn t your code be like this ? :
    public class LogFile
    private static LogFile instance = new LogFile();
    private FileWritter log = null;
    public LogFile()
    try
    File outputFile = new File("/Java/Iss/log/BatchQuery.log");
    log = new FileWriter(outputFile);
    catch (IOException exp)
    System.out.println("Fatal error, can't write to log - "+exp.getMessage());
    public static LogFile getInstance()
    if(instance==null)
    return instance = new LogFile();
    else
    return instance;
    public synchronized void write(String toLog)
    log.write(toLog);
    log.write("\n");
    public synchronized void close()
    log.close();
    instance = null;
    So now you have a static getInstance() method to recover the unique instance.
    All others methods shouldn t be static so that they shall be called only if an instance has been generated.

  • Custom Inherited/Derived Class & the Data Warehouse

    Hi there,
    I'd like to reference an older post by Travis in regards to getting a Custom Class into the Dataware House:
    DW Reporting on Custom Class
    Scenario:
    I've created an Inherited/Derived Class, with its own Form (not an
    Extension).
    This Class Inherits from the Base Incident Class.
    Inherited Class has two (2) new properties (a String and a List Property).
    Class and Form is all working well within the SCSM Console.
    By default, this class is inheriting all the Relationships (like AssignedTo, AffectedUser, CreatedBy, HasConfigItem) from the Base Incident Class without me having to do anything in the mp apart from including the <Reference> & <TypeProjection>
    components.
    To get this class I am aware that it has
    to be defined - as in a new dimension has to be created for this Derived Class.
    Questions:
    1. Is it recommended to have these dimension/s in a separate sealed mp, or include it with the derived Class mp (sealed) - is there a best practice?
    2. Online resources (technet and other) state a dimension has to be created for the new Class <Dimensions>, but do I also need to include <Outriggers> as well as <Facts>
    for this Class?

    In JDeveloper 10.1.2, I configure in adfjclient_binding.xml:
    <controlDefinition name="JTable Ext"
    className="com.lib.swing.JTableExt"
    classPath=""
    shortLabel="JTableExt"
    longLabel="com.lib.swing.JTableExt"
    tooltipText="com.lib.swing.JTableExt"
    bindingType="DCTable"
    icon="/oracle/ideimpl/resource/images/palette/JTable.gif">
    <useTemplate>
    <![CDATA[${FieldName}.setBinding(panelBinding,"${BindingName}","${IteratorBinding.getId()}")]]>
    </useTemplate>
    <imports>
    <![CDATA[javax.swing.JTable;javax.swing.table.TableModel;com.lib.swing.JTableExt]]>
    </imports>
    </controlDefinition>
    and In my projects, I only drag and drop JTableExt into my form, it auto generate code:
    jTableExt1.setBinding(panelBinding, "SubProfileTypeView2", "SubProfileTypeView1Iterator");
    because setBinding function of JTableExt in my library, it contains setmodel function and some other function.
    Now, In JDeveloper 10.1.3, I drag and drop JTableExt into my form and I have to code one line:
    jTableExt1.setBinding(panelBinding, "SubProfileTypeView2", "SubProfileTypeView1Iterator");
    Which way is there, I haven't to code?
    Jacque

  • Class and Object name change in the universe designer

    Hello Experts
    I have a confusion , I am just wondering if I devlop a bobj universe, lets say, based on SQL database and change the name of the class and object during the creation of the universe, will that fetch the data from d/base properly while running a query / report. Although universe class and object has different names than database now but the records are the same. Do we need to point the object to d/base object in some kind of different or special way .
    To make my question more simple, In d/base table name is "Employee" whereas on universe side I create a class name "Staff" and at the same time field name in the database is "Emploee ID" whereas in the universe I named it "Badge number".
    Please advise if that will make any difference while I run the query or Is there any kind of complication on the universe side that I should expect which I am not familiar with.
    I would apprecite your response.
    Best Regards

    Hello experts,
    Let me rephrase the issue with exact scenario.
    I have a table names "REGION" with fields region id, region and country id.
    I have another table name REGION_SLINE with fields SL_id, region id and Sales_revenue.
    I created an equi join between these two tables based on region id field and checked the cardinality which is ok.
    Now when I try to create a report based on sales revenue per customer ( "customer's first name"  is an other field on CUSTOMER table), I dont get any result in the report and get a message that "No data to reterive"
    Also please note that when I run a report which is sales revenue per region id, I get the result, seem slike these two tables REGION and REGION_SLINE can generate the report but sales revenue per customer report is not generated because customer first name is a field of another table.
    I was just wondering if I need to write some kind of where clause in the object properties of region id which is used to create equi joon link.
    I woulld appreciate your response.
    Regards
    Edited by: SAP_LCCHS on Jul 4, 2011 9:19 AM
    Edited by: SAP_LCCHS on Jul 4, 2011 9:21 AM

  • Object Class and Object Id for material Determination tables.

    I want to know what is the Object Class and Object Id for material Determination records to verify tables CDHDR and CDPOS.
    The purpose is to know the changes done by the different users for material determination records.
    Can any one help.

    Hi ZZZSUNNY,
    Similar question is answered recently.Please find the below link which will helps you
    Material determination: how to see the creater of a record?
    Thanks
    Dasaradha

  • Object class for object id 'ADRNR'

    Hi all,
    How can iget teh value of object class for object id 'ADRNR'. i tried to see teh contents in CDHDR but there are no entries in the dev and QA box either.
    I need this value to pass to the CHANGEDOCUMENT_OPEN
    and CHANGEDOCUMENT_CLOSe fm's.
    Thanks

    How can we find the correct object class id for a data element ?
    Regards
    Megha

  • Find Universe, classes and objects used in each report

    I want to find a list of universes, classes and objects used in each report
    or the other way to find list of reports which use a particular universe. please let know, i could not get much information from activity universe in a proper way.

    Hello Venkataramat,
    plese post in more detail what kind of report you are using Crystal report ? webi ? Deski.
    Please post in the specific forums.
    If you have a Crystal Report I recommend to post this query to the [Crystal Reports Design|SAP Crystal Reports; forum.
    Best regards
    Falk

Maybe you are looking for

  • How do I delete a credit card from my iTunes store account

    How do delete a credit card my iTunes account

  • Having problem with javax packages

    i had problem with compiling my program which is java servlet, and it is complaining that it does exist javax servlet * package , how can i solve this problem?

  • Connecting Ipod mini to mini dock problems...

    Hmm.. i just got my ipod mini charger today and when i plugged it into my computer and then connected my ipod mini my ipod went dead.. it would not charge and then when i took it off the charger it would no longer turn on and it is NOT on hold (i've

  • IMac G5 doesn' doi RSS/Atom

    I have used Safari for RSS since it started supporting it. I decided to check out some of the dedicated apps. I have found that my iMac G5 will not connect to any subscriptions using any of the programs. I have used NetNewsWireLite, Shrook, Owl and a

  • Material in different plants

    Hi guru's Is there is any transaction code for an overview the same material in different plants thanks in advance tulja singh