object oriented programming: java edition by: samuel robinson

19
Object Oriented Programming: Java Edition By: Samuel Robinson

Upload: katherine-garrett

Post on 12-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented Programming: Java Edition By: Samuel Robinson

Object Oriented Programming:Java Edition

By: Samuel Robinson

Page 2: Object Oriented Programming: Java Edition By: Samuel Robinson

Intro

In order for a language to be Object Oriented, it must support abstract data types, inheritance, and dynamic binding. Along with these 3 features, the language should also support classes, objects, instances, abstraction, encapsulation, polymorphism, etc. basically the fundamental features of OOP. Java has these.

Page 3: Object Oriented Programming: Java Edition By: Samuel Robinson

Classes and Objects

All Java classes are either subclasses of the Object root class or a class that is descendant of Object.

Java supports objects and non object data. Enumerations and Arrays are objects, but the values of primitive scalar types are not. All objects are explicit heap dynamic.

Page 4: Object Oriented Programming: Java Edition By: Samuel Robinson

Object Exclusivity

• Primitive types such as int and double are not objects, but it is possible to manipulate them like objects.

• This is possible with wrapper classes, which contain one of the primitive types, which then allow for the creation of objects that represent primitive types.

• Certain operations are only possible this way

Page 5: Object Oriented Programming: Java Edition By: Samuel Robinson

Examples

• System.out.println(Integer.toBinaryString(4));– Returns 100

• System.out.println(Integer.toHexString(16));– Returns 10

• int x = Integer.parseInt(“9”) returns 9• double c = Double.parseDouble(“5”) returns

5.0

Page 6: Object Oriented Programming: Java Edition By: Samuel Robinson

Initialization of Objects

• Constructors• Instance variable initialization• Instance initialization• Default value

Page 7: Object Oriented Programming: Java Edition By: Samuel Robinson

Constructor

class Demo{int a;public Demo(){// the initialization code for instance variablesa=0;}}

Page 8: Object Oriented Programming: Java Edition By: Samuel Robinson

Instance Variable Initialization

class Demo{private int a = 1; //this evaluates a with a value of 1}

Page 9: Object Oriented Programming: Java Edition By: Samuel Robinson

Instance Initialization

class Demo{private int a;//initializer{a=2;}}

Page 10: Object Oriented Programming: Java Edition By: Samuel Robinson

Inheritance

Direct support for single inheritance, partial support for multiple.

Single Inheritance is done by using the extends keyword, multiple inheritance is done by using an interface.

Page 11: Object Oriented Programming: Java Edition By: Samuel Robinson

Single Inheritance

Public class Vehicle{Int licensenum;Void getlicenseplate(int licensenum){}

Class Car extends Vehicle{Int numofDoors;}

Car SomeCar = new Car();SomeCar. NumofDoors;SomeCar.licenseplatenum;

Page 12: Object Oriented Programming: Java Edition By: Samuel Robinson

Multiple InheritancePublic interface Relatable {

Public int isLargerthan (Relatable other); }

Public class triangle implements Relatable {Public int base = 0;Public int height = 0;//other stuff and classes

Public int getArea(){return (base * height) / 2;

}

triangle otherTri = (triangle)other;if(this.getArea() < otherTri.getArea())

return -1;//and so on

Page 13: Object Oriented Programming: Java Edition By: Samuel Robinson

Dynamic Binding

All method calls are dynamically bound unless the method was defined as final, static or private.

These 3 keywords prevent overriding.

Page 14: Object Oriented Programming: Java Edition By: Samuel Robinson

Subclasses

• A Java subclass is a subtype only if there is a declared relationship (extends or implements) and for each method in the first one, there is a corresponding method in the other

Page 15: Object Oriented Programming: Java Edition By: Samuel Robinson

Examplepublic class Animal { public static void hide() { System.out.println("The hide method in Animal."); }

public void override() { System.out.println("The override method in Animal."); }}public class Cat extends Animal { public static void hide() { System.out.println("The hide method in Cat."); } public void override() { System.out.println("The override method in Cat."); } public static void main(String[] args) { Cat myCat = new Cat(); Animal myAnimal = (Animal)myCat; myAnimal.hide(); myAnimal.override(); }}

Page 16: Object Oriented Programming: Java Edition By: Samuel Robinson

Type Checking and Polymorphism

• Static type checking• Example of polymorphism:• http://download.oracle.com/javase/tutorial/

java/IandI/polymorphism.html

Page 17: Object Oriented Programming: Java Edition By: Samuel Robinson

Object Allocation/Deallocation

• All objects are explicit heap dynamic.• From the heap, most of allocated with the new

operator, but there is no explicit deallocation operator (garbage allocation).

• Problem: If an object has access to a resource other than heap memory, garbage collection won't catch it (closing a file).

• Use finalize

Page 18: Object Oriented Programming: Java Edition By: Samuel Robinson

Example

protected void finalize() throws Throwable { try { close(); // close open files } finally { super.finalize(); }}

Page 19: Object Oriented Programming: Java Edition By: Samuel Robinson

Nested Classes

• Inner classes – nonstatic, have an implicit pointer to the nesting class.

– An instance of a nested class can only exist within an instance of its nesting class

• Local nested class – defined in a method of its nesting class.

– Never defined with an access specifier– A method in this class can access variables

defined in its nesting class and final variables defined in the method in which the local nested class is defined