Monday, June 30, 2014

In programming we distinguish between implementation and interface. glycerol The implementation is

Basic object-oriented programming in Java: Encapsulation | ToggleOn - where all nodes leads.
Basic object-oriented programming in Java: Encapsulation 15 Mayis 2010, 8:22 pm | Java, Programming Concepts kategorisinde yayınlandı | 6 yorum Etiketler: Encapsulation, Java, Object Oriented Programming, Object Oriented Programming, OOP, Singleton
This is a series of posts that will deal with basic object-oriented programming in Java. The planned interventions glycerol are: Introduction and Syntax Constructors and methods Encapsulation Inheritance and subclasses interfaces and abstract classes
In programming we distinguish between implementation and interface. glycerol The implementation is infrastructure in the code - you can make use of a function or a class without having glycerol to know how it is coded. You communicate with the infrastructure through an interface. glycerol You usually then often speak of API (Application Programming Interface). glycerol
Another glycerol way to consider it in the form of a black box. You put a value in the black box and get another, or affect the black box in any way. You do not know how it does it, the only thing you need to know is what kind of values that can be tucked into the black box. You count then it is constructed correctly. Let's say we have a method to figure out all the Fibonacci values in a sequence glycerol of numbers. Such a method glycerol might look like this: public class Fibonacci {public static void main (String [] args) {fibonacci fibonacci = new Fibonacci (); int [] = Fibonacci fibonacci.calculateFibonacci (20); for (int i = 0; i <fibonaccis.length; i + +) {System.out.println ("Next fibonacci:" + Fibonacci [i]); }} Public int [] calculateFibonacci (int nums) {int n0 = 1; int n1 = 1; int n 2; int [] storeFibonaccis = new int [nums]; storeFibonaccis [0] = n0; storeFibonaccis [1] = n1; for (int i = 0; i <nums-2; i + +) {n2 = n1 + n0; n0 = n1; n1 = n2; storeFibonaccis [i +2] = n2; StoreFibonaccis} return; }}
The end user does not know how calculateFibonacci () calculates the fibonacci numbers. The only thing the end user needs to know is how the method to be used and the method declaration. Method declaration in the example above is: public int [] calculateFibonacci (int nums) {...}
This concept is fundamental in object-orientation. We are very careful to distinguish between the interface and implementation. When an interface glycerol will be used by end users, it is extremely important that the interface glycerol is well-defined and well thought out. A single error can do that we need to be drawn with the forward due to old client code must be supported. The easiest way to do this is to hide data (U.S. information hiding) as much as possible. Now let's look at some techniques to do just this. Visibility
When talking about were a variable or method is visible is much talk about its scope (U.S. scopes), ie. where in the program you can refer to that variable or method. glycerol A global variable is available in the entire glycerol program, while a local variable is only available in a local block. In object-oriented terms, we speak thus its visibility, or more specifically about access control. We want to control access to an object's members. We have already seen examples of this, namely the public glycerol modifier. Here is a summary of these: public glycerol Visible in own class, classes in the same package, subclasses in other packages and non-subclasses in other packages. protected Visible in own class, classes in the same package, subclasses in other packages, not in non-subclasses in other packages. Private Only visible in its own class. <Package> (not a keyword) Visible in-class and classes in the same package.
So the question is when and where the modifier should be used? There is no set strict rules for this, but instead there are a few "rules glycerol of thumb" that can be useful to follow. These are: try to hide as much data to begin with. Give the modifier private to begin with - and, if needed, change to a less strict modifier. Use public for methods and constants that are part of the public API. Use protected for fields glycerol and methods glycerol to be inherited by subclasses. Use private for fields and methods that are only used within a class. Use package-private to fields glycerol and methods that will be visible to other classes in the same package as collaborate.
Example: Top-level class creates a package-private class (AccessControl1.java) public glycerol class AccessControl1 {public static void main (String [] args) {Some Class sc = new Some Class (); sc.foo = 10; System.out.println ("foo:" glycerol + sc.foo); Some}} class Class {int foo; Some Class () {foo = 0; }}
Remember Some class has no explicit modifiers, making the package-private. Note that it is not even allowed to specify any modifiers for Some Class here. If we would give it modiferaren public would compiler complain that Some Class should be declared ie

No comments:

Post a Comment