Understanding Encapsulation in OOP with Java: Classes, Objects, and Access, Lecture notes of Computer science

An in-depth explanation of encapsulation in object-oriented programming (oop) using java. It covers the relationship between classes and objects, the concept of encapsulation, and the use of access modifiers to control access to class variables and methods. Examples and code snippets to illustrate the concepts.

Typology: Lecture notes

2010/2011

Uploaded on 01/02/2024

ive-teecher
ive-teecher 🇭🇰

1 document

1 / 36

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Lecture 2 – Classes
What is Object Oriented Programming?
Object oriented Programming languages (OOP for short) include all
the features of structured programming and add more powerful ways
to organize algorithms and data structures.
There are three key features of OOP languages:
Encapsulation
Inheritance
Polymorphism
All of them are tied to the notion of a class
These two lectures focus on encapsulation through the use of classes
and objects - Object Based Programming.
Introduction
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

Partial preview of the text

Download Understanding Encapsulation in OOP with Java: Classes, Objects, and Access and more Lecture notes Computer science in PDF only on Docsity!

Lecture 2 – Classes

  • (^) What is Object Oriented Programming?
  • (^) Object oriented Programming languages (OOP for short) include all

the features of structured programming and add more powerful ways

to organize algorithms and data structures.

  • (^) There are three key features of OOP languages:
    • (^) Encapsulation
    • (^) Inheritance
    • (^) Polymorphism
  • (^) All of them are tied to the notion of a class
  • (^) These two lectures focus on encapsulation through the use of classes

and objects - Object Based Programming.

Introduction

Classes

  • (^) A class defines a set of variables ( attributes or data members ) that can be associated with the methods which act on an object with the object itself. data member methods

Classes

  • (^) A class contains data declarations and method declarations int x, y; char ch; Data declarations Method declarations

Classes

  • (^) A class can be viewed as a blueprint of an object.
  • (^) It is the model or pattern from which objects are created.
  • (^) For example, the String class is used to define String objects.
  • (^) Each String object contains specific characters (its state).
  • (^) Each String object can perform services (behaviors) such as toUpperCase.

Classes and Objects s1:String "Programming" s2:String "Java" s3:String "ive-ty" String (class)

  • (^) The String class can be used to define as many String objects as you like.
  • (^) Its reusability is very high.
  • (^) The String class is provided for us by the Java standard class library.
  • (^) But we can also write our own classes that define specific objects that we need

class TestStringClass { public static void main(String s[]) { String s1 = new String("Programming"); String s2 = new String("Java"); String s3 = new String("ive-ty"); System.out.println(s1.toUpperCase()); System.out.println(s2.toUpperCase()); System.out.println(s3.toUpperCase()); } } Classes and Objects In this example, String is class. s1, s2 and s3 are the objects (or references to objects, precisely speaking). Each of them has different state but same behavior.

Example

  • (^) Thus when we create a new object we say we are instantiating the object. Each class exists only once in a program, but there can be many thousands of objects that are instances of that class.
  • (^) To instantiate an object in Java we use the new operator. Here's how we'd create a new employee: Employee x = new Employee(); Class Name Object Name Class Name (Again!)
  • (^) The instantiation can also be done in two lines: Employee x; x = new Employee();

Example

  • (^) The members of an object are accessed using the. (dot) operator, as follows: Employee x = new Employee(); x.setNum( 12651 ); Object Name . (dot) Method Name

Example Employee emp1 = new Employee(); Employee emp2 = new Employee(); Memory emp num : 0 getNum setNum emp num : 0 getNum setNum emp num : 0 getNum setNum

Example emp1.setNum(12651); emp2.setNum(36595); emp1.getNum() returns 12651 emp2.getNum() returns 36595

Note that the object name (emp1 and emp2) is simplified here. See

later slides for details

Memory emp num : 12651 getNum setNum emp num : 12651 getNum setNum emp num : 36595 getNum setNum emp num : 0 getNum setNum

Encapsulation

  • (^) As you can see from the above diagram, an object's variables make up the centre or nucleus of the object. Methods surround and hide the object's nucleus from other objects in the program.
  • (^) This is called encapsulation. emp num : 0 getNum setNum

Encapsulation

  • (^) Member access modifiers
    • (^) Control access to class’ instance variables and methods
    • (^) public
      • (^) Variables and methods accessible to other classes
    • (^) private
      • (^) Variables and methods not accessible to other classes
      • (^) Can be accessed via
        • (^) Accessor method (“ get ” method)
          • (^) Allow clients to read private data
        • (^) Mutator method (“ set ” method)
          • (^) Allow clients to modify private data

A BAD Example class PublicDataEmployee { public int num; } class PrintPublic { public static void main(String[] args) { PublicDataEmployee emp1 = new PublicDataEmployee(); PublicDataEmployee emp2 = new PublicDataEmployee(); emp1.num = 12651; emp2.num = 36595; System.out.println("num of emp1 : " + emp1.num ); System.out.println("num of emp2 : " + emp2.num ); } }

java PrintPublic num of emp1 : 12651 num of emp2 : 36595

  • (^) The following example shows an incorrect use of public data member.

A GOOD Example class PrivateDataEmployee { private int num; public int getNum() {return num;} public void setNum(int newNum) {num=newNum;} } class PrintPrivate { public static void main(String[] args) { PrivateDataEmployee emp1 = new PrivateDataEmployee(); PrivateDataEmployee emp2 = new PrivateDataEmployee(); emp1.setNum(12651); emp2.setNum(36595); System.out.println("num of emp1 : " + emp1.getNum() ); System.out.println("num of emp2 : " + emp2.getNum() ); } }

java PrintPrivate num of emp1 : 12651 Num of emp2 : 36595

  • (^) The following shows how to fix the problem in the previous example.