object oriented programming concepts n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Object-Oriented Programming Concepts PowerPoint Presentation
Download Presentation
Object-Oriented Programming Concepts

Loading in 2 Seconds...

play fullscreen
1 / 44

Object-Oriented Programming Concepts - PowerPoint PPT Presentation


  • 81 Views
  • Uploaded on

Object-Oriented Programming Concepts. Recap from last lecture. Variables and types int count Assignments count = 55 Arithmetic expressions result = count/5 + max Control flow if – then – else while – do do – while For SubPrograms Methods. Programming.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Object-Oriented Programming Concepts' - miles


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
recap from last lecture
Recap from last lecture
  • Variables and types
    • int count
  • Assignments
    • count = 55
  • Arithmetic expressions
    • result = count/5 + max
  • Control flow
    • if – then – else
    • while – do
    • do –while
    • For
  • SubPrograms
    • Methods
programming
Programming
  • Programming consists of two steps:
      • design (the architects)
      • coding (the construction workers)
    • Object oriented design
    • Object oriented programming
what is an object
What Is an Object?
  • Real world examples: bicycle; dog; dinosaur; table; rectangle; color.
  • Objects have two characteristics: state (attributes) and behavior
  • Software objects maintain its state in variablesor data, and implement the behavior using methods.

An Object

what is an object1
What Is an Object?
  • Real-world objects can be represented using software objects:

e.g., electronic dinosaur, bicycle

  • Software objects may correspond to abstract concepts:

e.g., an event

A Bicycle Object

what is an object2
What Is an Object?
  • Methods to brake, change the pedal cadence, and change gears.
  • Concept of encapsulation: hiding internal details from other objects:

you do not need to know how the gear mechanism works.

  • In Java, both methods and variables can be hidden

A Bicycle Object

what are messages
What Are Messages?
  • Software objects interact and communicate with each other using messages(method invocation)

A message with parameters

what are messages1
What Are Messages?

Three components comprise a message:

1.The object to whom the message is addressed (Your Bicycle)

2.The name of the method to perform (changeGears)

3.Any parameters needed by the method (lower gear)

what are classes
What Are Classes?
  • A class is a blueprint or prototype that defines the variables and the methods common to all objects of a certain kind.
  • Instantiation of a class: create an instance (object) according to the blueprint specification.
what are classes1
What Are Classes?
  • Consist of public API and private implementation details
object vs class
Object vs Class
  • Each object has its own instance variables:

e.g., each bicycle has its own (x,y) position.

object vs class1
Object vs Class
  • Usually no memory is allocated to a class until

instantiation, whereupon memory is allocated to an object

of the type.

  • Except when there are class variables. All objects of the same class share the same class variables:

e.g., extinct variable of dinosaur class; tax rate of certain

class of goods.

what is inheritance
What Is Inheritance?
  • A class inherits state and behavior from its superclass.
  • A subclass can define additional variables and methods.
  • A subclass can override methods of superclass (e.g., change gear method might be changed if an additional gear is provided.)
  • Can have more than one layer of hierarchy

Software Reuse

what is an interface
What is an Interface?

Definition: An interface is a device that unrelated objects use to interact with each other. An object can implement multiple interfaces.

Interface B

Object

Object 1

Interface C

Interface A

primitive and reference data type

0

x:

p1:

0

y:

p2:

Primitive and Reference Data Type

Point p1, p2;

p1 = new Point();

p2 = p1;

int x;

x = 5;

5

x:

Primitive Data

Type

Reference Data Type

brief introduction to classes
Brief Introduction to Classes

A point in 2-D space:

public class SimplePoint {

public int x = 0;

public int y = 0;

}

Upon instantiation

brief introduction to classes1
Brief Introduction to Classes

A simple rectangle class:

public class SimpleRectangle {

public int width = 0;

public int height = 0;

public SimplePoint origin = new SimplePoint();

}

Reference type vs

primitive type

a brief introduction to classes
A Brief Introduction to Classes

A Point Class with a constructor:

public class Point {

public int x = 0;

public int y = 0;

// a constructor!

public Point(int x, int y) {

this.x = x;

this.y = y; } }

new Point(44,78);

a brief introduction to classes1
A Brief Introduction to Classes

More sophisticated Rectangle Class:

public class Rectangle {

public int width = 0;

public int height = 0;

public Point origin;

// four constructors

public Rectangle() {

origin = new Point(0, 0);

}

public Rectangle(Point p) {

origin = p;

}

a brief introduction to classes2
A Brief Introduction to Classes

public Rectangle(int w, int h) {

this(new Point(0, 0), w, h);

}

public Rectangle(Point p, int w, int h) {

origin = p;

width = w;

height = h;

}

// a method for moving the rectangle

public void move(int x, int y) {

origin.x = x;

origin.y = y;

}

a brief introduction to classes3
A Brief Introduction to Classes

// a method for computing the area of the rectangle

public int area() {

return width * height;

}

// clean up!

protected void finalize() throws Throwable {

origin = null;

super.finalize();

}

}

basic structures of a class
Class Declaration

Variable

Instance Variable

Class Variable

Constructor

Method

Instance Method

Class Method

Cleanup

Rectangle2.java

Basic Structures of a Class
creating classes
Creating Classes
  • A blueprint or prototype that you can use to create many objects.
  • Type for objects.

