If in a project I want to call a method with another method inside,
Is it necessary to use extends?
No.
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
imprimir();
}
static void imprimir(){
System.out.println("Hola soy imprimir y me llamaron desde main");
imprimir1();
}
static void imprimir1(){
System.out.println("Hola soy imprimir1 y me llamaron desde imprimir");
}
link
Is it necessary to use extends?
The use of extends
is closely related to the inheritance, and the polymorphism ("although they usually apply inheritance without polymorphism but enter that would be a bit based on opinions when they could use composition"), extends
basically described Quick way for you to have an idea what it does is that it inherits all the content of a class within the class to which it is applied extends ("well really everything will not depend on other factors private
, protect
ect I leave some videos that still help you to understand better ").
link
public class Animal {
int patas;
}
public class Perro extends Animal {
}
Now when doing the extends Perro
has the variable patas
, and if with the methods of Animal if it had them, that is to simple features.
Now about this
I will give you an example I hope you clarify
public class Animal {
String nombre;
public void ejemplo(String nombre){ //puedes ver que el
//identificador de la
//variable es el mismo
//que el que tienes a nivel
//de class los dos son nombre
this.nombre = nombre; //en este caso usamos
//this.nombre para referirnos
//a la variable que esta fuera
//del metodo y nombre se a la
//refiere a la que se le pasa al
//al metodo
}
}
There are many uses for the word this
, it can also be used for the call to another constructor of the class see example below taken from here
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 1, 1);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
...
}
It would be something like this illustration more or less equal better understand
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() { <-- si usted hiciera Rectangle rec = new Rectagle();
this(0, 0, 1, 1); -- call -------------------------------
-------> |
| } |
| |
| |
| |
| public Rectangle(int x, int y, int width, int height) { <-----
| | this.x = x;
| | this.y = y;
| | this.width = width;
| | this.height = height;
| } |
--------
}
Within an instance method or a constructor method, this
is a
reference to the current object - the object whose method or constructor
It is being called. It can refer to any member of the object
current from the inside of an instance method or a constructor
by using this
.