29
Object Oriented Programming in Java

Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

Embed Size (px)

Citation preview

Page 1: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

Object Oriented Programming in Java

Page 2: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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.

Page 3: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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.

Page 4: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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 .

Page 5: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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 :

Page 6: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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.

Page 7: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

A motorcycle class might have some behavior like:

• Start the engine

• Stop the engine

• Speed up

• Change gear

• Stall

Example :

Page 8: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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.

Page 9: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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.

Page 10: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

class Motorcycle {

String make;

String color;

boolean enginestate = false;

void startEngine() {

if (enginestate = = true)

Example of the Motorcycle Class

Page 11: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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

else {

enginestate = true;

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

}

}

}

Page 12: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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

Page 13: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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(); }

Page 14: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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

Page 15: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

else {

enginestate = true;

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

}

}

void showAtts () {

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

Page 16: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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”;

Page 17: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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…”);

Page 18: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

m.showAtts();

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

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

m.startEngine(); } }

Page 19: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

The three major concepts in OOP are:

• Encapsulation

• Inheritance

• Polymorphism

Page 20: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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.

Page 21: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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; }

Page 22: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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); } }

Page 23: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for
Page 24: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

Inheritance

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

Page 25: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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.

Page 26: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

Example of Inheritance

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

Page 27: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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

Page 28: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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.

Page 29: Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for

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.