Getting to variable by using an inteface [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I've got an object that is interface, let's call it MyInterface something.
My interface is empty and is being implemented by two classes.
First on has one variable int x, and the second one two: int x, int y.
Variables are private but they've got "get" methods and i know them.
Is it possible to get to the variable of a class just by using "something"?

No, it's not possible (excluding reflection magic), unless you add a getX() method to MyInterface. Then, it's easy and it has the nice side-benefit of being a correct design.

Related

polymorpishm in java abstract class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
We cannot create objects from abstract classes in Java. But there is a part that I do not understand. I have an abstract class called "GameCharacter" in my project. A resource told me to write the code in my assignment like this, but I don't understand, did we not use an object in parentheses(GameCharacter gamechar) Didn't we use an object called gameChar in ?
public void setPlayer(GameCharacter gamechar){
}
This is where inheritance comes into play: gamechar can be any instance of GameCharacter, or any class that extends it. You may not be able to directly create a GameCharacter instance, but this method's logic should work with any instance of any concrete class that extends it.

Which functional interface to use if method don't accept anything but return object of the class in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I have one existing Java 7 method which accepts nothing but return class instance. I want to change it in java 8 using any existing functional interface but don't know what i can use here.
public NotificationPage infraSelection() {
......
return this;
}
Here NotificationPage is class name.
Maybe you can use the Supplier<T> interface?
It declares the method T get().

When to use public modifier for a class field in OOP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 12 months ago.
Improve this question
When programming in OO languages like C# or Java, is there a good situation where declaring a public field inside a class is actually valid (I myself always use a property for not making the user of the class depend on the data and to support data protection)?
Otherwise, it feels weird that C# for example allows you to do so.
According to the C# coding conventions public field should be used sparingly:
// A public field, these should be used sparingly
public bool IsValid;
Why? I think because of:
can be edited by any other user of class
if you want to add some logic to field, then you need to create property instead of field. By doing this, you will break a contract of class
it is not possible to override variable
However, there is a case when you need to have field as #VGR said:
public const string foo = "";

How can multiple classes call one method that takes an instance of the calling class as a parameter? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want all my other objects to be able to call one common method. What should my shared method type signature for parameters look like?
public class appSocket{
public appSocket appSocket(TYPE genericInstance){
// Do something with genericInstance here
}
}
In other words, what should "TYPE" look like?
I think you're a bit confused. This just corresponds to the current object. There is no reason to ever say someObject.this.foo(). You'd just say someObject.foo(). You just pass in the object.

Must write a java class constructor [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have been asked in a interview. It seems there is a scenario when, a constructor must need to write while writing a java class. I could not find a proper answer though. Please help me.
Cheers
You must write a constructor when your base class does not have a standard one, which takes no argument.
For example:
class A {
public A(int value) {
}
}
class B extends A {
}
This code does not compile. Java will try to add a default constructor to B, but it could not call super(), since A does not have a zero-argument constructor. Thus, you must define a constructor for B.
If you extend a class that doesn't have a parameterless constructor you must define a constructor.

Categories

Resources