0% found this document useful (0 votes)
4 views

CSC 211 Lesson 4 Part1-2

The document outlines basic Object-Oriented Programming (OOP) concepts in Java, including classes, objects, and variable types. It explains how to initialize objects and the use of access modifiers to control visibility and accessibility in Java. Examples of Java code demonstrate these concepts in practice.

Uploaded by

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

CSC 211 Lesson 4 Part1-2

The document outlines basic Object-Oriented Programming (OOP) concepts in Java, including classes, objects, and variable types. It explains how to initialize objects and the use of access modifiers to control visibility and accessibility in Java. Examples of Java code demonstrate these concepts in practice.

Uploaded by

justusnyamaiamin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Lecture 4: OOPS Concept

Mr. Tarus
Outline
 Basic Object Oriented Concepts in Java.

 Types of variables/attributes/fields in a class.

 Ways to initialize an object.

 Case program example to demonstrate more than one program executing


as one program.
 Access Modifiers
3. 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:

− The 'new' keyword is used to create the object.


Initialization:

− The 'new' keyword is followed by a call to a constructor. This call


initializes the new object.
Pto…
//Java Program to demonstrate having the main method in another class
//Creating Student class.
class Student{
int id;
String name;
}
//Creating another class TestStudent1 which contains the main method
class TestStudent1{
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
Ways to initialize an object

NB: Initializing an object means storing data into the object.

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 );
}

public static void main(String []args) {


// create an object myPuppy
Puppy myPuppy = new Puppy( "tommy",23, "Black");
//myPuppy.Pupp4("tommy",23, "Black“);
}
Example Animalia.java
 Create a 3 programs that run (being managed by one main method).
 Classes:
Animalia (main class)
Frog
Lizard
Tortoise
Access Modifiers
 Helps to restrict the scope of a class, constructor, variable, method, or data
member.
 It provides security, accessibility, etc. to the user depending upon the access
modifier used with the element.
 Types of Access Modifiers
 There are 4 types of access modifiers available in Java:
Default – No keyword required
Private
Protected
Public
Default Access Modifier, DriverDefault.Java

 When no access modifier is specified for a class, method, or data member, it is


said to be having the default access modifier by default.
 The default access modifiers are accessible only within the same package.
Private Access Modifier
 Specified using the keyword private.

 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.

 Top-level classes or interfaces can not be declared as private because,


 private means “only visible within the enclosing class“.
 protected means “only visible within the enclosing class and any
subclasses“.
Protected Access Modifier
 Specified using the keyword protected.

 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

• The public access modifier is specified using the keyword public.


• The public access modifier has the widest scope among all other access
modifiers.
• Classes, methods, or data members that are declared as public are accessible
from everywhere in the program. There is no restriction on the scope of public
data members.

You might also like