Using Extends in Java

3

If in a project I want to call a method with another method inside, is it necessary to use extends ? I have done some exercise with extends and this but I do not understand the difference well, in this case that I explain, could I use extends instead of what you see?

public class EjemploThis {
public static void main (String[] args){
    String name = "Rex";
    String type = "Chihuahua";
    int age = 10;
    int energy = 100;
    bark();}

static void bark(){
    System.out.println("Woff");  } 

void atack() {} //Método atacar     
void run(){} //Método correr

void defendHouse() { //Método defender la casa
    this.bark();
    this.atack();} }
    
asked by Pablo León 01.04.2016 в 19:56
source

2 answers

4
  

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 .

    
answered by 01.04.2016 / 22:58
source
0

The reserved word this refers to the same object

Class A{protected int a; public int getA(){return this.a;}}

For this case this is used to reference characteristics of the object, methods and attributes

Class B extends A{ int b; public int getB(){return this.b;}  public int getC(){return super.a +this.b ;}}

The reserved word super refers to its parent

To learn more about the topic link

    
answered by 01.04.2016 в 21:17