• Unit11: OOP AND JAVA

    Unit11: OOP AND JAVA

    Key Unit Competency: To be able to use objects to manipulate data in Java program

    Introductory Activity


    Observe the above picture and answer the following questions.

                  1. Briefly, discuss the object oriented programming paradigm.

                   2. Find defined classes.

                   3. What are the data members for each class?

                   4. What are the member functions for each class?

                   5. What do you understand by instance of a class?

                   6. Create 2 instances of student class

                   7. Using Java, declare Student class in Java?

    11.1. Classes vs Objects in Java

    Learning Activity 11.1

    Write the above program in your computer and run it. After analyzing the result, answer the following questions:

                  1. Give the output of the program?

                  2. What are new keywords in the above program?

                  3. Describe the structure of the part starting with the word class.

                  4. List the data members contained in the class “student”

                  5. What are the functions found in the above program?

                  6. What is the role played by the keyword new in the above program?

    11.1.1 Class in Java

               a. Definition

    A class is an entity that determines how an object will behave and what the object will contain. In other words, it is a blueprint or a set of instruction to build a specific type of object or a class is a group of objects which have common properties.

                b.  Syntax to declare a class:


                 c.  General form of a class

    A class is declared by the use of the class keyword. A simplified general form of a class definition is shown here:


    11.1.2. Object in Java

    An object is nothing but a self-contained component which consists of methods and properties to make a particular type of data useful. Object determines the behavior of the class. From a programming point of view, an object can be a data structure, a variable or a function. It has a memory location allocated. The object is designed as class hierarchies.

               a.  Syntax of an object

    ClassName ReferenceVariable = new ClassName();

               b.  Reference variable

    An object can be accessed only through a reference variable. A reference variable is declared to be of a specific type and that type can never be changed.

    Reference variables can be declared as static variables, instance variables, method parameters, or local variables. A reference variable that is declared as final can’t never be reassigned to refer to a different object. The data within the object can be modified, but the reference variable cannot be changed.

    • Object initialization through reference Variable:

    Initializing simply means storing data into object Example 1: Below is the example where class student is created, new keyword and the reference variable are used


    Example:  We can also create multiple objects and store information in it through reference variable


    • Object initialization through method in Java Language

    Two objects of Student class are going to be created and initialized with the value by invoking the insertRecord method. The state (data) of the objects are displayed by invoking the displayInformation() method.

    Example: The use of method in Java program


    Example: Example that maintains the records of Rectangle class.


    Application Activity 11.1

          1. What do you understand by object instantiation?

          2. What is the difference between a class and an object?

          3. What is the role of a method in java program?

          4. Discuss why main () method is public, static and void in java?

          5. What is meant by the terms instance variable and instance method?

    11. 2 Constructor in Java

     Learning activity 11.2

    Write the above program in your computer and run it. After analyzing the result, answer the following questions:

                    1. What is the output of the above program?

                    2. Discuss the difference between class district and the function district ()?

                    3. Why the function district () does not have a return type?

    11.2.1. Understanding constructor in Java

                        a.  Definition

    A constructor is a special method of a class that initializes an object of that type. A constructor is an instance method that usually has the same name as the class and can be used to set the values of the members of an object, either to default or to user-defined values

    A Constructor is a special type of method which is used to initialize the object. It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if the class doesn’t have any.

    Every time an object is created using new () keyword, at least one constructor is called. It is called a default constructor.

     There are two (2) ways of creating a constructor: explicit and implicit: • Explicit means that the constructor is created by the programmer. • Implicit means that the creation of a constructor was done by the Java Virtual Machine or the tool, not the Programmer.
    Java will provide default constructor implicitly even if the programmer doesn’t write the code for constructor. It is called default constructor. The explicit is opposite to this implicit which means that the programmer has to write a constructor. Example: The program below maintains records of employees.