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 given program and answer the questions given below:
class temp { int a; temp() { a=10; } temp(int z) { a=z; } void print() { System.out.println(a); } void main() { temp t = new temp(); temp x = new temp(30); t.print(); x.print(); } }
(a) What concept of OOPs is depicted in the above program with two constructors?
(b) What is the output of the method main()?
Why do we need a constructor as a class member?
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 = __________;
}
}