Declarations and Access Control Java Refresher Class A

  • Slides: 27
Download presentation
Declarations and Access Control Java Refresher Class A template that describes the kinds of

Declarations and Access Control Java Refresher Class A template that describes the kinds of state and behavior that objects of its type support. Object At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its own state, and access to all of the behaviors defined by its class. State (instance variables) Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object's instance variables make up the object's state. Behavior (methods) When a programmer creates a class, she creates methods for that class. Methods are where the class' logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.

Identifiers and Keywords Java Refresher Identifiers All the Java components we just talked about—classes,

Identifiers and Keywords Java Refresher Identifiers All the Java components we just talked about—classes, variables, and methods need names. In Java these names are called identifiers. Example: - Animal , employee. Name, set. Name(String name) Keywords Like all programming languages, Java has a set of built-in keywords. These keywords must not be used as identifiers. Example : - int , long, if , else, while, float, double, Integer, Double

Inheritance Java Refresher Inheritance Central to Java and other object-oriented languages is the concept

Inheritance Java Refresher Inheritance Central to Java and other object-oriented languages is the concept of inheritance, which allows code defined in one class to be reused in other classes. Example : - a Car superclass could define general methods common to all automobiles, but a Ferrari subclass could override the accelerate() method.

Interfaces Java Refresher Interfaces A powerful companion to inheritance is the use of interfaces.

Interfaces Java Refresher Interfaces A powerful companion to inheritance is the use of interfaces. Interfaces are like a 100 -percent abstract superclass that defines the methods a subclass must support, but not how they must be supported. Example: interface Animal { void eat(); } class Cow implements Animal { void eat() { System. out. println(“Vegitarian”); }

Identifiers & Java. Beans Legal Identifiers 1) 2) 3) 4) 5) Identifiers must start

Identifiers & Java. Beans Legal Identifiers 1) 2) 3) 4) 5) Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). Identifiers cannot start with a number! After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers. In practice, there is no limit to the number of characters an identifier can contain. You can't use a Java keyword as an identifier. Identifiers in Java are case-sensitive; foo and FOO are two different identifiers.

Identifiers & Java. Beans Legal Identifiers Examples of legal and illegal identifiers follow, first

Identifiers & Java. Beans Legal Identifiers Examples of legal and illegal identifiers follow, first some legal identifiers: int _a; int $c; int ______2_w; int _$; int this_is_a_very_detailed_name_for_an_identifier; The following are illegal (it's your job to recognize why): int : b; int -d; int e#; int. f; int 7 g;

Identifiers & Java. Beans Complete List of Java Keywords

Identifiers & Java. Beans Complete List of Java Keywords

Sun's Java Code Conventions Classes and interfaces The first letter should be capitalized, and

Sun's Java Code Conventions Classes and interfaces The first letter should be capitalized, and if several words are linked together to form the name, the first letter of the inner words should be uppercase (a format that's sometimes called “Camel. Case"). For classes, the names should typically be nouns. For example: Dog Account Print. Writer For interfaces, the names should typically be adjectives like Runnable Serializable

Sun's Java Code Conventions Methods The first letter should be lowercase, and then normal

Sun's Java Code Conventions Methods The first letter should be lowercase, and then normal camel. Case rules should be used. In addition, the names should typically be verb -noun pairs. For example: get. Balance do. Calculation set. Customer. Name Variables Like methods, the camel. Case format should be used, starting with a lowercase letter. Sun recommends short, meaningful names, which sounds good to us. Some examples: button. Width; account. Balance; Constants Java constants are created by marking variables static and final. They should be named using uppercase letters with underscore characters as separators: MIN_HEIGHT

Java. Beans Standards Java. Beans are java classes that have properties Using getter Using

Java. Beans Standards Java. Beans are java classes that have properties Using getter Using setter method to PROPERTIES initialize instance variable x; (also called mutter) The only way to access this Instance variable out side the Class is through public methods method to access instance variable x; (also called accessor) Private Instance Variable private int x; public void set. X(int xx) { x=xx; } public int get. X() { return x; }

Sun's Java Code Conventions Java. Bean Property Naming Rules 1. )If the property is

Sun's Java Code Conventions Java. Bean Property Naming Rules 1. )If the property is not a boolean, the getter method's prefix must be get. Example : private int size; public int get. Size() { return size; } 2) If the property is a boolean, the getter method's prefix is either get or is. public boolean get. Stopped() { return stopped; } public boolean is. Stopped() { return stopped; }

