You are on page 1of 17

JAVA PROGRAMMING LANGUAGE

Chapter one

Object and Class


OOP
OOP stands for Object-Oriented Programming.
Object Oriented Programming is a technique in which programs are written on the
bases of Object, where it adopts a concept that views everything as an object and
implements a logic a bout it.

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

So, a class is a template for objects, and an object is an instance of a class.


For example: in real life, a car is an object. The car has attributes, such as
weight and color, height and methods, such as drive and brake.
Create a Class
Syntax to create a class:
class <class-name>
{
// body of the class
}

Example:
public class MyClass
{
int x = 5;
}

Remember that a class name should always start with an uppercase first letter.
Create an Object

In Java, an object is created from a class.


To create an object of class specify the class name, followed by the object
name, and use the keyword new.

Syntax to create object:


class-name object-name = new class-name();
Create an Object

Example
Create an object called "myObj" and print the value of x:

public class MyClass


{ int x = 5;
public static void main(String[] args)
{
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}}
Multiple Objects
You can create multiple objects of one class:

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 define an objects behavior, we create methods, a set of java


statements that accomplishes some task.
Accessing Attributes
You can access attributes by creating an object of the class, and by using the dot
syntax (.):
The following example will create an object of the MyClass class, with the
name myObj. We use the x attribute on the object to print its value:
Example
public class MyClass
{ int x = 5;
public static void main(String[] args)
{
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}}
Java Inner Classes

In Java, it is also possible to nest classes (a class within a class).

The purpose of nested classes is to group classes that belong


together, which makes your code more readable and maintainable.

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

You might also like