Class members don't override via inheritance in Java?

Consider following code:
class A
protected int v=1;
void print()
System.out.println(v);
class B extends A
protected int v=2;
public class m
public static void main(String args[])
B kl=new B();
kl.print();
What do you think, what should be the program's output?
It is "1" - I think in C++ it would be "2" (I am not sure of this). Why the member variable "v" doesn't override the one from parent class in "print" method? Do you think it is ok?
Consider changing B class definition to this:
class B extends A
protected int v=2;
B() { v=3; }
The output is still "1",
but:
class B extends A
// protected int v=2;
B() { v=3; }
And the output is "3".
So we have two different variables: A.v and B.v.
Shouldn't be only one there, depending on the class of the object?

Considder the following code.
public class a
   int a;
   public a()
      a = 1;
   public int a()
      return a;
   public int a(a a)
      a.a = a.a++;
      if(this.a == a.a)
         return a();
      else
         return a.a;
}Are you surprised that it actually compiles? Do you have any doubt about the behavour? Do you think it is smart to reuse the symbol a? Do you think it is smart to reuse the symbol v? Because that is what you do. You have two different integers, both with the name v. Then it gets confusing. My advice: Give them different names!

Similar Messages

  • Hiding of super class members in subclass

    I want to hide super class members in subclass. like this
    class super{
    public int method(){
    class sub extends super{
    public string method(){
    give any solution for this criteria, I cann't make methods as private

    In java there is no way to override
    the methodsThat's incorrect. You can override methods in Java.
    However, you cannot do what the OP is trying to do.

  • Re: [iPlanet-JATO] Re: Use Of models in utility classes - Pease don't forget about the regular expression potential

    Namburi,
    When you said you used the Reg Exp tool, did you use it only as
    preconfigured by the iMT migrate application wizard?
    Because the default configuration of the regular expression tool will only
    target the files in your ND project directories. If you wish to target
    classes outside of the normal directory scope, you have to either modify the
    "Source Directory" property OR create another instance of the regular
    expression tool. See the "Tool" menu in the iMT to create additional tool
    instances which can each be configured to target different sets of files
    using different sets of rules.
    Usually, I utilize 3 different sets of rules files on a given migration:
    spider2jato.xml
    these are the generic conversion rules (but includes the optimized rules for
    ViewBean and Model based code, i.e. these rules do not utilize the
    RequestManager since it is not needed for code running inside the ViewBean
    or Model classes)
    I run these rules against all files.
    See the file download section of this forum for periodic updates to these
    rules.
    nonProjectFileRules.xml
    these include rules that add the necessary
    RequestManager.getRequestContext(). etc prefixes to many of the common
    calls.
    I run these rules against user module and any other classes that do not are
    not ModuleServlet, ContainerView, or Model classes.
    appXRules.xml
    these rules include application specific changes that I discover while
    working on the project. A common thing here is changing import statements
    (since the migration tool moves ND project code into different jato
    packaging structure, you sometime need to adjust imports in non-project
    classes that previously imported ND project specific packages)
    So you see, you are not limited to one set of rules at all. Just be careful
    to keep track of your backups (the regexp tool provides several options in
    its Expert Properties related to back up strategies).
    ----- Original Message -----
    From: <vnamboori@y...>
    Sent: Wednesday, August 08, 2001 6:08 AM
    Subject: [iPlanet-JATO] Re: Use Of models in utility classes - Pease don't
    forget about the regular expression potential
    Thanks Matt, Mike, Todd
    This is a great input for our migration. Though we used the existing
    Regular Expression Mapping tool, we did not change this to meet our
    own needs as mentioned by Mike.
    We would certainly incorporate this to ease our migration.
    Namburi
    --- In iPlanet-JATO@y..., "Todd Fast" <toddwork@c...> wrote:
    All--
    Great response. By the way, the Regular Expression Tool uses thePerl5 RE
    syntax as implemented by Apache OROMatcher. If you're doing lotsof these
    sorts of migration changes manually, you should definitely buy theO'Reilly
    book "Mastering Regular Expressions" and generate some rules toautomate the
    conversion. Although they are definitely confusing at first,regular
    expressions are fairly easy to understand with some documentation,and are
    superbly effective at tackling this kind of migration task.
    Todd
    ----- Original Message -----
    From: "Mike Frisino" <Michael.Frisino@S...>
    Sent: Tuesday, August 07, 2001 5:20 PM
    Subject: Re: [iPlanet-JATO] Use Of models in utility classes -Pease don't
    forget about the regular expression potential
    Also, (and Matt's document may mention this)
    Please bear in mind that this statement is not totally correct:
    Since the migration tool does not do much of conversion for
    these
    utilities we have to do manually.Remember, the iMT is a SUITE of tools. There is the extractiontool, and
    the translation tool, and the regular expression tool, and severalother
    smaller tools (like the jar and compilation tools). It is correctto state
    that the extraction and translation tools only significantlyconvert the
    primary ND project objects (the pages, the data objects, and theproject
    classes). The extraction and translation tools do minimumtranslation of the
    User Module objects (i.e. they repackage the user module classes inthe new
    jato module packages). It is correct that for all other utilityclasses
    which are not formally part of the ND project, the extraction and
    translation tools do not perform any migration.
    However, the regular expression tool can "migrate" any arbitrary
    file
    (utility classes etc) to the degree that the regular expressionrules
    correlate to the code present in the arbitrary file. So first andforemost,
    if you have alot of spider code in your non-project classes youshould
    consider using the regular expression tool and if warranted adding
    additional rules to reduce the amount of manual adjustments thatneed to be
    made. I can stress this enough. We can even help you write theregular
    expression rules if you simply identify the code pattern you wish to
    convert. Just because there is not already a regular expressionrule to
    match your need does not mean it can't be written. We have notnearly
    exhausted the possibilities.
    For example if you say, we need to convert
    CSpider.getDataObject("X");
    To
    RequestManager.getRequestContext().getModelManager().getModel(XModel.class);
    Maybe we or somebody else in the list can help write that regularexpression if it has not already been written. For instance in thelast
    updated spider2jato.xml file there is already aCSpider.getCommonPage("X")
    rule:
    <!--getPage to getViewBean-->
    <mapping-rule>
    <mapping-rule-primarymatch>
    <![CDATA[CSpider[.\s]*getPage[\s]*\(\"([^"]*)\"]]>
    </mapping-rule-primarymatch>
    <mapping-rule-replacement>
    <mapping-rule-match>
    <![CDATA[CSpider[.\s]*getPage[\s]*\(\"([^"]*)\"]]>
    </mapping-rule-match>
    <mapping-rule-substitute>
    <![CDATA[getViewBean($1ViewBean.class]]>
    </mapping-rule-substitute>
    </mapping-rule-replacement>
    </mapping-rule>
    Following this example a getDataObject to getModel would look
    like this:
    <mapping-rule>
    <mapping-rule-primarymatch>
    <![CDATA[CSpider[.\s]*getDataObject[\s]*\(\"([^"]*)\"]]>
    </mapping-rule-primarymatch>
    <mapping-rule-replacement>
    <mapping-rule-match>
    <![CDATA[CSpider[.\s]*getDataObject[\s]*\(\"([^"]*)\"]]>
    </mapping-rule-match>
    <mapping-rule-substitute>
    <![CDATA[getModel($1Model.class]]>
    </mapping-rule-substitute>
    </mapping-rule-replacement>
    </mapping-rule>
    In fact, one migration developer already wrote that rule andsubmitted it
    for inclusion in the basic set. I will post another upgrade to thebasic
    regular expression rule set, look for a "file uploaded" posting.Also,
    please consider contributing any additional generic rules that youhave
    written for inclusion in the basic set.
    Please not, that in some cases (Utility classes in particular)
    the rule
    application may be more effective as TWO sequention rules ratherthan one
    monolithic rule. Again using the example above, it will convert
    CSpider.getDataObject("Foo");
    To
    getModel(FooModel.class);
    Now that is the most effective conversion for that code if that
    code is in
    a page or data object class file. But if that code is in a Utilityclass you
    really want:
    >
    RequestManager.getRequestContext().getModelManager().getModel(FooModel.class
    So to go from
    getModel(FooModel.class);
    To
    RequestManager.getRequestContext().getModelManager().getModel(FooModel.class
    You would apply a second rule AND you would ONLY run this rule
    against
    your utility classes so that you would not otherwise affect yourViewBean
    and Model classes which are completely fine with the simplegetModel call.
    <mapping-rule>
    <mapping-rule-primarymatch>
    <![CDATA[getModel\(]]>
    </mapping-rule-primarymatch>
    <mapping-rule-replacement>
    <mapping-rule-match>
    <![CDATA[getModel\(]]>
    </mapping-rule-match>
    <mapping-rule-substitute>
    <![CDATA[RequestManager.getRequestContext().getModelManager().getModel(]]>
    </mapping-rule-substitute>
    </mapping-rule-replacement>
    </mapping-rule>
    A similer rule can be applied to getSession and other CSpider APIcalls.
    For instance here is the rule for converting getSession calls toleverage
    the RequestManager.
    <mapping-rule>
    <mapping-rule-primarymatch>
    <![CDATA[getSession\(\)\.]]>
    </mapping-rule-primarymatch>
    <mapping-rule-replacement>
    <mapping-rule-match>
    <![CDATA[getSession\(\)\.]]>
    </mapping-rule-match>
    <mapping-rule-substitute>
    <![CDATA[RequestManager.getSession().]]>
    </mapping-rule-substitute>
    </mapping-rule-replacement>
    </mapping-rule>
    ----- Original Message -----
    From: "Matthew Stevens" <matthew.stevens@e...>
    Sent: Tuesday, August 07, 2001 12:56 PM
    Subject: RE: [iPlanet-JATO] Use Of models in utility classes
    Namburi,
    I will post a document to the group site this evening which has
    the
    details
    on various tactics of migrating these type of utilities.
    Essentially,
    you
    either need to convert these utilities to Models themselves or
    keep the
    utilities as is and simply use the
    RequestManager.getRequestContext.getModelManager().getModel()
    to statically access Models.
    For CSpSelect.executeImmediate() I have an example of customhelper
    method
    as a replacement whicch uses JDBC results instead of
    CSpDBResult.
    matt
    -----Original Message-----
    From: vnamboori@y... [mailto:<a href="/group/SunONE-JATO/post?protectID=081071113213093190112061186248100208071048">vnamboori@y...</a>]
    Sent: Tuesday, August 07, 2001 3:24 PM
    Subject: [iPlanet-JATO] Use Of models in utility classes
    Hi All,
    In the present ND project we have lots of utility classes.
    These
    classes in diffrent directory. Not part of nd pages.
    In these classes we access the dataobjects and do themanipulations.
    So we access dataobjects directly like
    CSpider.getDataObject("do....");
    and then execute it.
    Since the migration tool does not do much of conversion forthese
    utilities we have to do manually.
    My question is Can we access the the models in the postmigration
    sameway or do we need requestContext?
    We have lots of utility classes which are DataObjectintensive. Can
    someone suggest a better way to migrate this kind of code.
    Thanks
    Namburi
    [email protected]
    [email protected]
    [Non-text portions of this message have been removed]
    [email protected]
    [email protected]

    Namburi,
    When you said you used the Reg Exp tool, did you use it only as
    preconfigured by the iMT migrate application wizard?
    Because the default configuration of the regular expression tool will only
    target the files in your ND project directories. If you wish to target
    classes outside of the normal directory scope, you have to either modify the
    "Source Directory" property OR create another instance of the regular
    expression tool. See the "Tool" menu in the iMT to create additional tool
    instances which can each be configured to target different sets of files
    using different sets of rules.
    Usually, I utilize 3 different sets of rules files on a given migration:
    spider2jato.xml
    these are the generic conversion rules (but includes the optimized rules for
    ViewBean and Model based code, i.e. these rules do not utilize the
    RequestManager since it is not needed for code running inside the ViewBean
    or Model classes)
    I run these rules against all files.
    See the file download section of this forum for periodic updates to these
    rules.
    nonProjectFileRules.xml
    these include rules that add the necessary
    RequestManager.getRequestContext(). etc prefixes to many of the common
    calls.
    I run these rules against user module and any other classes that do not are
    not ModuleServlet, ContainerView, or Model classes.
    appXRules.xml
    these rules include application specific changes that I discover while
    working on the project. A common thing here is changing import statements
    (since the migration tool moves ND project code into different jato
    packaging structure, you sometime need to adjust imports in non-project
    classes that previously imported ND project specific packages)
    So you see, you are not limited to one set of rules at all. Just be careful
    to keep track of your backups (the regexp tool provides several options in
    its Expert Properties related to back up strategies).
    ----- Original Message -----
    From: <vnamboori@y...>
    Sent: Wednesday, August 08, 2001 6:08 AM
    Subject: [iPlanet-JATO] Re: Use Of models in utility classes - Pease don't
    forget about the regular expression potential
    Thanks Matt, Mike, Todd
    This is a great input for our migration. Though we used the existing
    Regular Expression Mapping tool, we did not change this to meet our
    own needs as mentioned by Mike.
    We would certainly incorporate this to ease our migration.
    Namburi
    --- In iPlanet-JATO@y..., "Todd Fast" <toddwork@c...> wrote:
    All--
    Great response. By the way, the Regular Expression Tool uses thePerl5 RE
    syntax as implemented by Apache OROMatcher. If you're doing lotsof these
    sorts of migration changes manually, you should definitely buy theO'Reilly
    book "Mastering Regular Expressions" and generate some rules toautomate the
    conversion. Although they are definitely confusing at first,regular
    expressions are fairly easy to understand with some documentation,and are
    superbly effective at tackling this kind of migration task.
    Todd
    ----- Original Message -----
    From: "Mike Frisino" <Michael.Frisino@S...>
    Sent: Tuesday, August 07, 2001 5:20 PM
    Subject: Re: [iPlanet-JATO] Use Of models in utility classes -Pease don't
    forget about the regular expression potential
    Also, (and Matt's document may mention this)
    Please bear in mind that this statement is not totally correct:
    Since the migration tool does not do much of conversion for
    these
    utilities we have to do manually.Remember, the iMT is a SUITE of tools. There is the extractiontool, and
    the translation tool, and the regular expression tool, and severalother
    smaller tools (like the jar and compilation tools). It is correctto state
    that the extraction and translation tools only significantlyconvert the
    primary ND project objects (the pages, the data objects, and theproject
    classes). The extraction and translation tools do minimumtranslation of the
    User Module objects (i.e. they repackage the user module classes inthe new
    jato module packages). It is correct that for all other utilityclasses
    which are not formally part of the ND project, the extraction and
    translation tools do not perform any migration.
    However, the regular expression tool can "migrate" any arbitrary
    file
    (utility classes etc) to the degree that the regular expressionrules
    correlate to the code present in the arbitrary file. So first andforemost,
    if you have alot of spider code in your non-project classes youshould
    consider using the regular expression tool and if warranted adding
    additional rules to reduce the amount of manual adjustments thatneed to be
    made. I can stress this enough. We can even help you write theregular
    expression rules if you simply identify the code pattern you wish to
    convert. Just because there is not already a regular expressionrule to
    match your need does not mean it can't be written. We have notnearly
    exhausted the possibilities.
    For example if you say, we need to convert
    CSpider.getDataObject("X");
    To
    RequestManager.getRequestContext().getModelManager().getModel(XModel.class);
    Maybe we or somebody else in the list can help write that regularexpression if it has not already been written. For instance in thelast
    updated spider2jato.xml file there is already aCSpider.getCommonPage("X")
    rule:
    <!--getPage to getViewBean-->
    <mapping-rule>
    <mapping-rule-primarymatch>
    <![CDATA[CSpider[.\s]*getPage[\s]*\(\"([^"]*)\"]]>
    </mapping-rule-primarymatch>
    <mapping-rule-replacement>
    <mapping-rule-match>
    <![CDATA[CSpider[.\s]*getPage[\s]*\(\"([^"]*)\"]]>
    </mapping-rule-match>
    <mapping-rule-substitute>
    <![CDATA[getViewBean($1ViewBean.class]]>
    </mapping-rule-substitute>
    </mapping-rule-replacement>
    </mapping-rule>
    Following this example a getDataObject to getModel would look
    like this:
    <mapping-rule>
    <mapping-rule-primarymatch>
    <![CDATA[CSpider[.\s]*getDataObject[\s]*\(\"([^"]*)\"]]>
    </mapping-rule-primarymatch>
    <mapping-rule-replacement>
    <mapping-rule-match>
    <![CDATA[CSpider[.\s]*getDataObject[\s]*\(\"([^"]*)\"]]>
    </mapping-rule-match>
    <mapping-rule-substitute>
    <![CDATA[getModel($1Model.class]]>
    </mapping-rule-substitute>
    </mapping-rule-replacement>
    </mapping-rule>
    In fact, one migration developer already wrote that rule andsubmitted it
    for inclusion in the basic set. I will post another upgrade to thebasic
    regular expression rule set, look for a "file uploaded" posting.Also,
    please consider contributing any additional generic rules that youhave
    written for inclusion in the basic set.
    Please not, that in some cases (Utility classes in particular)
    the rule
    application may be more effective as TWO sequention rules ratherthan one
    monolithic rule. Again using the example above, it will convert
    CSpider.getDataObject("Foo");
    To
    getModel(FooModel.class);
    Now that is the most effective conversion for that code if that
    code is in
    a page or data object class file. But if that code is in a Utilityclass you
    really want:
    >
    RequestManager.getRequestContext().getModelManager().getModel(FooModel.class
    So to go from
    getModel(FooModel.class);
    To
    RequestManager.getRequestContext().getModelManager().getModel(FooModel.class
    You would apply a second rule AND you would ONLY run this rule
    against
    your utility classes so that you would not otherwise affect yourViewBean
    and Model classes which are completely fine with the simplegetModel call.
    <mapping-rule>
    <mapping-rule-primarymatch>
    <![CDATA[getModel\(]]>
    </mapping-rule-primarymatch>
    <mapping-rule-replacement>
    <mapping-rule-match>
    <![CDATA[getModel\(]]>
    </mapping-rule-match>
    <mapping-rule-substitute>
    <![CDATA[RequestManager.getRequestContext().getModelManager().getModel(]]>
    </mapping-rule-substitute>
    </mapping-rule-replacement>
    </mapping-rule>
    A similer rule can be applied to getSession and other CSpider APIcalls.
    For instance here is the rule for converting getSession calls toleverage
    the RequestManager.
    <mapping-rule>
    <mapping-rule-primarymatch>
    <![CDATA[getSession\(\)\.]]>
    </mapping-rule-primarymatch>
    <mapping-rule-replacement>
    <mapping-rule-match>
    <![CDATA[getSession\(\)\.]]>
    </mapping-rule-match>
    <mapping-rule-substitute>
    <![CDATA[RequestManager.getSession().]]>
    </mapping-rule-substitute>
    </mapping-rule-replacement>
    </mapping-rule>
    ----- Original Message -----
    From: "Matthew Stevens" <matthew.stevens@e...>
    Sent: Tuesday, August 07, 2001 12:56 PM
    Subject: RE: [iPlanet-JATO] Use Of models in utility classes
    Namburi,
    I will post a document to the group site this evening which has
    the
    details
    on various tactics of migrating these type of utilities.
    Essentially,
    you
    either need to convert these utilities to Models themselves or
    keep the
    utilities as is and simply use the
    RequestManager.getRequestContext.getModelManager().getModel()
    to statically access Models.
    For CSpSelect.executeImmediate() I have an example of customhelper
    method
    as a replacement whicch uses JDBC results instead of
    CSpDBResult.
    matt
    -----Original Message-----
    From: vnamboori@y... [mailto:<a href="/group/SunONE-JATO/post?protectID=081071113213093190112061186248100208071048">vnamboori@y...</a>]
    Sent: Tuesday, August 07, 2001 3:24 PM
    Subject: [iPlanet-JATO] Use Of models in utility classes
    Hi All,
    In the present ND project we have lots of utility classes.
    These
    classes in diffrent directory. Not part of nd pages.
    In these classes we access the dataobjects and do themanipulations.
    So we access dataobjects directly like
    CSpider.getDataObject("do....");
    and then execute it.
    Since the migration tool does not do much of conversion forthese
    utilities we have to do manually.
    My question is Can we access the the models in the postmigration
    sameway or do we need requestContext?
    We have lots of utility classes which are DataObjectintensive. Can
    someone suggest a better way to migrate this kind of code.
    Thanks
    Namburi
    [email protected]
    [email protected]
    [Non-text portions of this message have been removed]
    [email protected]
    [email protected]

  • Customize class members and methods showed by IDE

    Is anyway to customize the output of the class members and methods show by IDE when you code a class?
    In detail:
    If you have a class A:
    A myClass = new A();
    and you tipe myClass.
    then the IDE show you a list of class members and methods in a Dialog Box....
    can you customize these????????
    I want to set another output for these....
    Thanks!

    That's not the meaning of the question.....
    When someone work with a Java IDE and are editing source, the IDE help you writing code showing you the members and methods of the object that you are working on, like these:
    a.------------------------
    | void setX(int x) |
    | void setY(int y) |
    These information is provided by the IDE. I think that it makes a special type of introspection on classes ..... but a really strong one, because it can access to private members and methods....
    I want to customize these behaviour....... and i want the IDE's show only the strings that i want....
    For exemple:
    a.------------------------
    | to establish X |
    | to establish Y |
    Thanks!

  • Does resteasy API have class loader issues when using via OSGi

    Does resteasy API have class loader issues when using via OSGi

    Hi Scott,
    THis isnt an answer to ur Question, but could u tell me which jar files are needed for the packages:
    com.sap.portal.pcm.system.ISystems
    com.sap.portal.pcm.system.ISystem
    and under which path I coul dfind them.
    Thnx
    Regards
    Meesum.

  • How to stop a class which don't use thread is running

    i have a class call my.class, in my application, i use a button "start button" to run this class by call: my.main(new String[0])
    now I want to use another button call "stop button" to stop my.class.
    If in my.class I don't use thread, have any way to stop it
    please help me, it verry importand for me
    Any help will hightly wellcomed

    I take it that TimTheEnchanters reply to this question was scrubbed by your browser?
    http://forum.java.sun.com/thread.jspa?threadID=579074&tstart=30

  • How to check the show base class members in teststand programatically using c#

    I need to check the checkbox indicating show base class members in teststand using .Net adapter programatically through c#.can I get any help.Using CallMethod , I need to call a method "GetItemByName" .It is only possible by enabling the show Base Class Members  checkbox.Any Help?
    Attachments:
    ex2.gif ‏2305 KB

    bannerghatta -
    This is specified by the DotNetModule.MemberFlags property. The DotNetMemberFlags consist of DotNetMemberFlags_Static (0x1) and DotNetMemberFlags_TopLevel (0x2). These can be used individually or you can perform a Bit-Wise OR to use them together.
    In your case, the Show Base Class Members check box corresponds to the DotNetMemberFlags_TopLevel (0x2). If this flag is set, the box is not checked. If the flag is not set, the box is checked. Thus, you will either want to set DotNetModule.MemberFlags = 0x0 or 0x1 dependent on whether or not you are using a Static Member.
    Since you seem to be doing a lot with the DotNetModule, here is a link to the DotNetModule Help online.
    Hope this helps.
    Message Edited by Manooch_H on 01-07-2010 09:27 AM
    Manooch H.
    National Instruments

  • How to access class members in jsp??

    i m working in jsp and i make a class Student having attribute name and id and i store it at tomcat\classes\Student.java
    and i store a jsp page at tomcat\webapps\root\student\index.jsp
    now i create object of class Student in jsp page it works
    but when i try to access name(Student.name) or id(Student.id)(or any class member of Student class)it shows error message
    now pls tell me how can i access class members in jsp page.

    i m working in jsp and i make a class Student having attribute name and id and i store it at tomcat\classes\mypack\Student.java
    and i store a jsp page at tomcat\webapps\root\student\index.jsp
    now i create object of class Student in jsp page it works
    but when i try to access name(Student.name) or id(Student.id)(or any class member of Student class)it shows error message
    now pls tell me how can i access class members in jsp page.
    Note: class members r public.

  • My iphone5 don't pair via bluetooth with my macbook and my car kit

    My i phone5 don't pair via bluethooth with my macbook and my car kit , both of them can't see the iphone device ? what i have to do to let them pair with the iphone ??

    iPhones will only pair with computers for tethering (sharing an Internet connection).
    As for the car kit, is it in pairing mode?

  • Constructors and Inheritance in Java

    Hi there,
    I'm going over access modifiers in Java from this website and noticed that the following output is displayed if you run the snippet of code.
    Cookie Class
    import javax.swing.*;
    public class Cookie {
         public Cookie() {
              System.out.println("Cookie constructor");
         protected void foo() {
              System.out.println("foo");
    }ChocolateChip class
    public class ChocolateChip extends Cookie {
         public ChocolateChip() {
              System.out.println("ChocolateChip constructor");
         public static void main(String[] args) {
              ChocolateChip x = new ChocolateChip();
              x.foo();
    }Output:
    Cookie constructor
    ChocolateChip constructor
    fooI've been told that constructors are never inherited in Java, so why is "Cookie constructor" still in the output? I know that ChocolateChip extends Cookie, but the Cookie constructor isn't enacted when the new ChocolateChip object is defined... or is it?
    If you can shed any light on this, I would greatly appreciate it.
    Many thanks!

    896602 wrote:
    I've been told that constructors are never inherited in JavaThat is correct. If they were inherited, that would mean that, just by virtue of Cookie having a c'tor with some particular signature, ChocoChip would also "automatically" have a c'tor with that same signature. However, that is not the case.
    , so why is "Cookie constructor" still in the outputBecause invoking a constructor always invokes the parent class's c'tor before any of our own c'tor's body executes, unless the first statement is this(...), to invoke some other c'tor of ours. If this is the case, eventually down the line, some c'tor of ours will not have an explicit this(...) call. It will either have an explicit super(...) call, or no call at all, which ends up leading to the compiler generating a call to super().
    Note that the ability to call super(...) does not mean that that c'tor was inherited.
    I know that ChocolateChip extends Cookie, but the Cookie constructor isn't enacted when the new ChocolateChip object is defined... or is it?Yes, it is. As I pointed out above, if we don't explicitly call this(...) or super(...), then a call to super() is inserted by the compiler.

  • Abt "import" statement & Inheritance in Java

    Hi, All
    As we (Java Programmers) know we'll be using "import java.Math.*" or similar for importing custom/Java packages.
    If i have suppose imported "import java.math.BigDecimal;" in a Class 'A' & and iam extending this class 'A' in another class 'B', if i want to use any method from 'BigDecimal' here in this class(i.e., in class 'B') again i need to import "import java.Math.BigDecimal; " .
    so, my question is :
    1) When we import in one class a package and same package in another class does java import it twice (or) what it does?
    2) when I extend a class , why don't this import facility don't get extended.(I think u got my point.)
    PS: If any problems in this mail technical or what so ever, iam sorry.
    Thanks in advance.

    Importation is just a compiler trick. It has nothing to do with runtime.
    The separation of sources files brings the necessity of repeated imports from file to file.
    You can verify this if you create one source file and put several classes in it: you won't have to import
    more than once.
    so, my answers are :
    1) Each time the compiler encounters an import package.Class statement, it checks the CLASSPATH for the class existence and loads the class definition.
    When a reference to the class is done anywhere in the following statements, the compiler checks the referenced entity (member, method...) against the class definition.
    2) If the extending class is contained in the same source file, then only one import will be needed.
    Hope this helped,
    Regards.

  • Inheritance in Java

    I have a little confusion regarding inheritance in Java... or perhaps the general concept... I was trying to write polymorphic code and I encountered this question...
    When a class inherits from another class, all the member variable is supposed to be inherited right? So, if I have something like:
    abstract class super{
    private SomeObject someObj;
    public abstract void foo();
    class sub extends super{
    public void foo(){
    someObj = new SomeObject();
    the class sub does not know about someObj. I tried different visibility for someObj but none worked...
    Something I have not tried, but might as well settle my question with inheritance here, is that if the super class were a concrete class, are all member variables inherited into its subclasses regardless of the member variables' visibility?
    Can someone clear my confusion? Thanx!

    I changed the visibility to protected and then public and it still would not compile... I think there might be something to do with the abstract.
    However, I remember that long time ago (I had this question for a while) I tested the concept with concrete classes and it seems that the private member variable is not inherited...
    That confused me quite a bit because I thought that when a parent class's member variable was private, a sub-class's object has no access to that variable of an instance of the parent class. But the subclass should have inherited it and should have its own copy of that variable. And when the visibility was protected, that's when an object of the subclass has access to that variable of an object of the parent class...
    I think the confusion comes from if visibility is meant for the class or for the instances of the class...

  • Why don't VIs created from VITs automatically class members if the VIT is a member?

    I have an .lvclass that contains a VIT that I use to create methods.  The VIT is a member of the class so that it can access the data members.  However, the new VIs that I create from the template are not automatically members of the class, so I have to save them and add them to the class when they are complete.
    Is there anything I can do to save the extra steps?
    Solved!
    Go to Solution.

    New... opens this (I actually keep them on a pallate under favorites too with Place Contents enabled) 1 click to place this including documantation 
    Now if only New... was available in Projects...
    Jeff

  • Accessing Enclosing Class Members From Inner Class Subclass

    I have the following scenario that I cannot get to work. Notice the comments in B.doWork() for the problem code. In B.doWork(), how do I access m_strA?
    * A.java
    * Created on July 5, 2002, 2:20 PM
    package Projects.InnerTrouble.Files;
    public class A {
         public abstract class InnerA {
              public abstract void doWork ();
         public String m_strA;
         /** Creates new A */
         public A ()
                   new InnerA() {
                             public void doWork ()
                                       System.out.println("A$InnerA$1's doWork() called!");
                                       m_strA = "Annonymous subclass of InnerA's doWork did this";
                        }.doWork();
         * @param args the command line arguments
         public static void main (String args[])
                   A oTemp = new A();
                   System.out.println(oTemp.m_strA);
                   B.getB(oTemp).doWork();
                   System.out.println(oTemp.m_strA);
    class B extends A.InnerA {
         public B (A a)
                   a.super();
         public void doWork ()
                   System.out.println("B's doWork() called!");
                   // How do I access m_strA within B's doWork() method?  The following is what I would expect to be the answer, but it does not compile
                   // A.this.m_strA = "B's doWork did this";
         private static A.InnerA sm_oInnerA;
         public static A.InnerA getB (A a)
                   if (sm_oInnerA == null)
                        sm_oInnerA = new B(a);
                   return (sm_oInnerA);

    The whole point is that B is not an inner class of A
    so it does not have access to A's member variables.
    Eventhough B extends an inner class of A, that does
    not make B an inner class of A. That is in the JLS,
    but not so elegantly as I have put it, hehe.
    If B were an innerclass of InnerA, then it would
    qualify to access A's member variables.OK, I think that you are finally getting through to my thick skull on this one. Let me restate and get your check-off on my understanding of the situation.
    The only classes with access to A's this reference are A and inner classes of A that are found within the definition of A. So, despite the fact that A and B are in the same package (and B should have access to A's non-private members because B and A are in the same package), and despite the fact that we would normally state that B "is a" InnerA (which is an inner class of A and would have access to a reference to the A.this reference), B is not allowed access to A.this (because B "is not really a" InnerA in the same way that the anonymous implementation of InnerA "is a" InnerA). However, nothing would prevent me from giving B access to a reference of the enclosing A as long as it was done via a method of InnerA, and as long as the implementation of that method is contained in A's implementation.
    Does this "access" rule realy make sense? Are you aware of the justification for this rule? Or is the justification stated in the JLS? I would think that the compiler ought to be able to figure this kind of thing out and allow it. It seems to me the fact that I defined B in the way that I did, and the fact that B "is a" InnerA, implies that I desired a fairly tight relationship to A. In fact, I desired the exact relationship that exists for the anonymous implementation of InnerA.
    The following is a modified version of my original example that runs as I originally wanted it to, but works around the access rules discussed on this forum thread:
    * A.java
    * Created on July 5, 2002, 2:20 PM
    package Projects.InnerTrouble.Files;
    public class A {
         public abstract class InnerA {
              public abstract void doWork ();
              /** added to allow implementors of InnerA that are not enclosed in A's class definition to have access to the enclosing class */
              public A myEnclosingInstance ()
                        return (A.this);
         public String m_strA;
         /** Creates new A */
         public A ()
                   new InnerA() {
                             public void doWork ()
                                       System.out.println("A$InnerA$1's doWork() called!");
                                       m_strA = "Annonymous subclass of InnerA's doWork did this";
                        }.doWork();
         * @param args the command line arguments
         public static void main (String args[])
                   A oTemp = new A();
                   System.out.println(oTemp.m_strA);
                   B.getB(oTemp).doWork();
                   System.out.println(oTemp.m_strA);
    class B extends A.InnerA {
         public B (A a)
                   a.super();
         public void doWork ()
                   System.out.println("B's doWork() called!");
                   // The following is what I would expect to be the answer, but it does not compile
                   // A.this.m_strA = "B's doWork did this";
                   // added myEnclosingInstance() to get functionality desired above
                   myEnclosingInstance().m_strA = "B's doWork did this";
         private static A.InnerA sm_oInnerA;
         public static A.InnerA getB (A a)
                   if (sm_oInnerA == null)
                        sm_oInnerA = new B(a);
                   return (sm_oInnerA);
    }

  • HP CM1312MPF drivers don't work via Windows 7 but works fine via Linux dual booting

    HP CM1312MPF drivers don't seem work using Windows 7 (64-bit) .  Original HP drivers CD now lost. Current drivers were downloaded from HP's website. (This webpage has a number of drivers and most 64-bit ones have been tried. Prior to my Windows re-install, the machine printed and scanned satifactoralily in Windows 7. One day this O/S refused to boot and had to be fully re-installed. The reinstall has been tried on both a SSD and on a HDD. (I have 3 x HDDs and 1 x SSD in this Desktop system. and I dual boot Windows7 and Linux Mint with data on its own HDD.) No attempt has yet been made to scan after this Windows re-install, as I have a separate photo scanner.  I mainly wish on the to print on CM1312MPF, however it did scan perfectly well prior to the reinstall of Windows. THE CM1312MPF PRINTS FINE VIA THE SAME USB OUTPUT LEAD (AND POWERED HUB) AS VIA LINUX MINT 17 (PETRA)  when I dual boot and without any PHYSICAL CHANGES between changing O/Ss. It must be a driver problem, surely, mustn't it? I look forward to your help. John_of_Penrith

    You can also disable that windows wireless "Critter" from device manager and it shouldn't ever again show-up in your adapter list. Thanks for the lesson. I too have wondered about the windows virtual adapter and what it's for. I never had it show-up in my adapters though in the network and sharing center. That was strange that it happened to you. I also occasionally just go to device manager and reload the adapter that came with laptop-not the windows one. It
    seems to keep it running fairly well since there have been no new drivers for it. I think it was an experiment or something called the DW1501 Wireless half-mini n that came with my Dell Inspiron N7010 laptop. I don't particularly like it as compared to the other wireless adapters in my home, this thing is slow on downloads, but I don't want to bother to get a new adapter.
    Anyhow thanks for the interesting lesson.
    LindaSView

Maybe you are looking for

  • PDFs are low res even with Fixed Resolution deselected

    Motion 5.0.6 I am working on a project with a single pdf file imported from Illustrator. There are no other layers in the file, the pdf has no filters applied and Fixed Resolution is selected off. Render is set to Full resolution and Quality to Norma

  • Pdf page generation in javascript

    Hi guys I want to find out a way to generate a pdf page for data taken from a javascript page. I would be really obliged with any sort of help. Thanking you in advance Yogesh Edekar

  • How to add a keyboard shortcut to the Scripts... menu in Photoshop CC 2014? [was: Mune Bar Shortcut]

    Is it possible to add a shortcut to the Scripts... menu item in Photoshop CC 2014 OSX10.9

  • Speeding up playback

    Other than using proxies or bringing in rendered sections of the node tree what are the best ways of speeding up playback? Oh, and I'm having trouble losing the links to files when I move a project. I've tried changing the environment variables and S

  • 2 Question's  regarding "Droid Bionic".

    Hi, could someone please help my find the user's manual for the droid Bionic ((expecting delivery on Wed  would like to read up on new device) ?  Also, this is my first droid any setup tips or general tips would be appreciated. Thank You ! Greg