Computer Applications
Differentiate between constructor and function.
Java Constructors
ICSE 2017
89 Likes
Answer
Constructor | Function |
---|---|
Constructor is a block of code that initializes a newly created object. | Function is a group of statements that can be called at any point in the program using its name to perform a specific task. |
Constructor has the same name as class name. | Function should have a different name than class name. |
Constructor has no return type not even void. | Function requires a valid return type. |
Answered By
52 Likes
Related Questions
Consider the following Java class and answer the questions given below:
class Student { String name; int age; Student(String n, int a) { name = n; age = a; } void display() { System.out.println("Name: " + name + ", Age: " + age); } public static void main(String[] args) { Student s1 = new Student("John", 15); Student s2 = new Student(); s1.display(); s2.display(); } }
(a) Will this program compile successfully? If not, explain the error.
(b) How can this program be modified to work correctly?
Define a class named FruitJuice with the following description:
Data Members Purpose int product_code stores the product code number String flavour stores the flavour of the juice (e.g., orange, apple, etc.) String pack_type stores the type of packaging (e.g., tera-pack, PET bottle, etc.) int pack_size stores package size (e.g., 200 mL, 400 mL, etc.) int product_price stores the price of the product Member Methods Purpose FruitJuice() constructor to initialize integer data members to 0 and string data members to "" void input() to input and store the product code, flavour, pack type, pack size and product price void discount() to reduce the product price by 10 void display() to display the product code, flavour, pack type, pack size and product price Fill in the blanks to design a class:
class __________{
int l, b;
Area(int __________, int __________) {
l = __________;
b = __________;
}
}If the name of the class is "Yellow", what can be the possible name for its constructors?
- yellow
- YELLOW
- Yell
- Yellow