Object Oriented Programming in Java Habib Rostami Lecture
Object Oriented Programming in Java Habib Rostami Lecture 6
Today’s Presentation Constructor Singleton Pattern Inheritance
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.
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 x. Co, int y. Co){ r=radius; x=x. Co; y=y. Co; } many constructors, same name, what do they return? }
Class class Krog { private int x, y; private int r; Circle -x, y: int -r: int +get. Area() +get. Circumference() +move (dx: int, dy: int) Circle(int radius) { r=radius; x=0; y=0; } Circle(int radius, int x. Coor, int y. Coor ){ r=radius; x=x. Coor; y=y. Coor; } public double get. Circumference () { return 2*3. 14*r; } public double get. Area () {return 3. 14*r*r; } public void move (int dx, int dy) { x+=dx; y+=dy; }
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
Creating instances Circle small; small = new Circle(2); small: Circle x=0 y=0 r=2 large: Circle large = new Circle (10, 5, 5); x=5 y=5 r=10 : Circle new Circle (2, 4, 5); x=4 y=5 r=2
Class first public class Bank. Account { private String owner; private int balance; Bank. Account() { owner="anonymous"; balance=1000000; } public void withdraw (int ammount) { balance -= ammount; } Bank. Account (String name, int ini){ owner=name; balance=ini; } public void deposit (int ammount) { balance += ammount; } Bank. Account(int ammount) { owner="Metka"; balance=amount; } } // class BA
Then we can create and use objects (instances)… • declaration Class. Name object. Name; • creation/instatiation using new object. Name = new Class. Name (argument. List); Class. Name object. Name = new Class. Name (argument List); • usage object. Name. variable. Name; ime. Objekta. method. Name(parameters);
Singleton Pattern • Enable you restrict the number of instances of a class to be at most one • How you can do that?
Singleton Pattern public class Test{ private static Test the. Instance = null; private Test(){ } public static Test get. Instance(){ if (the. Instance == null){ the. Instance = new Test(); } return the. Instance; } }
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)
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)
Inhertiance class Person { … calculate. Age (){…} } class Student extends Person { … has. Passed. Exam (Exam e){…} } class Professor extends Person { … get. Courses. Taught(){…} } all classes are derived from java. lang. Object
Differentiate! multiple inheritance Depth of Inheritance
Abstract class can not be instatiated Shape {abstract} +move(dx, dy) +draw (){abstract} Rectangle Circle
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; } }
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 get. Balance (); }
abstract class Shape { abstract double get. Area (); } interface Drawable { public void draw (Graphics g); } class Rectangle extends Shape implements Drawable{ double a; Rectangle (double a) { this. a=a; } double get. Area {return 0. 5*a*a; } public void draw (Graphics g) { g. draw. Rect(0, 0, a, a); } }
- Slides: 19