This question already has answers here:
Can I call methods in constructor in Java?
(7 answers)
Closed 7 years ago.
Alright, I think this is a faily simple question, but I just can't wrap my head around this.
Lets say I have this pseudo classes with their respective functionalities. Can I call the methods from within the constructor itself, so it launches on object creation?
Class One
public class Apples{
public String a;
public String b;
Apples(String a, String b){
this.a = a;
this.b = b;
specificMethod();
}
public void randomMethod(){
System.out.println(this.a)
}
public void specificMethod(){
System.out.println(this.b)
}
}
Class Two
public class Oranges{
Apples green = new Apples(a,b)
}
Yes, if you put a method in an object constructor which is called it will run the methods inside the constructor.
Yes.
Many people will even just call an _init function instead of doing everything inside the constructor. That way you can reinitialize an object without creating a new one.
Related
This question already has answers here:
What does the 'static' keyword do in a class?
(22 answers)
Closed 5 years ago.
I've just started learning Java and I am stuck at this MCQ:
Assume class Temp is defined as belowand the statment Temp a= new temp() is
successfully executed Which of the statement is illegal in Java?
class Temp {
public static int i;
public void method1() { }
public static void method2() { }
}
A. System.out.println(i);
B. Temp.method1();
C. a.method1();
D. Temp.method2();
The answer is B, but I can't understand why. Is it because a void method cannot be defined using the dot notation unless it's static?
Because method1 is non-static method. You can use methods with class names only if they are static. Look here for more details.
This question already has answers here:
Does polymorphism apply on class attributes in Java?
(8 answers)
Closed 5 years ago.
Case 1:
In the below example if i call a.i it is printing 10 as answer. But A a = new B(); isn"t this like object of b is getting created so value 20 should be printed instead of 10.
class A
{
int i = 10;
}
class B extends A
{
int i = 20;
}
public class MainClass
{
public static void main(String[] args)
{
A a = new B();
System.out.println(a.i);
}
}
Case 2:
Also if i create the same program as above using methods inside classes instead of printing variable values as in above case then result is different :
class A{
void test(){
System.out.println("hi");
}
}
class B extends A{
void test(){
System.out.println("hello");
}
}
public class Test {
public static void main(String[] args) {
A a=new B();
System.out.println(a.test);
}
Now in this case hi is printed instead of hello , so why is the behavior different when i try to print variable value and when using methods? Overiding happens between the methods only and not with variables?
Because what you have in the first example has nothing to do with polymorphism as fields read are not dynamically dispatched.
What you have however is a name shadowing, so i in the statement A.i refers to the field declared in A and B.i is invisible at this point.
In Java only methods can be overriden, not instance variables.
When you declare a field with the same name as an existing field in a superclass, the new field hides the existing field. The existing field from the superclass is still present in the subclass, and can even be used
Check these -
Java Tutorial - Hiding Fields
JLS Example - Hiding of instance fields
This question already has answers here:
Why can't I do assignment outside a method?
(7 answers)
Closed 6 years ago.
I have created a class called player and beginner (public class Beginner extends Player)
then I tried to access beginner from the main class. It worked.
screenshot down below.
Then I tried to create another object and access that object from the jframe. It allows me to create the object (Beginner b1= new Beginner();)
But does not allow to access that object. It says identifier expected.
Here's the screenshot inside the jframe.
How can I access through the jframe code behind?? What causes this ?
note: I am still a beginner to java. So please do not misunderstand me for asking these type of questions. Thank you ..!
You can't call members like that unless you're doing an assignment.
public class A {
void foo() { ... }
int shoo() { ... }
}
public class b {
A a = new A();
// can't call a.foo() here
int x = a.shoo(); // valid
{
a.foo(); //valid
}
bar () {
a.foo(); // valid
}
}
This question already has answers here:
Overriding member variables in Java ( Variable Hiding)
(13 answers)
Closed 6 years ago.
class A
{
protected int i=10;
}
class B extends A
{
protected int i=15;
}
public class Test extends B
{
public static void main(String a[])
{
A obj=new Test();
System.out.print("i="+obj.i);
}
}
It's output is i=10, but how?
How is the memory allocation for the object will take place.
A obj=new Test();
Means, you are accessing the members of Class A and executing the methods of Test(polymorphism).
I suggest you to read the official docs on inheritance and polymorphism to understand deep.
Polymorphism is linked to objects not references. Since you are using a reference of type A, you will get A.i if you have method getI() in A and override it in B, then call obj.getI(), then you will get B.i's value
In java, Variable Overriding is not there. We have the concept of method Overriding.
In your code
A obj=new Test();
You can able to access members of A. If you have overridden same method in both the classes (Parent and Child). And you are calling that method with obj. This time you will get output from Child class overridden method. this is the concept of Polymorphism.
Line 1: A a = new A();
the method existence will be checked in A class and the method will be called from A class also.
a.g();//A class method will be called
System.out.print("i="+a.i);//A class i will be accessed
Line 2: A b = new B();
the method existence will be checked in A class and the method will be called from B class.
b.g();//B class method will be called and it will also check that it is` available in A also or not if not then compile time error.
System.out.print("i="+b.i);//A class i will be accessed
This question already has answers here:
What does "this" point to?
(5 answers)
Closed 4 years ago.
public class CommandForm extends Form implements CommandListener {
Display d;
public CommandForm(String msg) {
super(msg);
this.addCommand(exit);
}
private void showMessage(String title, String text) {
Alert a = new Alert(title, text, null, AlertType.INFO);
d.setCurrent(a, this);
}
public void prepare_view(Display d){
this.setCommandListener(this);
this.d = d;
}
public void show_view(){
d.setCurrent(this);
}
}
I do not know exactly what the 'this' keyword means in this example. My lecturer says it is the current object, when I inquire further, he said it is the CommandForm. Is that correct? When you pass in 'this' into a parenthesis, e.g setCommandListener(this) are you actually passing the CommandForm? The only way I know how to use 'this' is like this way, this.d = d. So this is kinda new to me.
He's right. If you call setCommandListener(this) you are passing a reference to the current object into the method. When you do this.d = d you are setting the variable d which is part of the class (i.e this) to the incoming value (in parenthesis).
Your lecturer is indeed correct. It's the current object, and this is simply a means to refer to the object currently in scope.
You use the keyword to pass the reference to other objects e.g. object.doSomethingWith(this), and/or resolve ambiguity between members and variables (e.g. this.x = x - there are two different xs here).
Check out the Java Language Specification section on 'this'.
Yes, the this keyword is a reference to that particular instance of the CommandForm class.