Procedural Programming vs. Object-Oriented Programming

Procedural

thinks in terms of what things a program has to do

Java OOP

when specification of the program changes, you have to change the existing code and re-test all

when specification of the program changes, you can test only the changed portion

Steps for efficient design:

  1. Check what is common in all the classes.
  1. Abstract the common feature and put in a superclass.
  1. Inherit all the subclass with feature from superclass.
  1. Override any specific requirement in one of the subclass.

Class

blueprint of an object

Object

knows things about itself

instance variables

they represent the state of an object

does things

methods

they represent the behavior of an object

tester

where you put the main method where you create and access objects of your new class type

uses the dot operator (.) to access the
methods and variables of the new objects

a java program is nothing more than objects ‘talking’ to other object

takes parameters

values you pass as an arguments to a method:

must match the order and type of the parameters declared by the method

one or more

can be a literal value or a variable of the declared parameter type

Values passed in and out of methods can be implicitly promoted to a larger type or explicitly cast to a smaller type

must declare a return type

Variable Types

instance

class

local

scope: within the block it is declared

scope: all through the class except in static methods

scope: all through the class

declared inside a class, not within a method

declared within a method

Data Types

primitive

reference

two primitives can be compared using the == operator

when == is used for equality check of references it just tells if both reference point to the same Heap Address

Encapsulation

The instance variable should be hidden by the use of private access modifiers.

Instance variables can only be accessed using a getter or a setter method with public access modifiers.

Any place where a particular value can be used, a method call that returns that type can be used.

data hiding (hides the implementation details from users)

Extra-Strength Methods

Java APIs

prep code

test code

real code

a form of pseudocode

instance variable declaration

method declaration

method logic

class or method that tests the real code and validate the process that is working according to the requirements

actual implementation of the class

converting a string to an integer:

Integer.parseInt() method

works only on Strings that represent the ASCII values for digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

for loop

enhanced for loop

regular for loop

for(typeName variable : collection (an array) ) { statements }

for(initialization; boolean test; iteration expression) { statements }

vs. while loop

has only the boolean test; it doesn’t have a built-in initialization or iteration expression

used when number of iteration is unknown and just want to keep going while some condition is true

Java standard edition ships with hundreds of pre-built classes

classes

interfaces

user interfaces

Array List

java.util package

resizable

objects can be added (through add(anObject) method) or removed

we can specify an index using the add(anInt, anObject) method

parameterized types

Using the Library

classes are grouped into packages

to use a class in the API, you have to know which package the class is in

provides name scoping to prevent collisions

provide security because you can restrict the code you write so that only other classes in the same package can access it

extensions

packages that do not come with a standard library

real

for the type of object you want to use