Search Results

Search found 256 results on 11 pages for 'superclass'.

Page 2/11 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11  | Next Page >

  • Use of 'super' keyword when accessing non-overridden superclass methods

    - by jonny
    I'm trying to get the hang of inheritance in Java and have learnt that when overriding methods (and hiding fields) in sub classes, they can still be accessed from the super class by using the 'super' keyword. What I want to know is, should the 'super' keyword be used for non-overridden methods? Is there any difference (for non-overridden methods / non-hidden fields)? I've put together an example below. public class Vehicle { public int tyreCost; public Vehicle(int tyreCost) { this.tyreCost = tyreCost; } public int getTyreCost() { return tyreCost; } } and public class Car extends Vehicle { public int wheelCount; public Vehicle(int tyreCost, int wheelCount) { super(tyreCost); this.wheelCount = wheelCount; } public int getTotalTyreReplacementCost() { return getTyreCost() * wheelCount; } } Specifically, given that getTyreCost() hasn't been overridden, should getTotalTyreReplacementCost() use getTyreCost(), or super.getTyreCost() ? I'm wondering whether super should be used in all instances where fields or methods of the superclass are accessed (to show in the code that you are accessing the superclass), or only in the overridden/hidden ones (so they stand out).

    Read the article

  • Subclass not seeing superclass' instance variable

    - by hyn
    I have a situation where my subclass is not seeing the superclass' instance variable x. The ivar is obviously @protected by default, so why do I get a compiler error "x undeclared"? If I make this ivar a property then I can access it by self.x. What's worse is sometimes the error disappears, so something in my code is obviously causing this error but I cannot figure it out.

    Read the article

  • Synthesized property of a protocol not seeing superclass' ivar

    - by hyn
    I have a situation where my subclass is not seeing the superclass' instance variable x. The ivar is obviously @protected by default, so why do I get a compiler error "x undeclared"? - (CGSize)hitSize { // Compiler error return size; } EDIT: hitSize is a property of a protocol my subclass is conforming to. The problem was that I had hitSize @synthesized, which was the culprit. The question then is why can't the synthesized getter see the ivar?

    Read the article

  • C++ superclass constructor calling rules

    - by levik
    What are the C++ rules for calling the superclass constructor from a subclass one?? For example I know in Java, you must do it as the first line of the subclass constructor (and if you don't an implicit call to a no-arg super constructor is assumed - giving you a compile error if that's missing).

    Read the article

  • Rails STI: SuperClass Model Methods called from SubClass

    - by Karl
    I would like a little confirmation that I'm doing this correctly. Using rails single table inheritance I have the following models and class method: class PhoneNumber < ActiveRecord::Base def self.qual?(number) klass = self klass.exists?(:phone_number => phone_number) end end class Bubba < PhoneNumber end class Rufus < PhoneNumber end Bubba.qual?("8005551212") Tests pass and everything seems to work properly in rails console. Just wanted to confirm that I'm not headed for future trouble by using self in the superclass PhoneNumber and using that to execute class methods on subclasses from the parent. Is there a better way?

    Read the article

  • Validating class and superclass on RoR

    - by Luís Guilherme
    In ruby, you have an attribute called "type" which is the class of the object. Rails stores this at the database in a column called type. So, if I have several blog "types", I can do something like this def create @blog = Blog.new(params[:blog]) @blog[:type] = params[:blog][:type] # ... end If I add someone like this, and then load it, and ask its class (for instance, at the console), I have the right class name answered back. However, when I save it afterwards, rails will run only the superclass validators, not the ones I defined in the subclass. How should I make rails run the subclass validators?

    Read the article

  • ActionScript: Determine wether superclass implements a particular interface?

    - by David Wolever
    Is there any non-hacky way to determine wether a class' superclass implements a particular interface? For example, assume I've got: class A extends EventDispatcher implements StuffHolder { private var myStuff = someKindOfStuff; public function getStuff():Array { if (super is StuffHolder) // <<< this doesn't work return super['getStuff']().concat([myStuf]); return [myStuff]; } class B extends A { private var myStuff = anotherKindOfStuff; } How could I perform that super is StuffHolder test in a way that, well, works? In this case, it always returns true.

    Read the article

  • Reflection: cast an object to subclass without use instaceof

    - by Fabrizio
    I have this simple interface/class: public abstract class Message { } public class Message1 extends Message{ } public class Message2 extends Message{ } And an utility class: public class Utility { public void handler(Message m){ System.out.println("Interface: Message"); } public void handler(Message1 m){ System.out.println("Class: Message1"); } public void handler(Message2 m){ System.out.println("Class: Message2"); } } Now, the main class: public static void main(String[] args) { Utility p=new Utility(); Message1 m1=new Message1(); p.handler(m1); Message m=(Message) m1; p.handler(m); } The output is Class: Message1 Interface: Message I would that p.handler(m) call the method p.handler(m:Message1) I don't want use the "manual" command instanceof because I have many cases: if(m instance of Message1) p.handler((Message1)m) else if (m instanceof Message2) p.handler((Message2)m) ... If I call m.getClass() I obtain "mypackage.Message1", so the subclass and not the superclass. I try with this code (use reflection): p.handler(m.getClass().cast(m)); But the output is Interface: Message So, this is my problem. I would do a runtime cast of superclass object to subclassobject without use the "code command" istanceof. I would a right command like this: p.handler((m.getclass)m); How can I obtain it? It's possible? Thank in advance. Fabrizio

    Read the article

  • [JAX-B] How can I ignore a superclass?

    - by MrSpandex
    I'm trying to write a web service for the java.util.logging api. So I wrote a class MyLogRecord that inherits from LogRecord. I annotated this class with JAX-B annotations, including @XmlAccessorType(XmlAccessType.NONE) so it would ignore non-annotated fields and properties. When I start up tomcat, I get errors that java.util.logging.Level and other java.util.logging classes do not have a default constructor, but none of my annotated methods make any reference to the Level class or any of the other java.util.logging classes. These are referenced by the parent class. My sub-class has everything it needs defined. How can I get JAX-B to ignore the parent class completely? Update: I found another post on this, which suggests modifying the parent class. This is obviously not possible because I am extending a java.util class. IS there any way to do this without modifying the superclass? Update2: I found a thread on java.net for a similar problem. That thread resulted in an enhancement request, which was marked as a duplicate of another issue, which resulted in the @XmlTransient annotation. The comments on these bug reports lead me to believe this is impossible in the current spec.

    Read the article

  • UITableView superClass for delegate?

    - by fuzzygoat
    A quick question, I am setting a delegate for UITableView and I have a question regarding setting the delegate and dataSource properties. I have noticed that the properties for delegate and dataSource are not available, I was thinking that adopting the protocols would make them available. But I am now thinking that I maybe have the superclass for my delegate class wrong. Currently I have: -(void)viewDidLoad { TestDelegate *tempDelegate = [[TestDelegate alloc] init]; [self setMyDelegate:tempDelegate]; // setDelegate // setDataSource [tempDelegate release]; [super viewDidLoad]; } My interface for TestDelegate looks like: @interface TestDelegate : NSObject <UITableViewDelegate, UITableViewDataSource> { NSArray *listData; int myCounter; } Can I ask if the above should be: @interface TestDelegate : UITableView <UITableViewDelegate, UITableViewDataSource> { NSArray *listData; int myCounter; } gary EDIT: I think it might be right as NSObject, I have a viewtableView in IB, thats what I will need to connect my delegate class to. I added to tableView in IB so maybe I just need to make it available in Xcode.

    Read the article

  • Correct usage of "<T extends SuperClass>"

    - by yusaku
    I am not familiar with "Generics". Is it a correct use of "<T extends SuperClass>" ? And do you agree that the codes after using generics are better? Before using Generics ================================================= public abstract class SuperSample { public void getSomething(boolean isProcessA) { doProcessX(); if(isProcessA){ doProcessY(new SubASample()); }else{ doProcessY(new SubBSample()); } } protected abstract void doProcessX(); protected void doProcessY(SubASample subASample) { // Nothing to do } protected void doProcessY(SubBSample subBSample) { // Nothing to do } } public class SubASample extends SuperSample { @Override protected void doProcessX() { System.out.println("doProcessX in SubASample"); } @Override protected void doProcessY(SubASample subASample) { System.out.println("doProcessY in SubASample"); } } public class Sample { public static void main(String[] args) { SubASample subASample = new SubASample(); subASample.getSomething(true); } } After using Generics ================================================= public abstract class SuperSample { public void getSomething(boolean isProcessA) { doProcessX(); if(isProcessA){ doProcessY(new SubASample()); }else{ doProcessY(new SubBSample()); } } protected abstract void doProcessX(); protected abstract <T extends SuperSample> void doProcessY(T subSample); } public class SubASample extends SuperSample { @Override protected void doProcessX() { System.out.println("doProcessX in SubASample"); } @Override protected <T extends SuperSample> void doProcessY(T subSample) { System.out.println("doProcessY in SubASample"); } } public class Sample { public static void main(String[] args) { SubASample subASample = new SubASample(); subASample.getSomething(true); } }

    Read the article

  • Setting the type of a field in a superclass from a subclass (Java)

    - by Ibolit
    Hi. I am writing a project on Google App Engine, within it I have a number of abstract classes that I hope I will be able to use in my future projects, and a number of concrete classes inheriting from them. Among other abstract classes I have an abstract servlet that does user management, and I hava an abstract user. The AbstractUser has all the necessary fields and methods for storing it in the datastore and telling whether the user is registered with my service or not. It does not implement any project specific functionality. The abstract servlet that manages users, refers only to the methods declared in the AbstractUser class, which allows it to generate links for logging in, logging out and registering (for unregistered users). In order to implement the project-specific user functionality I need to subclass the Abstract user. The servlets I use in my project are all indirect descendants from that abstract user management servlet, and the user is a protected field in it, so the descendant servlets can use it as their own field. However, whenever i want to access any project specific method of the concrete user, i need to cast it to that type. i.e. (abstract user managing servlet) ... AbstractUser user = getUser(); ... abstract protected AbstractUser getUser(); (project-specific abstract servlet) @Override protected AbstractUser getUser() { return MyUserFactory.getUser(); } any other project specific servlet: int a = ((ConcreteUser) user).getA(); Well, what i'd like to do is to somehow make the type of “user” in the superclass depend on something in the project-specific abstract class. Is it at all possible? And i don't want to move all the user-management stuff into a project-specific layer, for i would like to have it for my future projects already written :) Thank you for your help.

    Read the article

  • How to get the type of the class for comparison

    - by Halo
    I have this object which is an instance of a superclass. I want to know which subclass that object really is, so that I can decide what to do with it. There is this getClass() method but it's apparently not used for comparison issues. How can I get the sub-type of my object?

    Read the article

  • JavaScript inheritance extend function

    - by Zach
    I'm having some trouble understanding the IF clause at the end of this function from Pro JavaScript Design Patterns: function extend(subClass, superClass) { var F = function() {}; F.prototype = superClass.prototype; subClass.prototype = new F(); subClass.prototype.constructor = subClass; subClass.superclass = superClass.prototype; if(superClass.prototype.constructor == Object.prototype.constructor) { superClass.prototype.constructor = superClass; } } The book explains that these lines ensure that the superclass's constructor attribute is correctly set, even if the superclass is the Object class itself. Yet, if I omit those three lines and do the following: function SubClass() {}; extend(SubClass, Object); alert(Object.prototype.constructor == Object); The alert says 'true', which means the superclass's constructor is set correctly even without those last three lines. Under what conditions, then, does this IF statement do something useful? Thanks.

    Read the article

  • Single Table Per Class Hierarchy with an abstract superclass using Hibernate Annotations

    - by Andy Hull
    I have a simple class hierarchy, similar to the following: @Entity @Table(name="animal") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="animal_type", discriminatorType=DiscriminatorType.STRING) public abstract class Animal { } @Entity @DiscriminatorValue("cat") public class Cat extends Animal { } @Entity @DiscriminatorValue("dog") public class Dog extends Animal { } When I query "from Animal" I get this exception: "org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: Animal" If I make Animal concrete, and add a dummy discriminator... such as @DiscriminatorValue("animal")... my query returns my cats and dogs as instances of Animals. I remember this being trivial with HBM based mappings but I think I'm missing something when using annotations. Can anyone help? Thanks!

    Read the article

  • Java getMethod with superclass parameters in method

    - by Jonathon
    Given: class A { public void m(List l) { ... } } Let's say I want to invoke method m with reflection, passing an ArrayList as the parameter to m: List myList = new ArrayList(); A a = new A(); Method method = A.class.getMethod("m", new Class[] { myList.getClass() }); method.invoke(a, Object[] { myList }); The getMethod on line 3 will throw NoSuchMethodException because the runtime type of myList is ArrayList, not List. Is there a good generic way around this that doesn't require knowledge of class A's parameter types?

    Read the article

  • JAXB, marshalling sub-class that has the same rootNode name as the superclass

    - by SCdF
    Let's say I have this: public class Foo { private String value; // <snip> getters and setters, constructors etc } And I also have this: public class Bar extends Foo { private String anotherValue; // <snip> getters and setters, constructors etc } I want to be able to marshall this to a Bar object: <foo> <value>smang</value> <anotherValue>wratz</anotherValue> </foo> I'm not in a position to check right now, but if I change the @XmlRootNode name of Bar to 'foo' will that work? Do I have to do anything more clever than that?

    Read the article

  • how to register a selectionProvider for the Viewer from a superclass

    - by Tom
    I have a View which extends a View provided by another plugin like this; public class View2 extends some.other.package.View1 { public void createPartControl(Composite parent) { super.createPartControl(parent); //I want to do this, but I can't because its private //getSite().setSelectionProvider(treeViewer); } } But I can't edit the super class, so I need to access the viewer from the Control. public class some.other.package.View1 extends ViewPart { private TreeViewer treeViewer; public void createPartControl(Composite parent) { treeViewer = new TreeViewer(parent, SWT.V_SCROLL); } } I tried using parent.getChildren(); to get a Tree object, but I can't see that it can be cooerced into an ISElectionProvider as required

    Read the article

  • Using auto-generated event handler in both class and it's superclass

    - by abatishchev
    I have a abstract class: public abstract class MySuperControl : UserControl { protected void Page_PreRender(object sender, EventArgs e) { // do something in super } } and a class that is code-behind for a designed UserControl: public partial class MyControl : MySuperControl { protected void Page_PreRender(object sender, EventArgs e) { // do something in class } } how do declare both of them to nest automated call?

    Read the article

  • Hibernate -using Table per subclass - how to link an existing superclass object to a sibclass object

    - by Chandni
    Hi, I have a User hibernate class, Clerk class and Consumer class. All these maps to their own tables in database. The User PK also acts as Clerk's and Consumer's PK. So now my problem is that if a user is initially a Clerk, he has a record in Users table and Clerks table. If that user wants to become a consumer, I want to link that User's record to the new Consumer's record. So even if I pass the userId to the consumer's record, it treats it as a new User to be persisted and gives a duplicate_key exception. How do I tell Hiernate to link the same user object with this new Consumer object. Thanks in advance, -Chandni

    Read the article

  • Java Object superclass

    - by SHiNKiROU
    I have a weird Java question: As we know: All Java classes extend java.lang.Object All Java classes cannot extend itself Then, java.lang.Object must extend java.lang.Object, which is itself, therefore, it should be impossible. How is Object implemented in Java?

    Read the article

  • Java: Initializing a public static field in superclass that needs a different value in every subclas

    - by BinaryMuse
    Good evening, I am developing a set of Java classes so that a container class Box contains a List of a contained class Widget. A Widget needs to be able to specify relationships with other Widgets. I figured a good way to do this would be to do something like this: public abstract class Widget { public static class WidgetID { // implementation stolen from Google's GWT private static int nextHashCode; private final int index; public WidgetID() { index = ++nextHashCode; } public final int hashCode() { return index; } } public abstract WidgetID getWidgetID(); } so sublcasses of Widget could: public class BlueWidget extends Widget { public static final WidgetID WIDGETID = new WidgetID(); @Override public WidgetID getWidgetID() { return WIDGETID; } } Now, BlueWidget can do getBox().addWidgetRelationship(RelationshipTypes.SomeType, RedWidget.WIDGETID, and Box can iterate through it's list comparing the second parameter to iter.next().getWidgetID(). Now, all this works great so far. What I'm trying to do is keep from having to declare the public static final WidgetID WIDGETID in all the subclasses and implement it instead in the parent Widget class. The problem is, if I move that line of code into Widget, then every instance of a subclass of Widget appears to get the same static final WidgetID for their Subclassname.WIDGETID. However, making it non-static means I can no longer even call Subclassname.WIDGETID. So: how do I create a static WidgetID in the parent Widget class while ensuring it is different for every instance of Widget and subclasses of Widget? Or am I using the wrong tool for the job here? Thanks!

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11  | Next Page >