CSC 211 Lesson 4 Part1-2
CSC 211 Lesson 4 Part1-2
Mr. Tarus
Outline
Basic Object Oriented Concepts in Java.
Outline
1. Classes
2. Objects
class
Template / blueprint that describes the data and behavior associated with the class
instantiation.
logical template to create the objects that share common properties and methods.
group of objects which have common properties.
It is a logical entity.
Think of the class as a sketch (prototype) of a house. It contains all the details
about the floors, doors, windows, etc. Based on these descriptions we build the
house. House is the object.
Since many houses can be made from the same description, we can create many
objects from a class.
A class in Java can contain:
Fields
Methods
Constructors
Blocks
Nested class and interface
Syntax:
class <class_name>{
field;
method;
}
//java class example void sleeping() {
public class Dog1 { System.out.println("Dog is Sleeping\
String breed; n");
int age; }
String color; public static void main(String[] args)
void barking() { {
System.out.println("Dog is barking\n"); Dog1 d = new Dog1();
} d.barking();
void hungry() { d.sleeping();
System.out.println("Dog is Hungry\n"); d.hungry();
} }
}
Dog1.java
Types of variables/attributes/fields in a class
Local variables:
Variables defined inside methods, constructors or blocks.
Declared and initialized within the method and the variable will be destroyed
when the method has completed.
Instance variables:
Variables within a class but outside any method.
Initialized when the class is instantiated.
Can be accessed from inside any method, constructor or blocks of that particular
class.
Class variables / static variables:
Variables declared within a class, outside any method, with the static keyword.
Variable can be shared by all class instances
EmployeeTest.java
//Types of variables in java
class VariableType {
static int m=100; //static variable
void method()
{
int n=90; //local variable
System.out.println("Local variable = " +n);
}
public static void main(String args[]) {
VariableType vt = new VariableType();
vt.method();
int data=50; //instance variable
System.out.println("inatance variable = " +data);
}
object
Run time entity in oop.
An object is a real-world entity.
The object is an entity which has state and behavior.
The object is an instance of a class.
new keyword is used to create new objects.
Characteristics:
State: represents the data (value) of an object.
Behavior: represents the behavior (functionality) of an object such as deposit,
withdraw, etc.
Identity: An object identity is typically implemented via a unique ID. The value
of the ID is not visible to the external user. However, it is used internally by the
JVM to identify each object uniquely.
Three steps when creating an object from a class.
Declaration:
− An object declaration using the class name and valid variable name.
Instantiation:
1. By reference variable
2. By method
3. By constructor
Initialization through reference variable. StudentRef1.java
class Student5{
public int rollno;
String name;
}
public class StudentRef1{
public static void main(String args[]){
Student5 s1 = new Student5();
s1.rollno = 101;
s1.name = "last Boy";
System.out.println(s1.rollno+" " +s1.name);
}
}
//object creating and passing values (By Method). Puppy.java
public class Puppy {
public Pupp4(String name, int age, String color) {
System.out.println("Puppy Name is :" + name );
System.out.println("Puppy age is :" + age );
System.out.println("Puppy colour is :" + color );
}
The methods or data members declared as private are accessible only within the
class in which they are declared.
Any other class of the same package will not be able to access these members.
The methods or data members declared as protected are accessible within the
same package or subclasses in different packages.
Public Access Modifier
• Nb: Default package and test package