4. Constructors

Lesson 1

Class as Data Type and Class as Application

Definition

Classes can be defined in two ways: class as data type and class as application.
Class as data type: When a class is defined with member variables and member methods then it is known as class as data type. The variables of this class are objects. The class as data type cannot execute itself.
Class as application: When a class is defined with static main() method then it is class as application. This class and the main() method executes first. When an object is created and member method is called then the class as data type is executed. (Class as application applies the class as data type.)

A Program Using Class as Data Type and Class as Application

This program is to understand the concepts, not to practice as program

Two classes are defined, 1) class Rectangle as data type and 2) public class Shapes as application.
The class Rectangle consists data members l, b and member method findArea() that inputs, compute and display the values.
The class Shapes creates two objects rect1 and rect2.
Using these objects findArea() is called individually to find area of the objects.

Read Description on right side

Description

Combination of Class as Data Type and Class as Application

Non Constructor: Single Member Method

This lesson discusses exercises without constructor. From the third lesson onwards constructors are discussed.

  1. Define a class Rectangle. Data members: l and b. Member method: findArea() – to input length and breadth, calculate area and print it. Also define main() method to create two objects and implement the member method.

The identifiers - class name, variable names and method names, object names - should be written as given in the question

Input and Output

Description

Do Yourself

  1. Define a class Student with attributes (data member) roll, name, m1 and m2 (two marks). Member method: findAverage() – to input values, to find average and to display all details. Also define main() method to create two objects and implement the member method.

Input and Output

Description