Fundamentals of computing 1 - Lecture title: Encapsulation

Java Access Modifier Encapsulation static keyword final keyword

ppt34 trang | Chia sẻ: nguyenlam99 | Lượt xem: 852 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Fundamentals of computing 1 - Lecture title: Encapsulation, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Lecture Title: EncapsulationFundamentals of Computing 1AgendaEncapsulationJava Access Modifierstatic keywordfinal keywordWhat is Encapsulation?Encapsulation is a technique to hide the attribute (or some method) and provide access to the those attribute via public methodA person could perform all the functions such as seeing, smelling, eating, ... without knowing how does it performWhy encapsulation “exist”?Imagine that we work for a project with some programmers.Somehow your program got error because the other programmer set a wrong attribute’s value on the your classHow can it happen?Here is the exampleHere is the examplePossible, but not logical!Output: Trang weights -10 poundsHere is the better codePossible, but logic error is catched if one occursAgendaEncapsulationJava Access Modifierstatic keywordfinal keywordJava Access ModifierAccess Modifier defines the boundary and scope to access the method, variable and class. Java has define four types of access modifier such as:privateprotectedpublicdefaultThese four java access modifiers define the scope for variables/methods. if you are not define any access modifier so it will be as ‘default’ access modifier for varibles/methods.Java Access Modifier (cont.)◆ = can access ◇ = can not accessprivatedefaultprotectedpublicSame class◆◆◆◆Same packageSubclass◇◆◆◆Same packageNon-subclass◇◆◆◆Different packageSubclass◇◇◆◆Different packageNon-subclass◇◇◇◆Java Access Modifier (cont.)/* Rule 1 same class * default private protected public */public class A { int x = 1; //Access modifier default private int pvt = 2; protected int pro = 3; public int pb = 4; // Methods here can access all variables public void display1(){ System.out.println(x); System.out.println(pvt); System.out.println(pro); System.out.println(pb); }}Java Access Modifier (cont.)/* Rule 2 same package sub class * default protected public */package test;public class A { int x = 1; private int pvt = 2; protected int pro = 3; public int pb = 4; // Method here can access all variables}package test;public class B extends A { // Can not access private members public void display2(){ System.out.println(x); System.out.println(pvt); //error System.out.println(pro); System.out.println(pb); }}Java Access Modifier (cont.)/* Rule 3 same package non sub class * default protected public */package test;public class A { int x = 1; private int pvt = 2; protected int pro = 3; public int pb = 4; // Method here can access all variables}package test;public class Test{ // Can not access private members public void display3(){ A a = new A(); System.out.println(a.x); System.out.println(a.pvt); //error System.out.println(a.pro); System.out.println(a.pb); }}Java Access Modifier (cont.)/* Rule 4 different package sub class * protected public */package test;public class A { int x = 1; private int pvt = 2; protected int pro = 3; public int pb = 4; // Methods here can access all variables}package testnew;import test.A;public class C extends A{ // Can not access default private members public void display4(){ A a = new A(); System.out.println(a.x);//error System.out.println(a.pvt);//error System.out.println(pro); System.out.println(a.pb); }}Java Access Modifier (cont.)/* Rule 5 different package non sub class * public */package test;public class A { int x = 1; private int pvt = 2; protected int pro = 3; public int pb = 4; // Methods here can access all variables}package testnew;import test.A;public class D{ // Can not access default private protected // members public void display5(){ A a = new A(); System.out.println(a.x);//error System.out.println(a.pvt);//error System.out.println(a.pro);//error System.out.println(a.pb); }}AgendaEncapsulationJava Access Modifierstatic keywordfinal keywordstatic keywordThe static keyword is used in java mainly for memory managementThe static keyword belongs to the class than instance of the class. The static can be:variable (also known as class variable)method (also known as class method)block1) static variableIf you declare any variable as static, it is known as static variableThe static variable can be used to refer the common property of all objectsThe static variable gets memory only once in class area at the time of class loading.Advantage of static variableIt makes your program memory efficient (i.e it saves memory).Understanding problem without static variableOutput: Number of person objects created : 1 Number of person objects created : 1 Number of person objects created : 1Program with static variable (1)Output: Number of person objects created : 1 Number of person objects created : 2 Number of person objects created : 3Program with static variable (2)p1p2  name = Trangname = DuongHeap MemoryRam Memoryp2name = Luongcount = 32) static methodIf you apply static keyword with any method, it is known as static methodA static method belongs to the class rather than object of a class. A static method can be invoked without the need for creating an instance of a class. static method can only access static data member and can change the value of itExample of static methodOutput: 111 Trang BBDIT 222 Duong BBDITCall static methodSyntax: classname.methodname Restrictions for static methodThere are two main restrictions for the static method.The static method can not use non static data member or call non-static method directly.this and super cannot be used in static context.Restrictions for static method - Exampleclass A{int a=40;//non staticpublic static void main(String args[]){ System.out.println(a);}} Output: Compile Time ErrorAgendaEncapsulationJava Access Modifierstatic keywordfinal keywordfinal keywordThe final keyword in java is used to restrict the user. The final keyword can be used in many contextvariablemethodclass1) final variableIf you make any variable as final, you cannot change the value of final variable (It will be constant)Example of final variableOutput: Compile Time Error2) final methodIf you make any method as final, you cannot override itExample of final methodOutput: Compile Time Error3) final classIf you make any class as final, you cannot extend itExample of final classOutput: Compile Time ErrorAgendaJava Access ModifierEncapsulationstatic keywordfinal keyword

Các file đính kèm theo tài liệu này:

  • pptlecture_11_encapsulation_6873.ppt
Tài liệu liên quan