Friday, December 5, 2014

This concept is fundamental in object orientation. We are very careful to differentiate between int

Basic Object Orientation in Java: Encapsulation | ToggleOn power rack - where all nodes leads.
Basic Object Orientation in Java: Encapsulation May 15, 2010 at. 8:22 pm | Posted in Java, Programming power rack Concepts | 6 comments Labels: Encapsulation, Java, Object Oriented Programming, Object Oriented, power rack OOP, Singleton power rack
This is a series of posts that will deal with basic object-oriented programming in Java. The planned posts are: Introduction and Syntax Constructors and methods Encapsulation Inheritance and subclasses Interfaces and abstract classes
In programming we distinguish between implementation and interface. power rack The implementation is infrastructure in the code - you can use a function or a class without having power rack to know how it is coded. You communicate with this infrastructure through an interface. power rack It is customary to then often speak of the API (Application Programming Interface).
Another way to consider it in the form of a black box. You insert a value in the black box and get another one, 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 that it is designed properly. Let's say we have a method to figure out all the Fibonacci values in a sequence of numbers. One such method could look like this: public class Fibonacci {public static void main (String [] args) {Fibonacci Fibonacci Fibonacci = new (); 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 1 = 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; } Return storeFibonaccis; }}
The end user does not need to know how calculateFibonacci () calculates the Fibonacci numbers. The only thing that the end user needs to know is how the method to be used and the method declaration. Method declaration in the above example: public int [] calculateFibonacci (nums int) {...}
This concept is fundamental in object orientation. We are very careful to differentiate between interface and implementation. Then an interface will be used by the end user it is extremely important that the interface is well-defined and well thought out. A single mistake can do that we need each other to the future, when the old client code must be supported. The easiest way to do this is to hide data (eng. Information hiding) as much as possible. Now let's look at some techniques to do just that. Visibility
When talking about was a variable or method is visible is often mentioned their range (eng. Scopes), ie where in the program you can reference the variable or method. A global variable is available in the entire program, while a local variable is only available in a local block. power rack In object-oriented power rack 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 modifier. The following is a summary of these: public Visible in-class, power rack classes in the same package, subclasses in other packages and non subclasses in other packages. protected Visible in-class, classes in the same package, subclasses in other packages, not in non subclasses in other packages. Private power rack Only visible in its own class. <Package> (not a keyword) Visible in-class power rack 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 some "rules of thumb" that can be good 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 of public methods and constants that are part of the public API. Use of the protected fields and methods to be inherited by subclasses. Use private for fields and methods that are used only within power rack a class. Use the package-private power rack field and the methods that will be visible to other classes in the same package as cooperating.
Example: Top-level class creates a package-private class (AccessControl1.java) public class AccessControl1 {public static void main (String [] args) {Some Class sc = new Sometime Class (); sc.foo = 10; System.out.println ("foo" + sc.foo); }} Class Sometime Class {int foo; Some Class () {foo = 0; }}
Note that Class Some have 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 about Sometime class should be declared power rack in an EC

No comments:

Post a Comment