You are on page 1of 6

6/20/2013

Topics

Object-Oriented Concepts Java Program Structure

Review of Basic Concepts in Java

Introduction to Programming 2

Introduction to Programming 2

Object-Oriented Concepts

Object-Oriented Concepts

Object-Oriented Design

Object

Focuses on object and classes based on real world scenarios Emphasizes state, behavior and interaction of objects Advantages:

An entity that has a state, behavior and identity with a well-defined role in problem space An actual instance of a class Created every time you instantiate a class using the new keyword.

Faster development Increased quality Easier maintenance Enhanced modifiability Increase software reuse

Attribute

Data element of an object Stores information about the object A.K.A. Data member, instance variable, property, data field

Class

Allows you to define new data types Blueprint


Introduction to Programming 2 3 Introduction to Programming 2 4

Object-Oriented Concepts

Object-Oriented Concepts

Method

Describes the behavior of an object Also called a function or a procedure

Encapsulation/abstraction

Principle of hiding design or implementation information not relevant to the current object

Constructor

Method-like For creating and initializing a new object Not members of an object

Introduction to Programming 2

Introduction to Programming 2

6/20/2013

Object-Oriented Concepts

Object-Oriented Concepts

Inheritance

Polymorphism

Relationship between classes wherein one class is the superclass or the parent class of another Refers to the properties and behaviors received from an ancestor Also know as a "is-a" relationship

"poly" means many while "morph" means form Ability of an object to assume many different forms

Interface

SuperHero

Contract in the form of a collection of method and constant declarations Implementing class promises to follow the contract.

FlyingSuperHero

UnderwaterSuperHero

Introduction to Programming 2

Introduction to Programming 2

Java Program Structure: Declaring Classes

Java Program Structure: Declaring Classes


1 2 3 4 5 6 7 8

Syntax
<modifier> class <name> { <attributeDeclaration>* <constructorDeclaration>* <methodDeclaration>* }

class SuperHero { String superPowers[]; void setSuperPowers(String superPowers[]) this.superPowers = superPowers; } void printSuperPowers() { for (int i = 0; i < superPowers.length; i++) { System.out.println(superPowers[i]); } } }
Introduction to Programming 2 10

<classDeclaration> ::=

where

Modifier

9 10 11

Refers to an access modifier or other types of modifiers

Name

Is an identifier for the name of the class


Introduction to Programming 2 9

Java Program Structure: Declaring Attributes

Java Program Structure: Declaring Attributes


1 2 3 4

Syntax:
<modifier> <type> <name> [= <default_value>];

public class AttributeDemo { private String studNum; public boolean graduating = false; protected float unitsTaken = 0.0f; String college; }

<attributeDeclaration> ::=

<type> ::= byte | short | int | long | char | float | double boolean | <class> |

5 6

Introduction to Programming 2

11

Introduction to Programming 2

12

6/20/2013

Java Program Structure: Declaring Methods

Java Program Structure: Declaring Methods


1 2 3 4 5 6 7 8 9 10 11 12

Syntax:
<modifier> <returnType> <name>(<parameter>*) { <statement>* }

class MethodDemo { int data; int getData() { return data; } void setData(int data) { this.data = data; } void setMaxData(int data1, int data2) { data = (data1>data2)? data1 : data2; } }
Introduction to Programming 2 14

<methodDeclaration> ::=

<parameter> ::= <parameter_type> <parameter_name>[,]

Introduction to Programming 2

13

Java Program Structure: Declaring a Constructor

Java Program Structure: Declaring a Constructor


1 2 3 4 5 6

Syntax:
<modifier> <className> (<parameter>*) { <statement>* }

class ConstructorDemo { private int data; public ConstructorDemo() { data = 100; } ConstructorDemo(int data) { this.data = data; } }

<constructorDeclaration> ::=

where

Modifier

7 8 9

Can be any access modifier but not other types of modifiers.

Default constructor

No arguments Empty body


Introduction to Programming 2 15 Introduction to Programming 2 16

Java Program Structure: Instantiating a Class

Syntax:

Java Program Structure: Accessing Object Members


Dot notation:
<object>.<member>

new <constructorName>(<parameters>)

