Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to use constructor overloading in Java

2 Answers

0 votes
class Car {
    public String manufacturer;
    public String model;
    public float price;
    
    public Car(String _manufacturer, String _model, float _price) { // constructor
        manufacturer = _manufacturer;
        model = _model;
        price = _price;
    }
    
    public Car() { // constructor
        manufacturer = "unknown";
        model = "unknown";
        price = 0;
    }
    
    public void print() {
        System.out.println(manufacturer + " " + model + " " + price);
    }
}
public class JavaApplication {
     
    public static void main(String[] args) {
 
        Car m1 = new Car("Audi", "2016 Audi SQ5", 53000);
        Car m2 = new Car();
         
        m1.print();
        m2.print();
    }
}
 
 
 
/*
run:
 
Audi 2016 Audi SQ5 53000.0
unknown unknown 0.0
 
*/

 



answered Jul 4, 2017 by avibootz
edited May 4, 2024 by avibootz
0 votes
class Worker {
    public int id;
    public String name;
   
    public Worker(int _id, String s) { // constructor
        id = _id;
        name = s;
    }
    public Worker() { // constructor
        id = 74893;
        name = "a name";
    }
    public void Show() {
        System.out.println("ID = " + id + " : " + "Name = " + name);
    }
}
 
public class MyClass {
    public static void main(String args[]) {
        Worker w1 = new Worker(12343, "Dumbledore");
        Worker w2 = new Worker();
         
        w1.Show();
         
        w2.Show();
    }
}
 
 
 
 
/*
run:
 
ID = 12343 : Name = Dumbledore
ID = 74893 : Name = a name
 
*/

 



answered May 4, 2024 by avibootz

Related questions

1 answer 114 views
114 views asked Dec 1, 2022 by avibootz
2 answers 270 views
270 views asked Jul 4, 2017 by avibootz
1 answer 109 views
1 answer 107 views
107 views asked Jul 16, 2022 by avibootz
...