Sun's Java Code Conventions Java. Bean Property Naming Rules 3) The setter method's prefix

Sun's Java Code Conventions Java. Bean Property Naming Rules 3) The setter method's prefix must be set. for example: public void set. Size(int s) { size=s; }

Classes Source File Declaration Rules 1)There can be only one public class per source

Classes Source File Declaration Rules 1)There can be only one public class per source code file. 2) Comments can appear at the beginning or end of any line in the source code file; they are independent of any of the positioning rules discussed here. 3)If there is a public class in a file, the name of the file must match the name of the public class. For example, a class declared as public class Dog { } must be in a source code file named Dog. java. 4)If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present. 5)If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file. If there are no package or import statements, the class declaration must be the first line in the source code file. 6)import and package statements apply to all classes within a source code file. In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports. 7)A file can have more than one nonpublic class. 8) Files with no public classes can have a name that does not match any of the classes in the file.

Classes Class Declarations and Modifiers The following code is a bare-bones class declaration: class

Classes Class Declarations and Modifiers The following code is a bare-bones class declaration: class My. Class { } you can also add modifiers before the class declaration. Modifiers falls into two category. Access Modifier : public private protected Non-Access Modifier : strictfp, final, and abstract

Classes Class Access What does it mean to access a class ? Class :

