Constructors in Java

00:06:17
https://www.youtube.com/watch?v=HbQ2ASDc0Lw

Ringkasan

TLDRAquest vídeo explica el concepte de constructors en programació, centrant-se en com funcionen en classes i com es poden utilitzar per instanciar i inicialitzar objectes. S'explica que un constructor ha de tenir el mateix nom que la classe i no pot tenir cap tipus de retorn. El vídeo també aborda els constructors per defecte, que no prenen arguments ni realitzen cap acció, i la sobrecàrrega de constructors, mostrant com es poden tenir diversos constructors en una mateixa classe per proporcionar diferents opcions d'inicialització. S'ofereixen exemples amb una classe de cercle per demostrar com es poden aplicar aquests conceptes pràctics en un codi real.

Takeaways

  • 🔹 Els constructors tenen el mateix nom que la classe.
  • 🔸 No tenen cap tipus de retorn.
  • 🛠️ S'utilitzen per inicialitzar objectes.
  • 🔍 Un constructor per defecte no té paràmetres.
  • ↔️ La sobrecàrrega permet múltiples constructors.
  • ⚙️ Exemples pràctics amb classe de cercle.
  • 💡 Important per a la inicialització d'atributs.
  • 🔄 Java crea automàticament el constructor per defecte si no se'n defineix un.
  • 📜 Es pot sobrecarregar constructors per oferir múltiples inicialitzacions.
  • 🔑 Els constructors per defecte poden ser definits per proporcionar valors predeterminats.

Garis waktu

  • 00:00:00 - 00:06:17

    En aquesta lliçó, s'explica el concepte de constructors en programació, començant per una introducció general i abordant diferents tipus de constructors com el constructor per defecte i la sobrecàrrega de constructors. Un constructor és un mètode utilitzat per crear i inicialitzar objectes i ha de tenir el mateix nom que la classe. A més, els constructors no tenen un tipus de retorn i es criden utilitzant l'operador new per donar valors inicials als atributs d'un objecte. Es defineix un constructor per defecte com un constructor sense paràmetres ni cos que simplement s'utilitza per crear un objecte a partir de la classe.

Peta Pikiran

Mind Map

Pertanyaan yang Sering Diajukan

  • Què és un constructor?

    Un constructor és un mètode que s'utilitza per instanciar i inicialitzar objectes en una classe.

  • Quin nom ha de tenir un constructor?

    Un constructor ha de tenir el mateix nom que la classe en què es troba.

  • Es pot crear més d'un constructor per una classe?

    Sí, es poden crear múltiples constructors per una classe, conegut com la sobrecàrrega de constructors.

  • Què és un constructor per defecte?

    Un constructor per defecte és un constructor sense arguments amb un cos buit.

  • Quina diferència hi ha entre un constructor per defecte i un personalitzat?

    Un constructor per defecte no té paràmetres ni contingut, mentre que un constructor personalitzat pot tenir paràmetres i atribucions inicials.

Lihat lebih banyak ringkasan video

