Class Member
Dec 22, 2014
Class Member
Introduction
Member : Leang BunrongChea SocheatHeak MenghokChoun SreynavBuoy Sodeth
Content
• Access Modifier• Static and Final Keyword• Designing Class ( IS-A and HAS-A )
Access ModifierDefault Access Modifier - No keyword
• do not explicitly declare an access modifier. • can be accessed to any other class in the same package• Also known as package-private
Example:int x=5;boolean click(){
return true;}
http://www.tutorialspoint.com/java/java_access_modifiers.htm
Access Modifier (Cont)Private Access Modifier – private
• The private modifier specifies that the member can only be accessed in its own class.
• can be accessed outside the class by using Accesor method.
Example: public class transfer {
private String format; public String getFormat() {
return this.format; } public void setFormat(String format) {
this.format = format; }
http://www.tutorialspoint.com/java/java_access_modifiers.htm
Access Modifier (Cont)Protected Access Modifier – protected
• can be accessed only by the subclasses in other package or any class within the same package
• The same Packagepackage food;public class Pizza {protected String size="”;}public class Burger extends Pizza {public static void main(String[] args) {
Burger bsize=new Burger();bsize.size = "small";
}}
Access Modifier (Cont)• subclasses in other package
package directfamily;public class Husband {public String houses;protected float money;protected String Block;protected String gold;}package undirectfamily;import directfamily.Husband;public class SecondWife extends Husband {public static void main(String[] args) {
SecondWife get=new SecondWife();get.Block="1 hecta";get.money=100.8f;}
}
Access Modifier (Cont)
Public Access Modifier – public• can be accessed to any other class belong to Java
Universe.Example:public static void main(String[] args)
{//……
}
a.Final KeywordThe final keyword in java is used to declare a
field as final i.e. it prevents its content from modified. The final keyword can be used in many context. The final keyword applicable to:
1.VariableThe final keyword can be applied
with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only.
Final and Static keyword
class Book {final int qty;final static double price; Book(){
qty = 9000;}static{
price = 99.99;}public static void main(String[] myargs) {
Book b =new Book();}
}
Final and Static keyword(cont.)
2. Method
A Method declared as final can be inherited but you cannot override (redefine) it.class Book {
String name="";final void setBookName(String name){
this.name = name;}
}class MyBook extends Book { //Can’t Override method Book
void setBookName(String name){this.name = name;
}
}
Final and Static keyword(cont.)
A class can also be declare as final. A class declare as final cannot be inherited. String class in java.lang package is an example of final class.
final class Book{………………
} class SokBook extends Book{ //can’t
inherited from class Book
……………… }
3. Class
Final and Static keyword(cont.)
b. Static KeywordThe static keyword is used in java mainly
for memory management. The static keyword belongs to the class than instance of the class.The static keyword applicable to:
1. Variable(also known as class variable)2. Method(also known as class method)3. Block4. Nested class
Final and Static keyword(cont.)
1.Variable (also known as class variable)static variable defined as a class
member the class, not individual object of the class. The static keyword may come before or after the access modifier.
public static int a;static public int b;
class Employee {int id; int salary;
static int total; ...}
Example:
Final and Static keyword(cont.)
Static method do not need instance of its class for being accessed. main() method is the most common example of static method main() is declared as static because it is called before any object of the class is created. A 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 access static data member and
can change the value of it.
2. Method
Final and Static keyword(cont.)
class Book {public static int ChangeQty(int b){
return b + 50;}public static void main(String[] args) {System.out.println(Book.ChangeQty(50));}
}
Example:
Final and Static keyword(cont.)
Static block:- Is used to initialize the static data member.- It is executed before main method at the time of class loading.
class Book {static int qty;final static double price; static{
qty = 10;price = 99.99;
}}
3. Block
Final and Static keyword(cont.)
4. Nested classNested classes are divided into two
categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes. (Next Section)class Book {
……………static class Paper{
………………}
}
Final and Static keyword(cont.)
IS-A and HAS-A
Class Car
Class Audi Class EngineHAS-A
IS-A
IS-A
• Inheritance• One class is a subclass of another class• Using keyword “extends”• Example: We have a class ‘Car’ and a class
‘Audi’ which extends from ‘Car’.So we can say that ‘Audi’ is-a ‘Car’
IS-A ( cont )
Example:// Class Car is a super Classpublic class Audi extends Car{
//……}
Note: Audi is a CarCar is not a Audi
• Composition• One object belong to another object• Using instance variables that refer to other
objects • Example: We have another class ‘Engine’.
‘Audi’ car need an engine, a steering.So we can say ‘Audi’ has-a ‘Engine’. ‘Audi’ has-a ‘Steering’.
HAS-A
IS-A and HAS-A ExampleExample:
class Engine{//……
}class Audi extends Car{ // IS-A Relationship
Engine engine = new Engine(); // HAS-A Relationship
//…….
}
References
• http://www.javatpoint.com/static-keyword-in-java
• http://www.informit.com/articles/article.aspx?p=349047
• http://www.geeksforgeeks.org/static-class-in-java/
• http://www.studytonight.com/java/inheritance-in-java.php
• http://learnwithharsha.com/my-first-java-lesson-is-a-and-has-a-relationships/
Thank you!