Classes Class Access What does it mean to access a class ? Class : A 1) Create an instance of class B 2) Access certain methods & varia. Bles within class B, depending on The access control of those methods And variables 3) Extends class B When we say class A Has to access another Class B. it means Class A can do Three things Class : B class B { public int get. Size() class { B {} class A {28; return class A extends B{ public static void main(String args[]) }{ { extends B{ class BAb=new B(); //creating instance of B public static void main(String args[]) } {} } A a=new A(); } System. out. println(a. get. Size()); } }

Classes Default Access The superclass Beverage is on different package from sub class(Tea). the

Classes Default Access The superclass Beverage is on different package from sub class(Tea). the import This can be clear by example below : What’s the Solution statement at the top of the Tea file is trying to import the Beverage class. The package cert; Beverage class compile fines, but when class Beverage{} we try to compile Tea class we will get Can't access class cert. Beverage. following Class or interface must be public, in same package, or an accessible member class. import cert. Beverage; package stuff; import cert. Beverage; class Tea extends Beverage { }

Classes Default Access This can be clear by example below : package cert; public

Classes Default Access This can be clear by example below : package cert; public class Beverage{} Make your class public This means that any package Outside can inherit this class Think of default access as package-level access, because a class with default access can be seen only bypackage classesstuff; within the same package. import cert. Beverage; class Tea extends Beverage { }

Classes Public Access This can be clear by example below : package cert; public

Classes Public Access This can be clear by example below : package cert; public class Beverage{} package stuff; import cert. Beverage; class Tea extends Beverage { } Make your class public This means that any package Outside can inherit this class

Classes Other (Nonaccess) Modifiers strictfp is a keyword and can be used to modified

Classes Other (Nonaccess) Modifiers strictfp is a keyword and can be used to modified a class or a method, but never a variable. 1. 2. Marking a class as strictfp means that any method code in the class will conform IEEE 754 standard rules for floating points. If you mark methods as strictfp you can get behavior on method by method basis.

Classes Other (Nonaccess) Modifiers final The final keyword means the class can’t be subclassed.

Classes Other (Nonaccess) Modifiers final The final keyword means the class can’t be subclassed. Example : We get error : class B extends A final class A Can’t subclass final classes: {B class { } int size; extends A { 1 error public void set. Size(int n) { Size=n; } }

Classes Other (Nonaccess) Modifiers Abstract classes An abstract class can never be instantiated. its

Classes Other (Nonaccess) Modifiers Abstract classes An abstract class can never be instantiated. its sole purpose, mission in life is to be extended. Example : - Look Another. Class. java: 7: at the method class method Car is isanended abstract However if you ever the try to instantiate thiswith You also concrete method class. Semicolon, this It another can't be shows instantiated. the class thatinside extended This can code willuse compile fine Code in body of code , you will getany Abstract class. Car This xclass = new must Car(); define the like method Compiler error something this: inside the 1 error Car. abstract class Car{ private double price; private String model; private String year; public abstract void go. Fast(); public abstract void go. Up. Hill(); public abstract void impress. Neighbors(); // Additional, important, and serious //code goes here }

Classes Other (Nonaccess) Modifiers Abstract classes Points to remember : 1. if even single

Classes Other (Nonaccess) Modifiers Abstract classes Points to remember : 1. if even single method is abstract , the whole class must be declared abstract. 2. You can add non-abstract method in an abstract class. 3. You can’t mark a class both abstract and final.

Classes Exercise The following exercise will test your knowledge of public, default, final, and

Classes Exercise The following exercise will test your knowledge of public, default, final, and abstract classes. Create an abstract superclass named Fruit and a concrete subclass named Apple. The superclass should belong to a package called food and the subclass can belong to the default package (meaning it isn't put into a package explicitly). Make the superclass public and give the subclass default access. 1. Create the superclass as follows: package food; public abstract class Fruit{ /* any code you want */} 1. Create the subclass in a separate file as follows: import food. Fruit; class Apple extends Fruit{ /* any code you want */}

Classes Exercise(Cont. ) 3. 4. Create a directory called Food off the directory in

Classes Exercise(Cont. ) 3. 4. Create a directory called Food off the directory in class path setting. Attempt to compile the two files. If you want to use APPLE class, make sure you place the Fruit. class file in the food subdirectory.

Interface An interface is a contract. class Tire implements Bounceable{ public void bounce(){…. .

Interface An interface is a contract. class Tire implements Bounceable{ public void bounce(){…. . } public void set. Bounce. Factor(){…. . } } When you create an interface , you’re defining a contract for what a class can do , without saying anything about how the class will do it. Interface can be implemented by any class , from any inheritance What tree. this lets you take different radical classes and gives Implementing them a common characteristics. Interface Bounceable void bounce(); void set. Bounce. Factor(); What you declare class must do. (All interface Interface Bounceable must be implemented public void bounce(); and marked publicvoid set. Bounce. Factor(); public. What Compiler see

Interface Point to Remember Because interface methods aremust abstract, they cannot be Interface methods

Interface Point to Remember Because interface methods aremust abstract, they cannot be Interface methods must not be static. Think All variables interface of interface defined methods as in an an are 100 -percentage interface implicitly public abstract beand public, abstract. class. static, In and other final marked final, strictfp, native. (More on these words, —in other you words, do notinterfaces need tooractually can declare type only the public constants, and not abstract instance modifiers variables declaration, but method modifiers later. ). in the method abstract void bounce(); //ends with a semicolonthe rather is still always public and abstract. //than curly braces Interface An An interface types cannot can extend be extend implement used oneanything or with more another but Aninterface must be declared the other another polymorphically(more interfaces. interface. or class. on this later) keyword interface.

Interface An interface is a contract. When you create an interface , you’re defining

Interface An interface is a contract. When you create an interface , you’re defining a contract for what a class can do , without saying anything about how the class will do it. Interface can be implemented by any class , from any inheritance tree. this lets you take different radical classes and gives them a common characteristics.