Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming: Java Edition By: Samuel Robinson.

Similar presentations


Presentation on theme: "Object Oriented Programming: Java Edition By: Samuel Robinson."— Presentation transcript:

1 Object Oriented Programming: Java Edition By: Samuel Robinson

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

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

4 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

5 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

6 Initialization of Objects Constructors Instance variable initialization Instance initialization Default value

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

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

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

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

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

12 Multiple Inheritance Public 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

13 Dynamic Binding All method calls are dynamically bound unless the method was defined as final, static or private. These 3 keywords prevent overriding.

14 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

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

16 Type Checking and Polymorphism Static type checking Example of polymorphism: http://download.oracle.com/javase/tutorial/java /IandI/polymorphism.html

17 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

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

19 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


Download ppt "Object Oriented Programming: Java Edition By: Samuel Robinson."

Similar presentations


Ads by Google