Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
94 views

Module 2 Getting To Know Java and Object Oriented Programming

This document provides an overview of an introductory lesson on Java and object-oriented programming. The lesson objectives are to understand object model concepts. The document defines objects as instances of classes that have states and behaviors. It also defines classes as templates that describe the behaviors and states of their object types. The document then provides examples of creating objects from classes in Java using the new keyword and accessing object variables and methods. It also discusses class variables, instance variables, and constructors.

Uploaded by

Kahrl Cerin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Module 2 Getting To Know Java and Object Oriented Programming

This document provides an overview of an introductory lesson on Java and object-oriented programming. The lesson objectives are to understand object model concepts. The document defines objects as instances of classes that have states and behaviors. It also defines classes as templates that describe the behaviors and states of their object types. The document then provides examples of creating objects from classes in Java using the new keyword and accessing object variables and methods. It also discusses class variables, instance variables, and constructors.

Uploaded by

Kahrl Cerin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

College for Research & Technology of

Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com

Lesson Title: Getting to know Java and Object-Oriented Programming

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.

2. Activity 1: What I Know Chart (PART 1)

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.

What I Know Questions: What I Learned (Activity 4)


1. What is object in Java?

2. What is class in Java?


College for Research & Technology of
Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com

B. MAIN LESSON

1. Activity 2: Content Notes


Objects in Java
Objects also have a state of behavior. An object’s state is stored in fields and behavior is shown
via methods. So in software development, methods operate on the internal state of an object and the
object-object communication is done via methods. An object is created from a class. In Java, the new
keyword is used to create new objects.
There are three steps when creating an object from a class
1. Declaration – a variable declaration with a variable name with an object type.
2. Instantiation – the ‘new’ keyword is used to create the object.
3. Initialization – the ‘new’ keyword is followed by a call to a constructor. This call initializes the new
object.
Example:
public class Puppy {
public Puppy(String name) {
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public static void main(String []args) {
// Following statement would create an object myPuppy
Puppy myPuppy = new Puppy( "tommy" );
}
}

Output:

Passed Name is: tommy

Accessing Instance Variables and Methods

Instance variables and methods are accessed via created objects. To access an instance variable, following is
the fully qualified path −

/* First create an object */


ObjectReference = new Constructor();
College for Research & Technology of
Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com

/* Now call a variable as follows */


ObjectReference.variableName;

/* Now you can call a class method as follows */


ObjectReference.MethodName();

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

public class Puppy {


public Puppy() {
}

public Puppy(String name) {


// This constructor has one parameter, name.
}
}
College for Research & Technology of
Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com

Example 2
Public class MyClass;
Int num;
MyClass();
num=100;
{
}

You would call constructor to initialize objects as follows:


public class ConsDemo{
public static void main(String args[]){
MyClass t1=new MyClass();
MyClass t2=new MyClass();
System.out.printIn(t1.num+””+t2.num);
}
}
Output:
100 100

2. Activity 3: Skill- Building Activities

Let’s Practice! What is the output of the following Java codes? Write your answer in the box
below.

public class Puppy {


int puppyAge;

public Puppy(String name) {


// This constructor has one parameter, name.
System.out.println("Name chosen is :" + name );
}
College for Research & Technology of
Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com

public void setAge( int age ) {


puppyAge = age;
}

public int getAge( ) {


System.out.println("Puppy's age is :" + puppyAge );
return puppyAge;
}

public static void main(String []args) {


/* Object creation */
Puppy myPuppy = new Puppy( "tommy" );

/* Call class method to set puppy's age */


myPuppy.setAge( 2 );

/* Call another class method to get puppy's age */


myPuppy.getAge( );

/* You can access instance variable as follows as well */


System.out.println("Variable Value :" + myPuppy.puppyAge );
}
}

Output:

3. Activity 4: What I Know Chart

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.

4. Activity 5: Check for Understanding


Answer the following question:
College for Research & Technology of
Cabanatuan
Burgos Avenue, Cabanatuan City 3100
Tel. 463-2735 463-2697 600-2200 e-mail: crt.cabanatuan@gmail.com

1. What is the constructor parameter in the code below?


public Puppy(String gender) {

System.out.println("The respondents gender is :" + gender);

Answer:

2. What is the output of the following code?

// A simple constructor.
class MyClass {
int x;

// Following is the constructor


MyClass(int i ) {
x = i;
}
}

You would call constructor to initialize objects as follows −

public class ConsDemo {

public static void main(String args[]) {

MyClass t1 = new MyClass( 10 );

MyClass t2 = new MyClass( 20 );

System.out.println(t1.x + " " + t2.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.

You might also like