Example:
1. 2. 3. 4. 5. 6. 7. 8. 9.

class ConstructObj { int data; ConstructObj() { /* initialize data */ } public static void main(String args[]) { ConstructObj obj = new ConstructObj(); } }
Introduction to Programming 2 17

An example:

String myString = new String(My String); //Access length method System.out.println(Length: + myString.length()); Output: Length: 9

Introduction to Programming 2

18

6/20/2013

Java Program Structure: Accessing Object Members

Java Program Structure: Accessing Object Members


1 2 3 4 5 6

class ConstructObj { int data; ConstructObj() { /* initialize data */ } void setData(int data) { this.data = data; } public static void main(String args[]) { ConstructObj obj = new ConstructObj(); obj.setData(10); } }
Introduction to Programming 2 20

Another example

int intArr = {1, 2, 3, 4, 5}; //Access length attribute System.out.println(Length: + intArr.length); Output: Length: 5

7 8 9 10 11 12 13

//access setData()

System.out.println(obj.data); //access data

Introduction to Programming 2

19

14

Java Program Structure: Accessing Object Members


Output: 10

Java Program Structure: The Access Modifiers

Introduction to Programming 2

21

Introduction to Programming 2

24

Java Program Structure: Encapsulation


Java Program Structure: Encapsulation

Hide members by making them private Example


1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.}

What if I do not want to allow other classes to modify the value of secret? Answer: Make the setSecret() method private.

class Encapsulation { private int secret; public boolean setSecret(int secret) { if (secret < 1 || secret > 100) return false; this.secret = secret; return true; } public getSecret() { return secret; }
Introduction to Programming 2 25 Introduction to Programming 2 26

6/20/2013

Java Program Structure: Inheritance

Java Program Structure: Inheritance


1.import

Creating a child class or a subclass:


java.awt.*; Point {

Use extends in declaring the class Syntax:


2.class 3. 4.

class <childClassName> extends <parentClassName>

int x; int y;

A class can only extend one parent class

5.}

6.class 7. 8.}

ColoredPoint extends Point {

Color color;

Introduction to Programming 2

27

Introduction to Programming 2

28

Java Program Structure: Overriding Methods

Java Program Structure: Overriding Methods


1. 2. 3.

Subclass defines a method whose signature is identical to a method in the superclass Signature of a method

class Superclass { void display(int n) { System.out.println("super: " + n); } } class Subclass extends Superclass { void display(int k) { System.out.println("sub: " + k); }

4. 5.

Information found in the method header definition

Return type Method name Parameter list of the method


6. 7. 8.

Different from method overloading!

9. 10.}

Introduction to Programming 2

29

11.//

continued...

Introduction to Programming 2

30

Java Program Structure: Overriding Methods


14.class 15. 16. 17. 18. 19. 20. 21.}

Java Program Structure: Overriding Methods


Output: sub: 3 sub: 4

OverrideDemo { Subclass SubObj = new Subclass(); Superclass SuperObj = SubObj; SubObj.display(3); ((Superclass)SubObj).display(4);

public static void main(String args[]) {

Introduction to Programming 2

31

Introduction to Programming 2

32

6/20/2013

Java Program Structure: Overriding Methods

Java Program Structure: Overriding Methods


1. 2. 3.

Version of method called

class Superclass { void overriddenMethod() { } } class Subclass1 extends Superclass { public void overriddenMethod() { } } class Subclass2 extends Superclass { void overriddenMethod() { }

Based on actual data type of the object that invoked the method

Access modifier for the methods need not be the same

4. 5. 6. 7. 8. 9.

Access modifier of the overriding method

Same access modifier as that of the overridden method Less restrictive access modifier

10. 11. 12.} Introduction to Programming 2 33

13.//continued...

Introduction to Programming 2

34

Java Program Structure: Overriding Methods


14./* 15. 16. 17.}

class Superclass { void overriddenMethod() { } */ Subclass3 extends Superclass { protected void overriddenMethod() { } Subclass4 extends Superclass {

18.class 19. 20. 21.} 22.class 23. 24. 25.}

private void overriddenMethod() { }

Introduction to Programming 2

35

You might also like