My Notes on OCPJP(SCJP) – Chapter 1

SA,

I’m Currently Preparing for OCPJP (SCJP) Exam and i thought it might be useful to post my Notes on every chapter of  Kathy Sierra and Bert Bates book  “SCJP Sun Certified Programmer for Java 6 Exam 310-065” , you can not study from these notes, you must read every chapter from the book, these notes for everyone who wants to make a quick review on every chapter, i will post 10 posts (1 post/chapter),and here is the first Chapter of the Series,

Chapter 1: Declarations and Access Control

Instance Variables: Variables defined inside a class that each Object have a copy of it.

There is Three Types of Identifiers :

1) Legal Identifiers:

a) Starts with $,_,Letters but can’t start with a Number, after the first letter we   can add Number to Identifier.

b)They are Case-Sensitive.

c) No limit to Number of Characters an identifier can Contain.

2) Sun Code Conventions:

a) with Classes /Interfaces: class Name Must Start with a Capital Letter.

b) with Functions/Methods:  method name must start with a small letter.

c) with variables : same as methods must start with a small letter.

d) with Constants: all Constant Characters Must be CAPITAL.

3) Java Beans Standards :

a) Functions that doesn’t return boolean called Getters and must start with get for example : getSize()

b) Functions that return Boolean may start with is/get but “is” is perfered isValid()

c) Function that set values must start with set called Setters setSize()

d) Setters Must be Void and Accept Parameters.

e) Getters Must have a return type and doesn’t have Any-Parameters.

f) for Action Listeners it must be on this format to add (addActionListener) must start with add and ends with listener and removeActionListener the same.

Writing Classses:

General Information about Classes:

1) there is only one Public class in each java file and file name must be the same as public class name.

2) there can be more than one non-public classes in the same java file.

3) if there is no  public class in java file, then file name can be any name other than non-public classes names.

4) if this class included in a package you write package name in the first line of the source code.

5) all Imports located between package and class declaration.

6) if there is more than one class in java file then all imports and package declaration applies to all of them.

Class Modifiers:

1) Access Modifiers: public  – Default only (there is no private – protected for calss modifiers )

2) Non-Access Modifiers: strictfp – final- abstract

Access Modifiers :

Default: Default is Package-Level Access, default tdo

Public : Can be Accessed Any Where in the Application, just locate it using import.

Non-Access Modifiers: you can use Non-Access modifiers beside access Modifiers but you can’t use both of non-access or access modifiers in the same time you can’t say final abstract class !

Strictfp: (Strict Float Point) :  this modifier is not needed for exam but for knowledge it means that float results is not machine-dependant, it will result according to IEEE 754 rules.

Final: final Class Means two things :

1) this class Can’t be Sub-Classed.

2) this class methods can’t be Overriden. which means no body can edit this class.

Abstract: abstract class means it must be Extended, you can’t make instance   of it, it is made to be extended, if there is only one method abstract, the whole class must be abstract.

Interfaces :

Interface Declaration :  Interfaces are always abstract you can choose between public and default access, whether you wrote abstract or not it is always abstact.

Methods Declaration: Methods in Intefaces are always Public and abstract whether you wrote it or not in method declaration.

Variables Declaration : variables are always  public final static whether you wrote it or not.

1) Interface can only Extend another inteface.

2) if you Implemented in your Class  then you must implement all Abstract methods.

Class Members (Functions & Variables) Access Modifiers :

Public: means Class Member can be access from Any where even if the Class Object Declared in a Different Package, and also extended classes can access them as well, if you don’t use dot Operator to refer to class Members then that means this Class Member to the Class you are Seeing now, you can use also this keyword to refer to Current Class Members, this refers to Current Executing Object Members.

Private: Means no Other Class Can use this Class Member even SubClasses, so what if we have a private method name getIt() in a super class and another method with same name getIt() in the sub-class can we call this Override?    The answer is no, there is no override, the function in the subclass is just another Function that have the Same method name as SuperClass private Method getIt() , the sub-class is not supposed to see the private members of super class (it doesn’t inherit private Members).

Protected & Default : Protected and Default access modifiers are much similar both are (Package Level), only one Difference between them, Protected can be also Accessed through sub-class even if the Sub-Class in Another Package.

So Can Access Modifiers Applied to Local-Variables?

No, you can’t apply access modifiers to Local-Variables, for example we can’t say:

class Foo {
void doStuff() {
private int x = 7;
this.doMore(x);
}
}

there is only one Modifier you can apply to local Variables and it is final .

Final Arguments: there can be Final Arguments in Method Arguments ex:

public void RegisterPerson (final int id , String name);

here final keyword means that id variable can’t be Modified through function operations.

Abstract Methods: abstract methods are method that have “abstract” keyword in methods declaration, abstract methods are methods without body, which means abstract method à abstract class whenever there is abstract method the whole class becomes abstract and you can’t create object of that class, it Must be Extended and the Sub-Class must implement all abstract Methods if the Sub-Class didn’t implement any of the abstract methods then the Sub-Class becomes abstract as well.

Final Methods: Final Methods means that you can’t Override this Method through Inheritance, you can Extend the class as long as it is not Final Class but you can’t Override Methods Only with Final Keyword.

Methods with Variable Argument List (var-args): we use var-args when you have a method and you don’t know the number of parameters and you can’t declare array because you already don’t know the size, so the solution is to use var-args here is the Syntax of var-args :    public void addNumbers( int… x)                    int three dots then space then parameter name, you can have other parameters beside var-args but var-args must be the last Parameter, you can loop through x (array) to get entered values.

Constructor Declarations: Constructor Must have the same class name, every class must have a constructor if you build it, the compiler will build one for you, Constructor can’t be final, static and Abstract

Final Variables: variables declared with keyword “final” are called Final Variables, it can’t be re-initialized once it has been initialized.

Enumerations: An enum specifies a list of constant values assigned to a type.

An enum is NOT a String or an int; an enum constant’s type is the enum

type. For example, SUMMER and FALL are of the enum type Season.

An enum can be declared outside or inside a class, but NOT in a method.

An enum declared outside a class must NOT be marked static, final,

abstract, protected, or private.

Enums can contain constructors, methods, variables, and constant class bodies.

MyEnum.values() returns an array of MyEnum’s values.

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s