Module 2 Getting To Know Java and Object Oriented Programming
Module 2 Getting To Know Java and Object Oriented Programming
Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com
Lesson Objectives:
At the end of this module, you should be able to:
1. Understand the different concepts relevant to object model.
Reference: https://www.tutorialspoint.com/java/index.htm
Productivity Tip: To commence this academic year, may this quote of George Raymond Richard Martin
inspire and fuel your drive to learn, and be more in your chosen field; despite of the crisis you are all facing.
May you thrive and crave for self-improvement through EDUCATION, amidst of uncertainties.
“I’m a slow learner, it’s true. But I learn.”
A. LESSON PREVIEW/REVIEW
1. Introduction
- Java is an Object-Oriented Language. As a language that has the Object-Oriented feature, Java
supports the following fundamental concepts – Polymorphism, inheritance, encapsulation,
abstraction, classes, objects, instance, method, and message passing.
- Objects- have states and behavior. It is an instance of a class. For example, A dog has states –
color, name, breed, and behaviors – wagging the tail, barking, eating.
- Class – can be defined as a template, blueprint that describes the behavior/state that the
object of its type support.
For today’s lesson try answering the questions below fill out the first column of the table. Write
your idea about the questions being asked in “What I know” part. It’s okay to write what came
out to your mind after reading the questions.
B. MAIN LESSON
Output:
Instance variables and methods are accessed via created objects. To access an instance variable, following is
the fully qualified path −
Classes in Java
A class is a blueprint from which individual objects are created. A class can contain any of the following
variable types.
• Local variables – this are variables defined inside methods, constructors or blocks. The variable will be
declared and
initialized within the method and the variable will be destroyed when the method has completed.
• Instance variables − Instance variables are variables within a class but outside any method. These
variables are initialized
when the class is instantiated. Instance variables can be accessed from inside any method, constructor or
blocks of that
particular class.
• Class variables − Class variables are variables declared within a class, outside any method, with the static
keyword.
A class can have any number of methods to access the value of various kinds of methods. In the above
example, barking(),
hungry() and sleeping() are methods.
Constructor- Every class has a constructor. The main rule of constructor is that they should have the same
name as class. Each time a new object is created, at least one constructor will be invoked.
Example 1
Example 2
Public class MyClass;
Int num;
MyClass();
num=100;
{
}
Let’s Practice! What is the output of the following Java codes? Write your answer in the box
below.
Output:
Based on what you have learn in this course overview about Computer Programming 2, answer
the third column in the table found at the page 1 of this module.
Answer:
// A simple constructor.
class MyClass {
int x;
Output:
FAQ’S
1. What will happen if we do not explicitly write a constructor for a class?
• The Java compiler builds a default constructor for that class.