Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented...

Post on 14-Dec-2015

238 views 4 download

Transcript of Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented...

Object Oriented Programming in Java

Object Oriented Programming Concepts in Java

Object oriented Programming is a paradigm or organizing principle for software, where the program is structured according to the information it manipulates, rather than the operations it does. Data, not procedures, shape the program.

What is an Object ?

An Object is defined as a software package that incorporates a set of related data and all the functions needed to manipulate this data. The related set of data, the data members, captures the state of an object.

Defining Objects : STATE (Attributes)

Encompasses all of the properties of an object. Each property has a value. All objects of the same type have same properties ( although they may have different values).

Properties are implemented with Object Variables or instance variables .

A motorcycle class might include the following attributes and have these typical values:

• color : red, green, silver, brown

• style : cruiser, sport bike, standard

• Make : Honda, BMW, Bultaco

Here color, style, make are the instance variables.

Example :

Defining Objects : BEHAVIOR

Is how an object reacts, in terms of state changes and interaction with the other objects.

Behavior is the only way objects can do anything to themselves or have anything done to them.

A motorcycle class might have some behavior like:

• Start the engine

• Stop the engine

• Speed up

• Change gear

• Stall

Example :

Defining Objects : BEHAVIOR

The behavior is defined by the use of functions which are confined to a Class and are called member functions or Methods.

Classes

A class is a set of objects that share the same properties and behavior.

Its where state and behavior of objects is defined through Variables and Methods.

Example:

Every Motorcycle is defined in the Motorcycle class.

class Motorcycle {

String make;

String color;

boolean enginestate = false;

void startEngine() {

if (enginestate = = true)

Example of the Motorcycle Class

System.out.println (“The engine is already on”)

else {

enginestate = true;

System.out.println ( “The engine is now on”);

}

}

}

public static void main (String args[] )

{

Motorcycle m = new Motorcycle();

m.make = “Yamaha RZ350”;

m.color = “Yellow”;

System.out.println(“Calling showAtts…”);

m.showAtts();

The main() method for Motorcycle class

System.out.println (“………………”);

System.out.println(“Starting Engine …”);

m.startEngine();

System.out.println (“………………”);

System.out.println(“Calling showAtts…”);

m.showAtts();

System.out.println (“………………”);

System.out.println(“Starting Engine …”);

m.startEngine(); }

class Motorcycle {

String make;

String color;

boolean enginestate = false;

void startEngine() {

if (enginestate = = true)

System.out.println (“The engine is

already on”)

The final version of Motorcycle.java file

else {

enginestate = true;

System.out.println ( “The engine is now on”);

}

}

void showAtts () {

System.out.println (“This motorcycle is a” +color+ “ ” +make);

if (engineState = = true)

System.out.println (“The engine is on.”);

else System.out.println (“The engine is off.”);

}

public static void main (String args[] ) {

Motorcycle m = new Motorcycle();

m.make = “Yamaha RZ350”;

m.color = “Yellow”;

System.out.println(“Calling showAtts…”);

m.showAtts();

System.out.println (“………………”);

System.out.println(“Starting Engine …”);

m.startEngine();

System.out.println (“………………”);

System.out.println(“Calling showAtts…”);

m.showAtts();

System.out.println (“………………”);

System.out.println(“Starting Engine …”);

m.startEngine(); } }

The three major concepts in OOP are:

• Encapsulation

• Inheritance

• Polymorphism

Encapsulation

Embedding both data and code into a single entity.

•Allows us to use data hiding which is a way to prevent direct access to the variables in an object.

• Separates the interface to the class from its implementation.

Class Flight { int altitude; private int heading; int speed; float latitude; float longitude; // change the flight’s heading by angle degrees void turnFlight (int angle) { heading = (heading + angle) % 360; // make sure angle is in the range 0-359 degrees if (heading < 0) heading = heading + 360; }

void setHeading (int angle) { heading = angle % 360; // make sure angle is in the range 0-359 degrees if (heading < 0) heading = heading + 360; }int getHeading( ) { return heading; }// print information about the flight void printFlight () { System.out.println(altitude + “ / ” + heading + “ / ” + speed); } }

Inheritance

Inheritance is the process by which one object acquires the properties of another object.

Benefits of Inheritance

•Subclasses provide specialized behaviors from the basis of common elements provided by the superclass. Through the use of inheritance, programmers can reuse the code (in the superclass) many times.

• Programmers can implement superclasses called abstract classes that define “generic” behaviors. The abstract superclass defines and may partially implement the behavior but much of the class is undefined and unimplemented. Other programmers fill in the details with specialized subclasses.

Example of Inheritance

class CommercialFlight extends Flight {// extra members in CommercialFlightint flightNumber;int passengers;}

The Commercial Flight Class inherits member variables and functions from Flight, and then adds its own member variables.

Polymorphism

Polymorphism is the ability of a single function name to be used to operate on many different types. It is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation.

Polymorphism

• We can deal with objects without the need to know what exact class they belong to.

• This is an extension of the inheritance concept.