Day 7: Exploring Constructors :

1.Default Constructor:

We were trying to execute the above code! We haven't assigned any value to the variable brand. But the output we received is null. Any guesses who did that? Yes, it's the default constructor.

As we discussed yesterday, These are the Constructors created by Java when no constructors are explicitly declared in the class. We will not be able to see the code externally. It's internally used to Initializes an object with default values like null for objects, 0 for integers, false for Boolean.

Since the brand variable is not explicitly initialized, its default value (null) will be printed.

Default Constructor

2. No Argument Constructor:

These are explicitly defined by the user with no parameter. we can write specific initialization logic when required, even with no parameters. once a constructor is written explicitly it overrides the default constructor.


here the Member variable is assigned a value by constructor we have define so let's check the output executing it.



Output as expected assigned value is printed

3.Parameterized Constructor:

This is a constructor that accepts arguments to initialize an object with specific values. we can set up custom values in the time of object creation. We can create number of constructors with different sets of parameters.


As discussed we are setting up value for brand and speed during object creation as


Output

We will discuss more on constructors tomorrow. Happy Learning.

要查看或添加评论,请登录

Steffi Graph Preethi S的更多文章

  • Arrays

    Arrays

    Been so long since I posted, Happy that i'm able to be back with basic concepts again. Let's see what is Arrays: As we…

  • Day 19: StringBuffer and StringBuilder in Java:

    Day 19: StringBuffer and StringBuilder in Java:

    Mutable String: A String that can be modified or changed is known as mutable String. and are classes in Java used for…

  • Day 18: Strings:

    Day 18: Strings:

    String is a class in the java.lang package and is one of the most commonly used classes.

  • Day 17:

    Day 17:

    Static Keyword: Static is a keyword in Java. The keyword is used to indicate that a particular member (method…

  • Day 16: Jump Statements

    Day 16: Jump Statements

    Jump Statements These statements alter the normal flow of control by transferring execution. 1.

  • Day 16:

    Day 16:

    Looping Statements Looping statement are the execution of the block of code repeatedly until we break it. for Loop…

  • Day 15:

    Day 15:

    Java Control Statements: Java compiler executes the code from Top to Bottom. However Java provides the statement to…

  • Day 14: Encapsulation

    Day 14: Encapsulation

    Day 14: Encapsulation: Encapsulation in Java is integrating the data (fields) and the methods that operate on the data…

  • DAY 13 Polymorphism:

    DAY 13 Polymorphism:

    What is Polymorphism? In simple terms, polymorphism means "many forms," enabling a single method to work in different…

  • Abstract methods and abstract classes

    Abstract methods and abstract classes

    Day 12: What is Abstraction? Hiding the Implementation details. Interface allows 100% Abstraction, whereas Abstraction…

社区洞察

其他会员也浏览了