Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming in Java Habib Rostami Lecture 6.

Similar presentations


Presentation on theme: "Object Oriented Programming in Java Habib Rostami Lecture 6."— Presentation transcript:

1 Object Oriented Programming in Java Habib Rostami Lecture 6

2 Today’s Presentation  Constructor  Singleton Pattern  Inheritance

3 Contructors All classes have at least one constructor. A constructor is used to initialize a new object of that type and has the same name as the class. Constructor has no return type. A constructor is called by the new operator, which returns the newly created object. You cannot use the return statement in a constructor.

4 Constructors class Circle { private int x,y; // coordinates private int r; // radius Circle(int radius) { r=radius; x=0; y=0; } Circle(int radius, int xCo,int yCo){ r=radius; x=xCo; y=yCo; } many constructors, same name, what do they return?

5 Class class Krog { private int x,y; private int r; Circle(int radius) { r=radius; x=0; y=0; } Circle(int radius, int xCoor,int yCoor ){ r=radius; x=xCoor; y=yCoor; } public double getCircumference () { return 2*3.14*r;} public double getArea () {return 3.14*r*r;} public void move (int dx, int dy) { x+=dx; y+=dy; } Circle +getArea() +getCircumference() +move (dx:int, dy:int) -x,y:int -r:int

6 Constructors  If we don’t provide a constructor, compiler creates one (with no arguments) that invoke super()  constructor with empty argument list is a default costructor, that’s why we don’t have to invoke super()  Use super (arguments) to invoke specific constructor of superclass

7 Creating instances Circle small; small = new Circle(2); Circle large = new Circle (10,5,5); new Circle (2,4,5); x=0 y=0 r=2 small:Circle x=5 y=5 r=10 large:Circle x=4 y=5 r=2 :Circle

8 Class first public class BankAccount { private String owner; private int balance; BankAccount() { owner="anonymous"; balance=1000000; } BankAccount (String name,int ini){ owner=name; balance=ini; } BankAccount(int ammount) { owner="Metka"; balance=amount; } public void withdraw (int ammount) { balance -= ammount; } public void deposit (int ammount) { balance += ammount; } } // class BA

9 Then we can create and use objects (instances)… declaration ClassName objectName ; creation/instatiation using new objectName = new ClassName (argumentList); ClassName objectName = new ClassName (argument List); usage objectName.variableName; imeObjekta.methodName(parameters);

10 Singleton Pattern Enable you restrict the number of instances of a class to be at most one How you can do that?

11 Singleton Pattern public class Test{ private static Test theInstance = null; private Test(){ } public static Test getInstance(){ if (theInstance == null){ theInstance = new Test(); } return theInstance; }

12 Generalization Classes may have simmilar responsibilities identical methods common things in the superclass (generalized) Specific methods remain in the subclass concept: generalization/specialization inhertance – implementation element (extends)

13 Inheritance Subclasses inherit methods (and attributes) from a superclass Subclass can redefine (override) inherited methods Subclass can have additional (its own) methods and attributes Inheritance is used to organize class hierarchy Eliminates unnecessary duplication base class - superclass derived class - subclass single and multiple inheritnece (Java supports: single inheritance beetween classes)

14 Inhertiance class Person { … calculateAge (){…} } class Student extends Person { … hasPassedExam (Exam e){…} } class Professor extends Person { … getCoursesTaught(){…} } all classes are derived from java.lang.Object

15 Differentiate! Depth of Inheritance multiple inheritance

16 Abstract class Abstract class can not be instatiated Circle Shape {abstract} +move(dx,dy) +draw (){abstract} Rectangle

17 Abstract class Shape abstract class Shape { abstract double area ();} class Triangle extends Shape { double b, h; Triangle (double b, double h) { this.b=b; this.h=h;} double area () {return 0.5*b*h;} } class Square extends Shape { double a; Square (double a) { this.a=a;} double area () {return 0.5*a*a;} }

18 Interface has only abstract methods Can not be instatiated Provides signatures (and no implemenation) of methods that are defined (implemented) later by classes public interface Saleable { boolean increase (float quantity); boolean decrease (float quantity); float getBalance (); }

19 abstract class Shape { abstract double getArea (); } interface Drawable { public void draw (Graphics g);} class Rectangle extends Shape implements Drawable{ double a; Rectangle (double a) { this.a=a;} double getArea {return 0.5*a*a;} public void draw (Graphics g) { g.drawRect(0,0,a,a); }


Download ppt "Object Oriented Programming in Java Habib Rostami Lecture 6."

Similar presentations


Ads by Google