Download Java Classes & Object Oriented Programming and more Schemes and Mind Maps Object Oriented Programming in PDF only on Docsity! Object Oriented Programming 1 Java Classes & Object Oriented Programming Introduction Object Oriented Programming 2 Object Oriented Programming • One of the first applications of modern computing was modeling and simulation. • Scientists soon realized that functions alone were insufficient to model systems intuitively • If we are going to model a planet we would like to actually create a virtual planet, define how it behaves in our simulated universe, and then just observe it. Object Oriented Programming 5 Object Oriented Programming • We can pattern software after a group of specialists at a company working on a problem. • For example, there are two objects we have used – System.out and System.in. • System.in is the name of an object who knows all about reading data from the keyboard and putting it into a variable. • It is easier to ask System.out to do the work than write a program to do it ourselves. Object Oriented Programming 6 Object Oriented Programming • Important: we don’t have to have any idea how System.out does its job. We just trust that it does. • Just like we don’t question the US Mail about how our letter gets from here to Seattle. • We only care that it arrives within certain tolerances – not how it got there. • This is called abstraction, information- hiding, and encapsulation and we like it! Object Oriented Programming 7 Object Oriented Programming • When we mail a letter all we have to worry about is following the correct protocol to ensure our letter gets to the right place. • We have to know where to go, how to pay, the format expected for the destination address and return address, etc. • In software this protocol is called the interface. • All objects have to have an interface that clearly defines how we can interact with the object. Object Oriented Programming 10 Reasons for OOP Abstraction Encapsulation Information hiding Inheritance Software Engineering Issues Object Oriented Programming 11 Class: Object Types • Java uses classes and structures to define objects • A Java class is an object type. • When you create the definition of a class you are defining the attributes and behavior of a new type. – Attributes are data members. – Behavior is defined by methods. Object Oriented Programming 12 Creating an object • The interface acts as a contract specifying how the object will behave – as long as the code fulfills the contract we don’t care how it works. • Defining a class does not result in creation of an object. • Declaring a variable of a class type creates an object. You can have many variables of the same type (class). This is called instantiation of the class, i.e. we create an instance of the object. Object Oriented Programming 15 Private vs. Public • Classes define certain parts of the object to be public, private, or protected. • Public parts of the object can be used by anyone who has access to the object. • The private parts of the object are for the objects internal use only. • Protected parts are accessible from outside the object only under certain circumstances. • Try to make as much private as possible. Object Oriented Programming 16 Special Member Functions • Constructors: called when a new object is created (instantiated). – can be many constructors, each can take different arguments • Garbage Collection. This is why Java is so popular. Object Oriented Programming 17 Anatomy of a Class public class Dog { Dog( String dog_name ){ name = dog_name; } public void bark(){ System.out.println(“woof”); } public string getName() { return name } private String name; } Put all this in D og.java Object Oriented Programming 20 Accessing class methods • Within other class methods, a method can be called just like a function. • Outside the class, public methods can be called only when referencing an object of the class. Object Oriented Programming 21 Classes and Files Each class definition goes in it’s own .java file. Give the file the same name as the class. Java can automatically find the class definition this way. Object Oriented Programming 22 Classes and Objects • It is essential to understand the difference between a class and the corresponding object. • A class defines the properties of the object (methods, data members, name). • When you use the “new” keyword you instantiate the class. • This means you tell java to reserve some memory in the computer to store data required by the class.