Professional Documents
Culture Documents
Chapter one
Advantages of OOP:
OOP is faster and easier to execute
It provides a clear structure for the programs
It makes it possible to create full reusable applications with less code and
shorter development time.
Concepts of OOP
Object
Object is an instance of class. An object is nothing but a self-contained
component which consist of methods and attributes to make a particular type of
data useful.
Class
In java programming the class is defined as a blueprint from which objects are
created. A class is a blueprint that defines the variables and the methods common
to all objects of a certain kind.
A simple java program
public class FirstProgram
{
public static void main (String [] args)
{
System.out.println(“Welcome to Java world”);
}
}
Classes and Objects
Classes and objects are the two main aspects of object-oriented programming.
Look at the following illustration to see the difference between class and objects:
Class Objects
Fruit Apple
Banana
Mango
Example:
public class MyClass
{
int x = 5;
}
Remember that a class name should always start with an uppercase first letter.
Create an Object
Example
Create an object called "myObj" and print the value of x:
Example
Create two objects of MyClass:
public class MyClass
{ int x = 5;
public static void main(String[] args)
{
MyClass myObj1 = new MyClass(); // Object 1
MyClass myObj2 = new MyClass(); // Object 2
System.out.println(myObj1.x);
System.out.println(myObj2.x);
}}
Attributes And Behavior
Every class you write in java has two basic features : Attributes and Behavior.
Attributes:
The attribute of an object are variables that hold information, or data, about the object.
Attributes are the individual things that differentiate one object from another and
determine the appearance, state, or other qualities of that object.
For the class Motorcycle we have attributes:
Color: red, green, silver
Style: Sport Bike, Standard
Make: Honda, BMW
Behavior
A class’s behavior determines how an instance of that class operates,
for example of Motorcycle class, some behaviors that the motorcycle
class might have are:
Start the engine
Stop the engine
Speed up
Change gear
To access the inner class, create an object of the outer class, and
then create an object of the inner class.
Java Inner Classes
Example
class OuterClass {
int x = 10;
class InnerClass {
int y = 5;
}}
public class MyMainClass {
public static void main(String[] args) {
OuterClass myOuterObj = new OuterClass();
OuterClass.InnerClass myInnerObj = myOuterObj.new InnerClass();
System.out.println(myInnerObj.y + myOuterObj.x);
}}
Static Inner Class
An inner class can also be static, which means that you can access it without creating an object of
the outer class:
Example
class OuterClass {
int x = 10;
static class InnerClass {
int y = 5; }}
public class MyMainClass {
public static void main(String[] args) {
OuterClass.InnerClass myInner = new OuterClass.InnerClass();
System.out.println(myInner.y); }}
Note: just like static attributes and methods, a static inner class does not have access to members of
the outer class.
End of Chapter