oop basics

14
Internet Programming II Yildiz Technical University 2015 Object Oriented Programming Ömer Taşkın

Upload: oemer-taskin

Post on 14-Jul-2015

146 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Oop basics

Internet Programming IIYildiz Technical University 2015

Object Oriented Programming

Ömer Taşkın

Page 2: Oop basics

OUTLINE

• Class

– Property

– Methods

– Constructor

• Package

• Inheritance

• Interface

• Method Overloading

IP II - OOP 2

Page 3: Oop basics

Classes

IP II - OOP 3

• Basically it’s a data type

• Holds attributes (variables)

• Has behaviors (methods)

class Person {

String name;

}

class Phone{

int number;

}

Page 4: Oop basics

Classes

IP II - OOP 4

• Methods can be void() or have return type

class Phone {

private int number;

public int getNumber() {

return number;

}

public void setNumber(int number) {

this.number = number;

}

}

Page 5: Oop basics

Classes

IP II - OOP 5

• Constructors are initializing method for a Class

• Phone number would be settled when Phone class initialized

class Phone {

private int number;

public Phone(int number) {

this.number = number;

}

}

• Constructors cannot have any return type!

Page 6: Oop basics

Packages

IP II - OOP 6

• Packages have directory behavior

• Provides hierarchy and grouping

package com.otaskin.domain;

class Member {

//

}

package com.otaskin.domain;

class Product {

//

}

Page 7: Oop basics

Packages

IP II - OOP 7

• Classes can import each other by package name + class name

• Generated value is called as FQN (Fully Qualified Name)

package com.otaskin;

import com.otaskin.domain.Member;

import com.otaskin.domain.Product;

class App {

//

}

Page 8: Oop basics

Inheritance

IP II - OOP 8

• Basically its parent / child relationship

• Child has Parent’s behaviors

• extends is the keyword of inheritance

class Phone {

protected int number;

protected void callNumber(int

number) {

// implementation of call

}

}

class DeskPhone extends Phone {

// Phone is parent class of DeskPhone

// Also means DeskPhone is a Phone

// DeskPhone has a number so

// DeskPhone has callNumber behavior

}

Page 9: Oop basics

Inheritance

IP II - OOP 9

• UML Diagram of extended version

Page 10: Oop basics

Inheritance

IP II - OOP 10

• Another Example

class Person {

protected String name;

}

class Student extends Person {

// Person is parent class of Student

// Also means Student is a Person

// Student has a name so

}

Page 11: Oop basics

Interface

IP II - OOP 11

• Interface says what can do

• Does not explain about how can do

• implements is the keyword of interfaces

interface Callable {

// cannot have body

void callNumber(int number);

}

class Phone implements Callable{

// method body is defined here

void callNumber(int number) {

// …

}

}

Page 12: Oop basics

Overloading

IP II - OOP 12

• In OOP method names can be duplicated

• has to be parameter difference(s) between overloaded methods

interface Callable {

void callNumber(int number);

void callNumber(int number[]);

}

Page 13: Oop basics

Overloading

IP II - OOP 13

• Also constructors are overloadable

class Person{

private String name;

private int id;

public Person() {}

public Person(String name) {

this.name = name;

}

public Person(int id) {

this.id = id;

}

public Person(String name, int id) {

this.id = id;

this.name = name;

}

}

Page 14: Oop basics

QUESTIONS

IP II - OOP 14