Posts

Showing posts with the label constructor

Constructor Overloading (java)

 CONSTRUCTOR OVERLOADING (java) example of a class that has both a default constructor and a parameterized constructor in Java: Source code:   public class Person { private String name; private int age; // Default constructor public Person () { name = "Unknown" ; age = 0 ; } // Parameterized constructor public Person ( String name, int age) { this .name = name; this .age = age; } // Getter methods public String getName () { return name; } public int getAge () { return age; } // Setter methods public void setName ( String name) { this .name = name; } public void setAge ( int age) { this .age = age; } } In this example, we have a class called Person with two constructors - a default constructor and a parameterized constructor. The default constructor sets the name field t...