Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topics Instance variables, set and get methods Encapsulation

Similar presentations


Presentation on theme: "Topics Instance variables, set and get methods Encapsulation"— Presentation transcript:

1 Topics Instance variables, set and get methods Encapsulation
Access Modifiers Default Access Modifier Private Access Modifier Public Access Modifier Program Example and UML Constructor Default and Parameter Constructor Program and UML

2 Instance variables, set and get Methods
class variables are called as instance variables, these variables are created whenever an object of the class is instantiated. class Rectangle { public int length; //length of rectangle public int width; //width of rectangle public double height //height of rectangle } length, width, height are called instance variables. length and width is initialization with 0 and height is initialization with 0.0

3 Instance variables, set and get Methods
These methods are used for storing and accessing the instance variables of the class. For Example: Instance variables class BankAccount { private int accountNo; private double balance; public void setAccountNo(int accNo){ accountNo = accNo; } public int getAccountNo(){ return accountNo; public void setBalanace(double bal){ balance = bal; public double getBalance(){ return balance; set and get methods

4 Encapsulation and Access Modifiers
Encapsulation is one of the four fundamental OOP concepts. Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled. Benefits of Encapsulation: The fields of a class can be made read-only or write-only. A class can have total control over what is stored in its fields. The users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code. Object encapsulation can be done in java by using access modifiers because they control access to the members of a class. The access modifiers are also known as visibility modifiers that can be applied to the variables and methods of a class.

5 Access Modifiers Java provides a number of access modifiers to help you set the level of access you want for classes as well as the fields, methods and constructors in your classes. Access Modifiers 1. default 2. private 3. public 4.protected

6 Default Access Modifier
Default access modifier means we do not explicitly declare an access modifier for a class, field, method etc. A variable or method declared without any access control modifier is available to any other class in the same package. The default modifier cannot be used for methods, fields in an interface. Example: Variables and methods can be declared without any modifiers, as in the following examples: String version = “1.5.1” boolean processOrder(){ return value; }

7 Private Access Modifier
Methods, Variables that are declared private can only be accessed within the declared class itself. Private access modifier is the most restrictive access level. Class and interfaces cannot be private. Variables that are declared private can be accessed outside the class if public getter methods are present in the class. Using the private modifier is the main way that an object encapsulates itself and hide data from the outside world.

8 Private Access Modifier
Example: The following class uses private access control: class Logger{ private String format; public String getFormat(){ return this.format } public void setFormat(String format){ this.format = format; Here, the format variable of the Logger class is private, so there's no way for other classes to retrieve or set its value directly. So to make this variable available to the outside world, we defined two public methods: getFormat(), which returns the value of format, and setFormat(String), which sets its value. Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this. The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

9 Public Access Modifier
A class, method, constructor, interface etc declared public can be accessed from any other class. Therefore fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe. However if the public class we are trying to access is in a different package, then the public class still need to be imported. Example: The following function uses public access control: public static void main(String args[ ]){ //...... } The main() method of an application has to be public. Otherwise, it could not be called by a Java interpreter (such as java) to run the class.

10 Access Modifier

11 Program Instance variable courseName of private
setMethod for courseName getMethod for courseName Call getMethod

12 UML for above Class GradeBook - courseName : String
+ setCorseName(name: String) + getCourseName() : String + displayMessage()

13 Call getMethod for courseName Call setMethod for courseName
Program Call getMethod for courseName Call setMethod for courseName Call displayMessage

14 Constructor It would be simpler and more concise to initialize an object when it is first created. Java supports special type of method, called Constructor, that enables an Object to initialize itself when it is created. A java constructor has the same name as the name of the class to which it belongs. Constructor are of twp type: they are Default Constructor Parameterized constructor

15 Default / Parameter Constructor
Default Constructor: If you don’t define a constructor for a class, a default parameter less constructor is automatically created by the compiler. The default constructor is called and initialized with (Zero to numeric types , null for object references and false for Booleans); Parameter less constructor: Constructor with no parameters is called parameter less constructor. Parameterized constructor: Constructors that can take arguments are termed as parameterized constructors. The types must match those that are specified in the constructor definition. Example: 1 public class Rectangle { int length, width, height Rectangle(){ Length = 10; width = 30; height = 10; } Example: 2 public class Rectangle { int length, width, height Rectangle(int len, int wid, int hei){ Length = len; width = wid; height = hei; }

16 Constructor to initialize courseName
Program Constructor to initialize courseName

17 UML for above Class GradeBook - courseName : String
<<constructor>> GradeBook(name : String) + setCorseName(name: String) + getCourseName() : String + displayMessage()

18 Calls a constructor to create an object for GradeBook
Program Calls a constructor to create an object for GradeBook

19 ?


Download ppt "Topics Instance variables, set and get methods Encapsulation"

Similar presentations


Ads by Google