How Does Inheritance Work Between Classes?

3

I can not understand something simple, I explain below:

I have the class MainActivity.java (The main one) and then I create another class of name Funcion.java so in the Function class all content (This all empty)

public class Funcion {
}

in the MainActivity class, add the following code:

 Funcion f = new Funcion(); //Creo un objeto de la clase funcion

Everything is fine, but now in the class Function I add a function that asks for a parameter with the name of the class:

public class Funcion {

public  Funcion(String nombre)
{
    String name = nombre;
}
}

Now in the MainActivity class I add the following:

 Funcion f = new Funcion(); 

It gives me an error and it tells me that I must pass a String as a parameter so I must do it like that

 Funcion f = new Funcion("Nombre");

I do not understand the usefulness of doing this action and because it is so used in adapters or databases. Do not know what it is for?

    
asked by Diego 19.05.2018 в 21:44
source

4 answers

3

The first thing is to know that you are doing it is not inheritance, the second is that your constructor in the class Funcion you created to request a String as a parameter, you can create multiple controllers in your class like this:

public class Funcion {

    private String nombre;

    public  Funcion(String nombre) //Constructor con paramentro
    {
        this.nombre = nombre;
    }

    public  Funcion(){ //Constructor sin paramentro
        this.nombre = "";
    }

    public String getNombre(){ //Metodo selector
        return this.nombre;
    }

    public void setNombre(String nombre){ //Metodo modificador
        this.nombre = nombre;
    }

}

In your MainActivity you can create an instance of Funcion in two ways in this case

//Primera forma
Funcion funcion1 = new Funcion("Android");
funcion1.getText(); //Android

//Segunda forma
Funcion funcion2 = new Funcion();
funcion2.setNombre("Android")
funcion2.getNombre(); //Android
    
answered by 19.05.2018 в 21:58
1
public  Funcion(String nombre)

is not a 1 function, it is a constructor .

A constructor is used to create an instance of the class (which is what you do with new Funcion("Hola") .

In the first example

 public class Funcion {
 }

Since you have not defined any constructors, Java adds the implicit constructor , which is a constructor without parameters. That's what you're invoking when doing new Funcion() . When you add your own constructor, Java stops adding the implicit constructor so the code that uses it stops compiling.

1 Java talks more about methods than functions.

    
answered by 19.05.2018 в 21:58
0

most of the constructors of the classes pass parameters to process information, in android it is used more to pass contexts, but you in the class you should create two constructors, the one receives parameters and the other empty, everything depends on what goes to make your class, in the adapters it is used to receive lists and populate a listview or some component.

public class Funcion {
    public  Funcion()
    {
    }

    public  Funcion(String nombre)
    {
      String name = nombre;
    }
}
    
answered by 19.05.2018 в 21:52
0

The usefulness of "doing that" lies in the construction capacity of the classes, that is why it is called a constructor, because it "builds the class" and not only that, when creating an object you can move its content within the code , bringing the information that accompanies it, that's why it is said that "instances", look at the following ...

    public class Contact
{
private String name;
private String address;
public Contact(String name,String address)
{
    this.name = name;
    this.address = address;
}
}

This class could easily represent an entity from some database x, and since opening and closing connections to a database has its cost at a computational level, it may be more optimal to download the data to a structure type such as this, to work on them.

    
answered by 20.05.2018 в 07:01