You are on page 1of 11

Class And Object

■ 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, he 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.
• A class can contain any of the following variable
types.
• Local variables: Variables defined inside methods,
constructors or blocks are called local variables.
The variable will be declared and initialized within
the method and the variable will be destroyed
when the method has completed.
• Instance variables: Instance variables are variables
within a class but outside any method. These
variables are instantiated when the class is loaded.
Instance variables can be accessed from inside any
method, constructor or blocks of that particular
class.
• Class variables: Class variables are variables
declared with in a class, outside any method, with
the static keyword.
Identifiers and Keywords
• All the Java components we just talked about—classes, variables, and methods—
• need names. In Java these names are called identifiers, and, as you might expect,
• there are rules for what constitutes a legal Java identifier. Beyond what's legal,
• ■ 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.
• 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 7g;
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 "camelCase"). For
classes, the names should typically be nouns. For example:
Dog
Account
PrintWriter
For interfaces, the names should typically be adjectives like
Runnable
Serializable
■ Methods The first letter should be lowercase, and then normal camelCase
rules should be used. In addition, the names should typically be verb-noun
pairs. For example:
getBalance
doCalculation
setCustomerName
■ Variables Like methods, the camelCase format should be used, starting with
a lowercase letter. Sun recommends short, meaningful names, which sounds
good to us. Some examples:
buttonWidth
accountBalance
myString
■ 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
Source File Declaration Rules
Before we dig into class declarations, let's do a quick review of the rules associated
with declaring classes, import statements, and package statements in a source file:
■ There can be only one public class per source code file.
■ Comments can appear at the beginning or end of any line in the source code file.
■ 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.
■ 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.
■ 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.
■ 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.
■ A file can have more than one non public class.
OOPS Concept
• Polymorphism gives us the ultimate flexibility in extensibility. The ability
to define more than one function with the same name is called
Polymorphism. In java, c++ there are two type of polymorphism: compile
time polymorphism (overloading) and runtime polymorphism (overriding).
• Inheritance is the property which allows a Child class to inherit some
properties from its parent class. In Java this is achieved by using extends
keyword. Only properties with access modifier public and protected can
be accessed in child class. Java does not allow to extend multiple classes
but to overcome this problem it allows to implement multiple Interfaces.
• Abstraction is way of converting real world objects in terms of class. For
example creating a class Vehicle and injecting properties into it. E.g
• public class Vehicle {
• public String colour;
public String model;
}
Abstraction
• Hiding internal details and showing functionality is known as
abstraction. For example: phone call, we don't know the
internal processing.
• In java, we use abstract class and interface to achieve
abstraction.
Encapsulation:-
• Binding (or wrapping) code and data together into a
single unit is known as encapsulation. For example:
capsule, it is wrapped with different medicines.
• A java class is the example of encapsulation. Java bean
is the fully encapsulated class because all the data
members are private here.
• The encapsulation is achieved by combining the methods and attribute into a
class. The class acts like a container encapsulating the properties. The users
are exposed mainly public methods.
• class Test {
• private String name;
• private int marks;
• public String getName() {
• return name;
• }
• public void setName(String name) {
• this.name = name;
• }
• public int getMarks() {
• return marks;
• }
• public void setMarks(int marks) {
• this.marks = marks;
• }
• }
Modifiers
• Access modifiers: default , public, protected, private.
• Non-access modifiers (including strictfp , final, and abstract).
• The four Access modifiers are:
• Visible to the package. the default. No modifiers are needed.
• Visible to the class only (private).
• Visible to the world (public).
• Visible to the package and all subclasses (protected).
• Default Access Modifier - No keyword:
• Default access modifier means we do not explicitly declare an access
modifier for a class, field, method, etc.
• A variable or method declared without any access control modifier is
available to any other class in the same package.
package pkg1;
class Demo {
private int i=0;
public int j=1;
protected int k=2;
int l=3;
//all instance variables are accessible anywhere in this class.
}
package pkg1;
class Test{
public static void main(String[] arg) {
Demo d=new Demo();
System.out.println(“i=="+d.i);//Error can to be accessible private variable i.
System.out.println(“j=="+d.j);//Fine
System.out.println(“k=="+d.k);//Fine
System.out.println(“l=="+d.l);//Fine
}
}
To break privacy:-
Test protectedPrivacy=new Test();
Method method = protectedPrivacy.getClass().getDeclaredMethod(“anyMethodNameOfTestclass", null);
method.setAccessible(true);
Object result = method.invoke(protectedPrivacy);
System.out.println(result.toString());
package pkg2;
import pkg1.Demo;
class Test{
public static void main(String[] arg) {
Demo d=new Demo();
System.out.println(“i=="+d.i);//Error can not to be accessible private variable i.
System.out.println(“j=="+d.j);//Fine
System.out.println(“k=="+d.k);//Error protected members are not available with object from outside the .
System.out.println(“l=="+d.l);//Error.
}
}
package pkg2;
import pkg1.Demo;
class Test extends Demo{
public static void main(String[] arg) {
Demo d=new Demo();
System.out.println(“k=="+d.k);//Error protected members are not available with object from outside the //package. For
a subclass outside the package, the protected member can be accessed only
//through inheritance.
}
void onlyWayToAccess(){
System.out.println(“k=="+k);//Fine}}}
class Test2{//another class in pkg2
void show(){
Demo dm=new Demo();
System.out.println(dm.k);//Error k is protected var of Demo’s superclass so k will behave as a private var for Demo
//Class.
}
}

You might also like