This question already has answers here:
A superclass method is called instead of the subclass method
(3 answers)
Java inheritance (method overriding and overloading)
(4 answers)
Inheritance and Overloading methods with different argument data types in Java
(2 answers)
Closed 4 years ago.
So in class, we are going over-loading and over-ridding though for some reason my professor could not explain why when we have two classes (a and b; as well as b extends to a) when calling a method it prefers the method in the parent class even though the parameters meet those of the child class. Yet java uses the parent one.
class B extends A {
public int m(int x) {
return 20;
}
}
class A {
public int m(double x) {
return 10;
}
}
public class Tester {
public static void main(String[] args) {
A x = new B();
System.out.println(x.m(5));
}
}
Output: 10
This question already has an answer here:
Why won't my public void Constructor {} compile?
(1 answer)
Closed 5 years ago.
i have one class with an arrayList, and i create a method (getList) that will return its value.
ArrayList<String> data = new ArrayList<>();
public void Tes2(){
data.add("aku");
data.add("sudah");
data.add("mandi");
}
public ArrayList getList(){
return data;
}
then i have another class that will call the arrayList from the previous class.
public static void main(String[] args) {
Tes2 ambil = new Tes2();
ambil.getList();
System.out.println(ambil.getList()+" ");
}
but when i compile the code, there's nothing is printed. i don't know what is wrong, it's like my arraylist is empty. how can i do it right?
Either remove the void in the constructor or change it to some other name (eg Loadlist) so that it becomes a method. Then from your main class you can call ambit.Loadlist() to load the list and then you can write ambil.getList ()
This question already has answers here:
"Program to an interface". What does it mean? [duplicate]
(8 answers)
Closed 6 years ago.
I am now studying JAVA.
When we make interface like this, sometimes we create object like this
Sample code :-
interface test01{
public boolean A(int i);
public int B(int a);
public String C (String c);
}
class ttt implements test01{
public boolean A(int a){
return true;
}
public int B(int a){
return a;
}
public String C(String c){
return c;
}
}
public class Interface_test {
public static void main(String[] args) {
ttt t1 = new ttt();
System.out.println(t1.A(1));
System.out.println(t1.B(1));
System.out.println(t1.C("C"));
test01 tt = new ttt();
System.out.println(tt.A(1));
System.out.println(tt.B(1));
System.out.println(tt.C("C"));
}
}
This code's result is same
but I was wondering why we use the pattern like this
"test01 tt = new ttt();"
instead of
"ttt t1 = new ttt();"
Please let me know...
Thank you!
Interfaces are used as types to allow implementations to be swapped in the future without having to worry about changing method signatures or implementations in other classes that are using your interface.
If you didn't use an interface, you had to revisit all classes using your old implementation and change it to use the new implementation.
Interfaces are also used as an abstraction to hide logic of the implementing class from users of the interface.
This question already has answers here:
Why doesn't the compiler complain when I try to override a static method?
(9 answers)
Closed 6 years ago.
I'm new at learning Java can anyone explain me the execution flow of the following code? I'm quite confused with the output.
This is the code:
public class MainClass {
public static void main(String[] args) {
car c = new car();
vehicle v = c;
/* I am unable to understand what's happening while printing the values using the objects of the different classes*/
System.out.println("|" + v.getModelName()
+ "|" + c.getModelName()
+ "|" + v.getRegNo() + "|" + c.getRegNo() + "|");
}
}
class vehicle {
public static String getModelName() {
return "Volvo";
}
public long getRegNo() {
return 12345;
}
}
class car extends vehicle {
public static String getModelName() {
return "Toyota";
}
#Override
public long getRegNo() {
return 54321;
}
}
Object creation
You are creating car instance ( new car())
Add new object pointer to variable c
Copy content of variable c to variable vehicle ( which point to car object)
Method call flow
When you are call static function on object it will not apply inheritance rules, so in call to v.getModelName() Java Virtual Machine call method in class vehicle.
But when you are call car() object with vehicle pointer (v variable) getRegNo method of class vehicle will call and also when you are using car pointer (c variable) getRegNo method of class vehicle will call.
edite suggestion form comment:
This ability called "Polymorphism": here you can find good tutorial. "Polymorphism" is definitely as important a concept as "inheritance" and "encapsulation'.
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 8 years ago.
Improve this question
Within my main method I'm trying to understand how to call up a variable from a different class.
I've attempted to break it down to the most simple solution possible just so I can get my head around the logic involved.
I have two classes within my package "var":
Class 1 - Source.java
package var;
public class Source {
int source1;
class setSource{
int source1 = 5;
}
}
Class 2 - Var.java
package var;
public class Var {
public static void main(String[] args) {
int Var;
Var = Source.setSource();
}
}
First time post here but I've spent 4 days and almost all my spare time trying to figure this out, please be gentle I'm dedicated but extremely newbie right now. Thanks in advance, I hope I've submitted this correctly.
Okay, I can sort of see what you were thinking but you've got some of the semantics incorrect. What you want to define is a method. A method takes the following structure:
<access modifier> <return type> <method name> (<method arguments>)
So for example
public void doSomething(String value) {
// This is the public method that returns nothing. It is called doSomething
// It expects a string value that it will call "value"
}
In your case, you want to create one of these, and you want to make a setter and a getter (or accessor and mutator if you're being posh).
Your Setter
This is just a normal method. Its purpose is to set the value of some class field. So let's define our class..
public class MyClass {
private int num;
}
Now we've got a class MyClass with a field num. But oh no, it's private, so let's create a setter so that the user can update the value.. Following our formula for methods, we start with a public access modifier. We then define the return type, which is void because it returns nothing. The name of the method should follow the java naming convention, which is the word "set" followed by the name of the member and finally the value for the setter.. Or all together:
public void setNum(int num) {
this.num = num;
}
This will update the value in the class with the value that you pass in. Excellent!
Your Getter
Well, this is nice and simple. Following our formula, it is a method that is public because everyone can access it; it returns something (in this case int) so that is the return type; the name follows the convention of "get" followed by the name and it expects no parameters.
public int getNum() {
return num;
}
This will return the value of num.
Finally, Using them!
public class MainClass {
public static void main(String[] args) {
MyClass myClass = new MyClass();
// Create a new MyClass instance.
myClass.setNum(4);
// Update the value in the class with the number 4.
System.out.println("The number is " + myClass.getNum());
// Outputs: "The number is 4"
}
}
you are using static calls, so you have to set this variables public static :
public static int source1;
and access them directly:
Var = Source.source1;
Your concepts are not well polished.
Your classes should have been like this
public class Source {
private int source;
public void setSource(int src){ // Called setter
source = src;
}
public int getSource(){ // Called getter
return source;
}
}
And
public class Var {
public static void main(String[] args) {
int Var;
Source source = new Source();
source.setSource(10);
Var = source.getSource(); // Var has value 10 in it.
}
}
Source is a class, you'll need to create an object that's a member of this class and then call the method on it. Additionally, the syntax for your method call is incorrect.
package var;
public class Source {
int source1 = 1;
public void setSource(){
source1 = 5;
}
}
Then:
package var;
public class Var {
public static void main(String[] args) {
Source source = new Source();
System.out.println(source.source1);
source.setSource();
System.out.println(source.source1);
}
}
I hope that makes sense to you when you compile it and run.
(Note that Java is case sensitive. On the above example, Source is the class and source is the object).
An alternative would be to declare methods and fields as static (static methods are called directly on the class), but I would suggest you make sure you understand the basic concepts of class and object instantiation before moving on to that.