This is a question out of curiousity.
Today I took a look at the implementations of StringBuilder and StringBuffer. Here's the append() method:
public StringBuilder append(String str) {
super.append(str);
...
|
When do you call super() in Java?
I see it in some constructors of the derived class, but isn't the constructors for each of the parent class called automatically? Why would you ... |
Suppose you have an object of type BlahChild that is an extension of BlahParent, what happens when BlahChild calls super().someMethod and someMethod contains a call to another function, anotherMethod() that is ... |
Is there any way to forbid the son class to call the public method of super class in java?
For example
public abstract class TObject{
abstract public void quark();
}
public class ...
|
|
I am a newbee so advise and help is always greatly appreciated.
Cannot seem to get my container contentPane to display the title.
My code:
class CreateStockCodeDetails extends JFrame implements ActionListener
{
...
|
|
|
|
Can I call the overridden version of an object's foo() method outside that object's class, without using super? EG: class MainProgram { public static void main(String[] args) { Beta b = new Beta(); // I want to invoke Alpha.foo() but on the Beta instance. // I want to cancel the dynamic binding that makes Beta.foo() // get called instead // b.super.foo() ... |
public class ThreadTest { public static void main(String[]args){ PrintThread pt = new PrintThread ( "hello thread" ); pt.start(); } } class PrintThread extends Thread{ private int sleepTime; public PrintThread( String name ){ super( name ); sleepTime = (int) (Math.random() * 50); System.out.println(getName()); } public void run(){ try{ System.out.println(getName()); Thread.sleep( sleepTime ); } catch (InterruptedException e){ System.out.println(e.toString()); } } } |
I think u r confused with super() constractor call and super(as a obj ref of super class.Method().Here in the code aMethod is not a constractor.This is a method which is present in two classes.AsillyClass is the super class.The method is overloaded in AsillierClass.If u call just aMethod() it will call the overloaded method to call the super class(AsillyClass) here super.aMethod() is ... |
could anyone help with this... I'm calling a super class method.Now, i want this method to call other super class methods..instead it calls the subclass method.(all methods in the superclass are overridden in the subclass). the expected ouput is - in C.m1 in P.m2 in P.m1 instead it goes in a loop... in C.m1 in P.m2 in C.m1 in P.m2.... following ... |
No, there is no way to call a method in a grandparent class directly. You can only call methods in the parent class. Note that if you create an instance of the grandparent class (class C in this case), then calling a method on it will have no direct effect on the current instance of class A. This behavior is very ... |
Hi ranchers, In k&b p139 I have been reading that a constructor can never have a call to super() and a call to this() because either needs to be the first statement in a constructor. Also it has been stated that - 'That also means the compiler will not put a call to super() in any constructor that has a call ... |
Hi Folks, 1) Class A is the Super class and B extends from A 2) Class C extends B 3) The method display has been over-ridden in all the classes. 4) From the method in class C, I wish to call that of B & A. 5) I am able to call that of B using super. Question: Is there a ... |
While reading my JSCP book, I starting playing about with enums. I wonder if anyone is able to answer this question. //*****CODE SAMPLE******** class Coffee3 { enum CoffeeSize3 { BIG(2){public int getOunces(){return super.ounces()*3;}}, SMALL(1); CoffeeSize3(int ounces) { this.ounces = ounces; } public int getOunces() { return ounces; } public void setOunces(int ounces) { this.ounces = ounces; } private int ounces; } ... |
We know that if we implement a constructor for a Class, Java do not create the default one. We also know that if a subclass constructor is called, constructor chaining happens and the superclass's constructor is called.= Say we have Class A and Class B. B is the subclass of A. public class A { int size; public A (int mySize) ... |
Welcome to the Ranch, Willy Ray. You need to declare b as type Derived; there is no getFromSuper method in Base. Then your code will work, but you are not calling super from outside the class; it is inside the subclass. Please use the code button below the message window whenever you quote code; it preserves the formatting and makes it ... |
The first line in any constructor is either a call to super(x) or to this(x). The first will call the matching constructor of the super class, with x being zero or more parameter values. The second will call the matching constructor in the same class. If you don't put either of these in your code, the compiler will add a call ... |
The main method is static, and it can only call other static methods and is not allowed to refer to super or even 'this' for that matter, so you'll find you can't even call Dog's doStuff without an instance. You could call that super.doStuff method from the child's doStuff method. You could also create some other a non-static method that makes ... |
|
Piece of code: class A { public void go() { System.out.println("A"); } } class B extends A { public void go() { System.out.println("B"); } } class C extends B { public void go() { System.out.println("C"); } public void call() { ... } } public class Program { public static void main(String[] args) { new C().call(); } } Task: fill the call ... |
We are trying to standardize or define coding guidelines for message broker. In that effort, We decided to call super.onDelete() while overriding onDelete() in Java compute node in Message broker. While doing so, I feel like calling super.onDelete() should be the first statement before defining any cleanup work in onDelete(). But my colleague suggested to call super method at the end ... |
Hi, all: I am killing some time reviewing simple java examples and expanding on thems. I am have trouble with the follow example: public class Ball { private double weight; public Ball(double weight) { this.weight = weight; } } public class BaseBall extends Ball { public BaseBall() { super(5.125); // TODO Auto-generated constructor stub } } public class SportsEquipment { /** ... |
|
birthDate should be a Date. I'd create a constructor that takes a Date object. As for the getters, you just want a getBirthDate, which returns the Date. You don't want to be worrying about display stuff in here. Depending on how you are creating these objects will determine how you get that Date (or how you display it), but that should ... |
Okay so, no matter what the results of this coin toss, The user's money never goes up. What i want is if the results are HHH your money should be 3 * The money specifed for this toss. So: Example: How many coin tosses would you like to make 1-50? 20 20 Tosses will be made --------------------------------- How much money is ... |
28. Super call forums.oracle.com |
When a cast is effectuated, the content of the memory is not changed, nor the address I agree that C++ is irrelevant to this posting but this statement is incorrect. The address is indeed changed, to point to the SuperTest part of the Test object, but the virtual function table that this Test object points to is still a SuperTest vft, ... |
|
Class constructor can call super or this in it. Both are INVOKESPECIAL. Is there any way i can know if instrcution is calling super class method or any other method. Class B extends A class B(int i) { super(i); } or class B(int i) { this(i,null); } class B(int i,String desc) { super(i); } I tried getting class name, but it ... |
Problem: I referece an object in a subclass method which overwrites a superclass method. The object is initialized in the constructor of the subclass. However, I need to initialize the superclass first by calling super(). The superconstructor calls the overwritten method leading to an NPE in the overwritten method because the object has not been initialized yet. Are there patterns that ... |
This is common when creating your own exception. Normally, when you want a specific type of exception to a certain use case. Because, typically the exception should give you an idea what went wrong. Now for the BasicException, maybe this should be the exception to be used in your application instead for using the more general Exception class. Another point to ... |
|
AR Edited by: pbrockway2 on Jan 7, 2009 1:00 PM wrt reply #4 Previous postings by this person - trollishly confusing the fact that the language does not conform to his wishes with the assertion that it's broken or sux - end up many posts later with "My previous discussion abot the this issue has been removed. I must reconstruct it ... |
|
|
|
From the Javadoc for Object.equals(): "The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true)." So you can simply use the == operator ... |
Hello user, I got some problem regarding the use of super(). I came to see one java class, which is implementing one primitive java interface. From another calss, i came to see that the former one was instantiated. So i went to see the default constructor of the first class. There only "super()" was written. How come a class which is ... |
I don't know if I'm doing something wrong, or if something somehow corrupted my compiler, but it seems to be refusing to allow me to call superclass methods. Attempts to call superclass methods directly generally bring up "cannot find symbols"s, calling them through 'super' generally brings up the same on "variable super," and attempts to invoke them through reflection brings up ... |
You have 3 classes A, B and C. C extends B, B extends A and they all implement a public non-static method fred() with signature public int fred(); You have created an instance of C. In a method in C you want to call A's version of fred(). In C++ it would be just A::fred(). But what's the syntax for java? ... |