Object Oriented Programming in Java Habib Rostami Lecture 6.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming with Java
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Object Oriented Programming
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Reusable Classes.  Motivation: Write less code!
OOP: Inheritance By: Lamiaa Said.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Object Oriented Programming Inheritance and Polymorphism Dr. Mike Spann
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
Java Programming Review (Part I) Enterprise Systems Programming.
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
What is inheritance? It is the ability to create a new class from an existing class.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 10 Inheritance and Polymorphism
Peyman Dodangeh Sharif University of Technology Fall 2014.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Coming up: Inheritance
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Object Oriented Programming in Java Habib Rostami Lecture 7.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
CS 116 Object Oriented Programming II Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Lecture 12 Inheritance.
2.7 Inheritance Types of inheritance
Final and Abstract Classes
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Computing with C# and the .NET Framework
CS240: Advanced Programming Concepts
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Polymorphism and access control
Week 6 Object-Oriented Programming (2): Polymorphism
Object Oriented Programming
Software Design Lecture : 12.
Inheritance Inheritance is a fundamental Object Oriented concept
Chapter 14 Abstract Classes and Interfaces
An Example of Inheritance
Final and Abstract Classes
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

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 xCo,int yCo){ r=radius; x=xCo; y=yCo; } many constructors, same name, what do they return?

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

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); 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

Class first public class BankAccount { private String owner; private int balance; BankAccount() { owner="anonymous"; balance= ; } 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

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);

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 theInstance = null; private Test(){ } public static Test getInstance(){ if (theInstance == null){ theInstance = new Test(); } return theInstance; }

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 { … calculateAge (){…} } class Student extends Person { … hasPassedExam (Exam e){…} } class Professor extends Person { … getCoursesTaught(){…} } all classes are derived from java.lang.Object

Differentiate! Depth of Inheritance multiple inheritance

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

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 getBalance (); }

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); }