Top Banner
Class Member
25

Class Member

Dec 22, 2014

Download

Education

Menghok Heak

Understanding:
- Class member
- Access modifier
- Static/Final keyword
- IS-A / HAS-A Relationship
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Class Member

Class Member

Page 2: Class Member

Introduction

Member : Leang BunrongChea SocheatHeak MenghokChoun SreynavBuoy Sodeth

Page 3: Class Member

Content

• Access Modifier• Static and Final Keyword• Designing Class ( IS-A and HAS-A )

Page 4: Class Member

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

Page 5: Class Member

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

Page 6: Class Member

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";

}}

Page 7: Class Member

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;}

}

Page 8: Class Member

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)

{//……

}

Page 9: Class Member

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

Page 10: Class Member

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.)

Page 11: Class Member

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.)

Page 12: Class Member

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.)

Page 13: Class Member

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.)

Page 14: Class Member

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.)

Page 15: Class Member

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.)

Page 16: Class Member

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.)

Page 17: Class Member

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.)

Page 18: Class Member

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.)

Page 19: Class Member

IS-A and HAS-A

Class Car

Class Audi Class EngineHAS-A

IS-A

Page 20: Class Member

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’

Page 21: Class Member

IS-A ( cont )

Example:// Class Car is a super Classpublic class Audi extends Car{

//……}

Note: Audi is a CarCar is not a Audi

Page 22: Class Member

• 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

Page 23: Class Member

IS-A and HAS-A ExampleExample:

class Engine{//……

}class Audi extends Car{ // IS-A Relationship

Engine engine = new Engine(); // HAS-A Relationship

//…….

}

Page 25: Class Member

Thank you!