classDeclaration {

classBody

}

the class declaration
The Class Declaration
  • Simplest class declaration

class NameOfClass {

. . .

}

e.g.,

class ImaginaryNumber{

. . .

}

Start with capital letter by convention

the class declaration1
The Class Declaration

Class declaration can say more about the class:

  • declare what the class's superclass is
  • declare whether the class is public, abstract, or final (if not specified, then default)
  • list the interfaces implemented by the class
declaring a class s superclass
Declaring a Class's Superclass
  • All class has a superclass. If not specified, superclass is Object class by default
  • To specify an object's superclass explicitly,

class NameOfClass extends SuperClassName{

. . .

}

e.g.,

class ImaginaryNumber extends Number {

. . .

}

Part of java.lang package

declare whether the class is public final or abstract
Declare whether the Class is Public, Final, or Abstract

Modifier class NameOfClass {

. . .

}

  • Default: accessible only by classes within same package
  • Public: accessible by classes everywhere
  • Final: the class cannot be further subclassed.
  • Abstract: some methods are defined but unimplemented; must be further subclassed before instantiation.
listing the interfaces implemented by a class
Listing the Interfaces Implemented by a Class
  • An interface declares a set of methods and constants without specifying the implementation for any of the methods.

e.g.,

class ImaginaryNumber extends NumberimplementsArithmetic {

. . .

}

Contains unimplemented

methods such as add(), substract()

the class body
The Class Body
  • Contains two different sections: variable declarations and methods.

classDeclaration {

memberVariableDeclarations

methodDeclarations

}

e.g.,class TicketOuttaHere {

Float price; String destination; Date departureDate;

void signMeUp(Float forPrice, String forDest, Date forDate) {

price = forPrice; destination = forDest;

departureDate = forDate;

}

}

declaring member variables
Declaring Member Variables

All variables must have a type; “class”is also a type

  • amember variable declaration

typevariableName;

e.g., class IntegerClass {

intanInteger;

. . .

// define methods here

. . .

}

IntegerClassanIntergerObject;

anIntegerObject = new IntegerClass();

Lowercase by convention

statement for member variable declaration
Statement for Member Variable Declaration

[accessSpecifier] [static] [final] [transient][volatile]

type variableName

  • accessSpecifier defines which other classes have access to the variable (public, private, or protected)
  • static indicates that the variable is a class member variable, not an instance member variable.
  • final indicates that the variable is a constant:

class Avo { final double AVOGADRO = 6.023e23;}

  • transient variables are not part of the object's persistent state
  • volatilemeans that the variable is modified asynchronously

By convention, all capitals

managing inheritance
Managing Inheritance

All classes inherit from the Object class.

creating subclasses
Creating Subclasses

class SubClass extends SuperClass {

. . .

}

  • A Java class can have only one direct superclass. Java does not support multiple inheritance.
what member variables does a subclass inherit
What Member Variables Does a Subclass Inherit?
  • Rule: A subclass inherits all of the member variables within its superclass that are accessible to that subclass.
    • Member variables declared as public or protected. Do not inheritprivatemember variables.
    • Member variables declared with no access modifier so long as subclass is in the same package
    • If subclass declares a member variable with the same name, the member variable of the superclass is hidden.
hiding member variables
Hiding Member Variables

class Super {

Number aNumber;

}

class Sub extends Super {

Float aNumber;

}

what methods does a subclass inherit
What Methods Does a Subclass Inherit?
  • Rule: A subclass inherits all of the methods within its superclass that are accessible to that subclass.
    • publicor protected methods, but not privatemethods
    • no access modifier but in the same package
    • If subclass declares a method with the same name, the method of the superclass isoverridden.
overriding methods
Overriding Methods
  • A subclass can either completely override the implementation for an inherited method or the subclass can enhance the method by adding functionality to it.
replacing a superclass s method implementation
Replacing a Superclass's Method Implementation

An example: Thread class has an empty implementation of run().

class BackgroundThread extends Thread {

void run() {

. . .

}

}

adding to a superclass s method implementation
Adding to a Superclass's Method Implementation

Another Example: want to preserve initialization done by superclass in constructor:

class MyWindow extends Window {

public MyWindow(Frame parent) {

super(parent);

. . .

// MyWindow-specific initialization here

. . .

}

}

Superclass constructor

methods a subclass cannot override
Methods a Subclass Cannot Override
  • A subclass cannot override methods that are declared final in the superclass.
m ethods a subclass must override
Methods a Subclass Must Override
  • Subclass must override methods that are declared abstract in the superclass, or the subclass itself must be abstract.
being a descendent of object
Being a Descendent of Object
  • Every class in the Java system is a descendent (direct or indirect) of the Object class.
  • Your class may want to override:
    • clone
    • equals
    • finalize
    • toString
  • Your class cannot override (they are final):
    • getClass
    • notify
    • notifyAll
    • wait
    • hashCode
summary
Summary

You should know

  • Objects are created from classes
  • An object's class is its type
  • Difference between reference and primitive types.

You also should have a general understanding or a feeling for the following:

  • How to create an object from a class
  • What constructors are
  • What the code for a class looks like
  • What member variables are
  • How to initialize objects
  • What methods look like