Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for."— Presentation transcript:

1 Object Oriented Programming in Java

2 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.

3 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.

4 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.

5 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 :

6 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.

7 A motorcycle class might have some behavior like: Start the engine Stop the engine Speed up Change gear Stall Example :

8 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.

9 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.

10 class Motorcycle { String make; String color; boolean enginestate = false; void startEngine() { if (enginestate = = true) Example of the Motorcycle Class

11 System.out.println (“The engine is already on”) else { enginestate = true; System.out.println ( “The engine is now on”); }

12 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

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

14 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

15 else { enginestate = true; System.out.println ( “The engine is now on”); } void showAtts () { System.out.println (“This motorcycle is a” +color+ “ ” +make);

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

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

18 m.showAtts(); System.out.println (“………………”); System.out.println(“Starting Engine …”); m.startEngine(); } }

19 The three major concepts in OOP are: Encapsulation Inheritance Polymorphism

20 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.

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

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

23

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

25 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.

26 Example of Inheritance class CommercialFlight extends Flight { // extra members in CommercialFlight int flightNumber; int passengers; }

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

28 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.

29 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.


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

Similar presentations


Ads by Google