This question already has answers here:
Why can I create an variable with type of parent class
(2 answers)
What does it mean to "program to an interface"?
(33 answers)
Closed 12 months ago.
I am currently learning Java as a past time, and I am troubling in absorbing this instantiation syntax.
I know that if a Main class is created, I can create an object by using this
Main obj = new Main();
But in further lectures (specifically inheritance and polymorphism), they are creating objects as
Parentclass object = new Subclass();
I've tried using
Subclass object = new Subclass();
and have the same output.
Can you please tell me the difference about these two?
Related
This question already has answers here:
what is the Class object (java.lang.Class)?
(7 answers)
Closed 2 years ago.
Recently I was learning annotations in java and came across Reflection API and a part of its code was:
Meta obj = new Meta();
//Get a Class object
CLass<?> c = obj.getClass();
The thing that I am not able to understand is that what is the difference between object obj and object c since both of them are just class objects.
You could treat Class object as metadata of normal object. Moreover, when you get obj.getClass() e.g. for different Integer objects, you'll get the smae Class object.
This question already has an answer here:
New object instantiation when using Java 8 streams
(1 answer)
Closed 2 years ago.
I came across a piece of code as follows:
final static Map<String, Supplier<Shape>> map = new HashMap<>();
map.put("CIRCLE", Circle::new);
Where Circle is a class. I guess here new is used to access the constructor of the class Circle. How can new be used like this? What is this technique called? I could not find any documentation.
This a reference to a constructor. You can pass that where a matching functional interface is expected. Look here for more info http://tutorials.jenkov.com/java/lambda-expressions.html#constructor-references
This question already has answers here:
What does "qualified this" construct mean in java?
(2 answers)
What is a class literal in Java?
(10 answers)
Closed 3 years ago.
I know what "this" means, but what does it mean when you put it behind the name of a class, or if you put ".class" behind the name of a class as in the code below?
Someone please explain
Intent intent = new Intent(ActivityA.this, ActivityB.class);
ActivityA.this refers to this of the enclosing class (ActivityA).
ActivityB.class simply refers to the ActivityB Class reference.
It means this. It's useful for Lambdas, nested classes, etc..
This question already has answers here:
super() in Java
(18 answers)
Closed 7 years ago.
public TableViewerTest() {
super(null);
model = new PlayerTableModel();
}
what does super(null) do here. I am new to java so was not able to figure out.
It calls the constructor of the super class with parameter null.
super(null) call a constructor of the super class of TableViewerTest which accepts a single reference type argument. It passes null to that constructor.
This question already has answers here:
Methods vs Constructors in Java
(11 answers)
Closed 7 years ago.
is setMnemonic(int) a constructor or method?
http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html
It is a method. A constructor will always consist of the name of the class being called on. This is a method, because it is called after an instance of some class, using the dot.