Download Presentation

Object-Oriented Programing in Java

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.

Note: In class method, you cannot use this and instance variables.(why?)

Class diagram of an Account class.

Class Name

Instance Methods

ShowNumberOfAccount

Class Methods

Declare class field/method with the ‘static’ Modifier

makes methods and variablesbelong to the class rather than instances of the class.

Example: counting how many circles:

public class Circle

{ public double x, y, r; // instance variables

// ncircles is class variable

public static int ncircles = 0;

// Constructors

public Circle(double x, double y, double r)

{ this.x = x; this.y = y; this.r = r;

ncircles++; }

public Circle(double r)

{ x = 0.0; y = 0.0; this.r = r;

ncircles++; }

...}

The static Modifier (cont.)

In the above example, there's only one instance of the ncircles variable.

Diff. ways to referencencircles:

Circle.ncircles // ClassName.classVarName

ncircles; this.ncircles, // used inside the Circle class definition only

c.ncircles // where c is a Circle variable

Similar approach for static methods.

Examples of static methods (or called class method):

Math.cos(x) Math.pow(x,y) Math.sqrt(x)

Notes on class methods

Must be declared with the static keyword

Also called static method

Can only operate on class variables (e.g., static)

Cannot use ‘this’

Cannot use instance variables

To access a class method: same as to access class vars:

Circle.countCircles() // ClassName.classVarName

countCircles(); this.countCircles(),

// legal only when inside the Circle class definition

c.countCircles() // where c is a Circle variable

Lots of examples of class methods in the JDK (e.g., String)

Class B { int x; static int y;

static int b1() { … }

int b2 () { … }

static int b3() { … // class method

int c;

c = x; c = this.x //error

c = y; c = B.y; //ok!

A a = new A();

B b = new B();

c = a.a1(); //ok!

c = a.a2() ; // ok!

c = A.a2() ; // error!

c = A.a1() ; // ok!

c = b.b1() ; //ok!

c = b.b2() ; // ok!

c = B.b2() ; // error!

c = B.b1() ; // ok!

c = b1(); // ok

c = b2(); // error }

Int b4() { // instance method

c = x; c = this.x //ok!

c = y; c = B.y; c = this.y //ok!

…

c = b1(); c = this.b1() // ok

c = b2(); c=this.b2() // ok

}

}

Class A {

public static int a1(){…}

public int a2() {…} … }

Example: instance member v.s. class member

Class and Instance initializers

Both class and instance variables can have initializers attached to their declarations.

static int num_circles = 0;

float r = 1.0;

Class variables are initialized when the class is first loaded.

Instance variables are initialized when an object is created.

Sometimes more complex initializationis needed.

For instance variables, there are constructor methods ,and instance initializer.

For class variables static initializers are provided

An example static/instance initializer

public class TrigCircle { // Trigonometric circle

// Here are our static lookup tables, and their own simple initializers.

static private double sines[] = new double[1000];

static private double cosines[] = new double[1000];

// Here's a static initializer "method" that fills them in.

// Notice the lack of any method declaration!

static {

double x, delta_x;

int i;

delta_x = (Circle.PI/2)/(1000-1);

for(i = 0, x = 0.0; i < 1000; i++, x += delta_x) {

sines[i] = Math.sin(x);

cosines[i] = Math.cos(x);

} … // The rest of the class omitted.

An example static/instance initializer (continued)

// instance field and methods

Private int[] data = new int[100]; // data[i] = i for i = 0..99

// instance initializer as an unnamed void method

{ for(int I = 0; I <100; I++) data[I] = I; }

…

}

Notes on initializers

can have any number of static/instance initializers;

can appear anywhere a field or method can appear.

Static initializer behaves like class method and cannot use this keyword and any instance fields of the class

The body of each instance initializers (alone with field initialization expressions) is executed in the order they appear in the class and is executed at the beginning of every constructor.

The body of each static initializers (alone with static field initialization expressions) is executed in the order they appear in the class and is executed while the class is loaded.

Inheritance in OOP

Inheritance is a form of software reusability in which new classes are created from the existing classes by absorbing their attributes and behaviors.

Instead of defining completely (separate) new class, the programmer can designate that the new class is to inherit attributes and behaviours of the existing class (called superclass). The new class is referred to as subclass.

Programmer can add more attributes and behaviors to the subclass, hence, normally subclasses have more features than their superclasses.

Inheritance relationships form tree-like hierarchical structures.

Subclasses and Inheritance

An important aspect of OO programming:

the ability to create new data types based on existing data types

Example ... a class of drawable Circles:

we'd like to be able to draw the circles we create, as well as setting and examining their properties.

for drawing, we need to know the color of the circle's outline and its body

In Java, we implement this by defining a new class that extends the behavior of the Circle class.

This new class is a subclass of Circle.

Subclass Example

The class GraphicCircle:

public class GraphicCircle extends Circle

{ // Extra fields

Color outline, fill;

// Extra constructors

public GraphicCircle(Color edge, Color fill)

{ x = 0.0; y = 0.0; r = 1.0;

outline = edge; this.fill = fill; }

public GraphicCircle(double r, Color edge, Color fill)

{ super(r); outline = edge; this.fill = fill; }

// Extra methods

public void draw(Graphics g)

{ g.setColor(outline); g.drawOval(x-r, y-r, 2*r, 2*r);

g.setColor(fill); g.fillOval(x-r, y-r, 2*r, 2*r); }

}

Subclass Inheritance

A subclass inherits fields and methods from its parent class.

A subclass methodoverridesa superclass method if they have the same signature.

A subclass field shadows a superclass field if they have the same name.

Refer to the superclass field via super.field

Note: you can also use super.method(…) to refer to overridden superclass method.

Using Subclasses

Subclasses are just like ordinary classes:

GraphicCircle gc = new GraphicCircle();

...

double area = gc.area();

We can assign an instance of GraphicCircle to a Circle variable.

Example:

GraphicCircle gc;

...

...

Circle c = gc; //widening conversion is

// always safe; explicit cast is not needed.

Superclasses, Objects, and the Class Hierarchy

Every class has a superclass.

If a class has no extends clause, it extends the Object class.

Object Class:

the only class that does not have a superclass

methods defined by Object can be called by any Java object

Abstract Classes

Abstract classes let us define the ``appearance'' of a group of classes.

We subsequently implement specific classes according to this pattern.

declare classes that define only part of an implementation, leaving extended classes to provide specific implementation of some or all of the methods.

The benefit of an abstract class is that

methods may be declared such that the programmer knows the interface definition of an object,

however, methods can be implemented differently in different subclasses of the abstract class.