Dapatkan akses instan ke ringkasan video YouTube gratis yang didukung oleh AI!
Teks
en
Gulir Otomatis:
  • 00:00:06
    hello friends and welcome back in this
  • 00:00:07
    lecture we will talk about constructors
  • 00:00:09
    and here is our outline
  • 00:00:11
    we will talk in general about
  • 00:00:12
    constructors we will talk about the
  • 00:00:14
    default constructor
  • 00:00:15
    and finally we will talk about
  • 00:00:16
    overloading constructors
  • 00:00:18
    let's get started as you know a
  • 00:00:20
    constructor is a method that is used to
  • 00:00:22
    instantiate and
  • 00:00:23
    initialize objects now let's talk about
  • 00:00:26
    constructors in more details
  • 00:00:28
    first of all a constructor must have the
  • 00:00:30
    same name as the class
  • 00:00:32
    for example the constructor of the
  • 00:00:34
    string class is called string and the
  • 00:00:36
    constructor of the point class is called
  • 00:00:38
    point
  • 00:00:39
    also the constructor of the circle class
  • 00:00:41
    that we created
  • 00:00:42
    is called circle okay moreover
  • 00:00:45
    constructors don't have a return type
  • 00:00:48
    also constructors are invoked or called
  • 00:00:51
    using the new operator and as we said
  • 00:00:53
    before we should use constructors to
  • 00:00:55
    initialize
  • 00:00:56
    objects so we use constructors to give
  • 00:00:59
    initial values to the attributes of our
  • 00:01:01
    object
  • 00:01:02
    finally a default constructor is a zero
  • 00:01:04
    argument constructor with an empty body
  • 00:01:07
    so it is a constructor that doesn't take
  • 00:01:09
    any parameters and doesn't do anything
  • 00:01:11
    we simply use it to create an object
  • 00:01:13
    from the class
  • 00:01:14
    now let's consider the same class as
  • 00:01:16
    before which represents a circle
  • 00:01:18
    it has the following attributes and the
  • 00:01:20
    following methods
  • 00:01:21
    now we want to add this constructor it
  • 00:01:24
    is called circle because the name of the
  • 00:01:25
    class is circle
  • 00:01:27
    this constructor is going to take two
  • 00:01:28
    parameters the first parameter is a
  • 00:01:30
    point
  • 00:01:31
    and we are going to call it initial
  • 00:01:32
    center and the second parameter is a
  • 00:01:34
    double
  • 00:01:35
    and we will call it initial radius so
  • 00:01:37
    now when we create circle objects we can
  • 00:01:39
    give the constructor two arguments
  • 00:01:42
    a point and a double and we will
  • 00:01:43
    initialize the values of our attributes
  • 00:01:46
    with these values that are passed over
  • 00:01:47
    here
  • 00:01:48
    so our center attribute will be equal to
  • 00:01:50
    the initial center and our radius
  • 00:01:52
    attribute will be equal to the initial
  • 00:01:54
    radius so let's create this constructor
  • 00:01:56
    first of all i'm in the circle class and
  • 00:01:58
    we have these two attributes
  • 00:02:00
    and now we have a method that is called
  • 00:02:02
    circle and it doesn't have a return type
  • 00:02:04
    this method takes a point initial center
  • 00:02:07
    and a double initial radius
  • 00:02:08
    and inside this method we are assigning
  • 00:02:10
    the center attribute to be equal to the
  • 00:02:12
    initial center
  • 00:02:13
    and the radius to be equal to the
  • 00:02:14
    initial radius so this is our
  • 00:02:16
    constructor and you are using it to
  • 00:02:18
    initialize the values of the attributes
  • 00:02:20
    as you can see
  • 00:02:21
    so now let's use this constructor to
  • 00:02:23
    create circle objects
  • 00:02:25
    have a look at this code inside the main
  • 00:02:27
    method i'm creating a circle c1 which is
  • 00:02:29
    equal to a new circle
  • 00:02:31
    and over here i'm passing two arguments
  • 00:02:33
    so i'm calling the constructor that is
  • 00:02:35
    called circle and takes two arguments
  • 00:02:38
    the first argument is a point with x
  • 00:02:40
    equal one and y equal to and the second
  • 00:02:42
    argument is an integer
  • 00:02:43
    of course this value will be casted to a
  • 00:02:46
    double because the second argument is a
  • 00:02:48
    double
  • 00:02:48
    okay so now i'm printing the center of
  • 00:02:50
    the circle c1 and the radius and this
  • 00:02:52
    will be the output
  • 00:02:54
    so as you can see we use constructors to
  • 00:02:56
    initialize the attributes of our object
  • 00:02:58
    in other words we use it to initialize
  • 00:03:00
    our object now let's
  • 00:03:01
    try to use the default constructor over
  • 00:03:04
    here i'm creating a circle c1 which is
  • 00:03:06
    equal to a new circle
  • 00:03:07
    have a look over here we have an error
  • 00:03:09
    the error says expected two arguments
  • 00:03:12
    but found
  • 00:03:12
    zero so our circle class doesn't have a
  • 00:03:15
    default constructor anymore
  • 00:03:17
    it only has the constructor that takes
  • 00:03:19
    two arguments so have a look over here
  • 00:03:21
    when no constructors are created a
  • 00:03:23
    default constructor is created
  • 00:03:25
    automatically by java
  • 00:03:26
    but when we create a constructor the
  • 00:03:28
    default constructor is not created
  • 00:03:30
    in other words if you want a zero
  • 00:03:32
    argument constructor you have to create
  • 00:03:34
    it
  • 00:03:35
    so have a look over here now we are
  • 00:03:37
    creating two constructors
  • 00:03:38
    so we are overloading the constructor
  • 00:03:41
    the first constructor doesn't take any
  • 00:03:42
    parameters
  • 00:03:43
    and it does nothing it has an empty body
  • 00:03:46
    and the second constructor is the same
  • 00:03:47
    as before
  • 00:03:48
    so now we have two ways to create circle
  • 00:03:50
    objects the first one is using this
  • 00:03:52
    constructor
  • 00:03:53
    and in this case the value of the center
  • 00:03:55
    and the radius will be default values
  • 00:03:57
    the center will be null and the radius
  • 00:03:59
    will be zero and the second way is using
  • 00:04:01
    this constructor
  • 00:04:02
    and in this case the values of the
  • 00:04:04
    center and the radius will be given by
  • 00:04:06
    the user
  • 00:04:06
    now let's modify this constructor over
  • 00:04:08
    here let's suppose that when we use this
  • 00:04:10
    constructor we want to give default
  • 00:04:12
    values for the center and the radius
  • 00:04:14
    other than null and zero so have a look
  • 00:04:16
    over here
  • 00:04:17
    now inside this constructor we are
  • 00:04:19
    assigning the center to be equal to a
  • 00:04:21
    new point with x equals zero and y
  • 00:04:23
    equals zero
  • 00:04:24
    also we are assigning the radius to be
  • 00:04:26
    equal to one so now if you use this
  • 00:04:28
    constructor to create a circle object
  • 00:04:30
    the center will be equal to this point
  • 00:04:31
    and the radius will be equal to 1.
  • 00:04:33
    so if the user doesn't want to give a
  • 00:04:35
    value for the center and the radius he
  • 00:04:37
    can use this constructor
  • 00:04:38
    and you will give default values as you
  • 00:04:40
    can see so
  • 00:04:42
    now let's create circle objects using
  • 00:04:44
    both of these constructors have a look
  • 00:04:46
    over here
  • 00:04:46
    in our main method i'm going to create a
  • 00:04:48
    circle c1
  • 00:04:50
    using this constructor after that i'm
  • 00:04:52
    printing the center and the radius and
  • 00:04:53
    this is the output
  • 00:04:54
    as you can see the center is a point
  • 00:04:56
    with x equals 0 and y equals 0 and the
  • 00:04:58
    radius is equal to 1.0
  • 00:05:00
    so java automatically knows what
  • 00:05:02
    constructor to use based on the number
  • 00:05:04
    of parameters
  • 00:05:05
    now let's use the other constructor we
  • 00:05:07
    are creating a circle c2 and we are
  • 00:05:09
    passing values
  • 00:05:10
    as arguments after that i'm printing the
  • 00:05:13
    center and the radius and this will be
  • 00:05:14
    the output
  • 00:05:15
    now in this case we only have two
  • 00:05:17
    constructors and of course you can
  • 00:05:19
    create as many constructors as you want
  • 00:05:21
    we are simply overloading a method which
  • 00:05:23
    is the constructor
  • 00:05:24
    okay now i want to show you another
  • 00:05:26
    example of using two constructors
  • 00:05:28
    have a look over here i'm creating a
  • 00:05:31
    point p1
  • 00:05:32
    using this constructor it doesn't take
  • 00:05:34
    any parameters
  • 00:05:35
    and after that i'm printing p1 as you
  • 00:05:37
    will see over here
  • 00:05:38
    x will be equal to 0 and y will be equal
  • 00:05:40
    to 0. so inside the point class there is
  • 00:05:43
    a constructor that doesn't take any
  • 00:05:45
    parameters and you are able to use this
  • 00:05:46
    constructor over here
  • 00:05:48
    also we have another constructor this
  • 00:05:50
    constructor takes the value of x
  • 00:05:52
    and the value of y so over here i'm
  • 00:05:54
    creating a point p2
  • 00:05:56
    and after that i'm printing p2 and this
  • 00:05:58
    will be the output
  • 00:05:59
    so this is similar to what we have done
  • 00:06:01
    with the circle class okay
  • 00:06:02
    so this is it thanks for watching and
  • 00:06:04
    i'll see you in the next video
  • 00:06:06
    [Applause]
  • 00:06:08
    [Music]
Tags
  • constructors
  • sócdesenvolupament
  • classe
  • objectes
  • sobrecàrrega
  • inicialització
  • programació