Docsity

Object-Oriented Programming Java, Exams of Object Oriented Programming

Partial preview of the text

Download Object-Oriented Programming Java and more Exams Object Oriented Programming in PDF only on Docsity!

OOP - JAVA

Object-Oriented Programming

Java

Margit ANTAL

Goals

  1. Java Language
  2. Objects and classes
  3. Static Members
  4. Relationships between classes
  5. Inheritance and Polymorphism
  6. Interfaces and Abstract Classes
  7. Exceptions
  8. Nested Classes
  9. Threads
  10. GUI Programming
  11. Collections and Generics
  12. Serialization

Java language

● History

● Java technology: JDK, JRE, JVM

● Properties

● 'Hello world' application

● Garbage Collection

Short History

● 1991 - Green Project for consumer electronics market

(Oak language → Java)

● 1994 – HotJava Web browser

● 1995 – Sun announces Java

● 1996 – JDK 1.

● 1997 – JDK 1.1 RMI, AWT, Servlets

● 1998 – Java 1.2 Reflection, Swing, Collections

● 2004 – J2SE 1.5 (Java 5) Generics, enums

● 2014 – Java SE 8 Lambdas

Java technology

● JDK – Java Development Kit

● JRE – Java Runtime Environment

● JVM – Java Virtual Machine

JDK javac, jar, debugging

JRE java, libraries

JVM

Properties

● Object-oriented

● Interpreted

● Portable

● Secure and robust

● Scalable

● Multi-threaded

● Dynamic language

● Distributed

Hello World Application

HelloWorld.java

HelloWorld.class

javac HelloWorld.java

bytecode

java HelloWorld

Runtime

JVM

Garbage Collection

● Dynamically allocated memory

● Deallocation

− Programmer's responsibility (C/C++)

− System responsibility (Java):

● Is done automatically (system-level thread)

● Checks for and frees memory no longer needed

Module 2

Object-Oriented Programming

Object-oriented programming

Classes and Objects

● Class

● Attributes and methods

● Object (instance)

● Information hiding

● Encapsulation

● Constructors

● Packages

Declaring Classes

● Syntax

* class <class_name>{

<attribute_declaration>*

<constructor_declaration>*

<method_declaration>*

● Example

public class Counter{ private int value; public void inc(){ ++value; } public int getValue(){ return value; } }

Declaring Attributes

● Syntax

* <attribute_name>[= <initial_value>];

● Examples

public class Foo{ private int x; private float f = 0.0; private String name =”Anonymous”; }

Accessing Object Members

● Syntax

.

● Examples

public class Counter{ public static final int MAX = 100; private int value = 0;

public void inc(){ if( value < MAX ){ ++value; } } public int getValue(){ return value; } }

Counter c = new Counter(); c.inc(); int i = c.getValue();

Information Hiding

● The problem:

● Client code has direct access to internal data

/* C language */

struct Date {

int year, month, day;

/* C language */

Date d;

d.day = 32; //invalid day

d.month = 2; d.day = 30;

// invalid data

d.day = d.day + 1;

// no check