Review 4 View classes as modules Encapsulate operations Functions are static methods 4 View classes as struct types Encapsulate data 4 View classes as.

Slides:



Advertisements
Similar presentations
Written by Paul Pu All Right Reservedwww.torontocollege.com Controlling Access to a Member(Java) public, private, protected (default)
Advertisements

Final and Abstract Classes
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Object Oriented Programming in Java. Characteristics of Object Oriented Programming  Object Programming classes  Encapsulation  Polymorphism  Inheritance.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
1 Object-Oriented Programming Concepts. 2 Recap from last lecture Variables and types –int count Assignments –count = 55 Arithmetic expressions –result.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Java Class and Inheritance.
Liang Chapter 6 Variable scope and the keyword this.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
UML Class Diagram: class Rectangle
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Object Oriented Programming: Java Edition By: Samuel Robinson.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
ICS 201 Introduction to Computer Science Problem Solving #4.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Interfaces An interface is like an extreme case of an abstract class – However, an interface is not a class – It is a type that can be satisfied by any.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Classes and Objects Introduction Life Cycle Creation Example.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Methods.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Final and Abstract Classes
Interface.
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
UML Class Diagram: class Rectangle
Corresponds with Chapter 7
null, true, and false are also reserved.
Interface.
Classes & Objects: Examples
Building Java Programs
Basics of OOP A class is the blueprint of an object.
Building Java Programs
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
CSG2H3 Object Oriented Programming
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Review 4 View classes as modules Encapsulate operations Functions are static methods 4 View classes as struct types Encapsulate data 4 View classes as abstract data types Encapsulate both data and operations

Point p = new Point(0,0); Stack s = new Stack(); Life Cycle of Objects 4 Creating objects 4 Using objects 4 Cleaning up unused objects –An object becomes garbage when no variable refers to it p.x = 100; rx = s.pop();

Class Declaration in Java [public] [abstract] [final] class ClassName [extends ClassName] [implements Interfaces] { [VariableDeclaration | ConstructorDeclaration | MethodDeclaration | ClassDeclaration | InterfaceDeclaration]* }

Programming in Java4 Declaring Variables –Access Specifier public: accessible from anywhere private: within the class (default): within the class and the package protected: within the package and subclasses –static : class variable –final: constant –Examples [AccessSpecifier] [static] [final] type VariableName

Programming in Java5 Method Declarations MethodDeclaration ::= MethodHead MethodBody MethodHead ::= [AccessSpecifier ] [static] [final] ReturnType MethodName(T1 p1,..., Tn pn) MethodBody ::= { [LocalVariableDeclarationStatement | ClassOrInterfaceDeclaration | [Identifier :] Statement ]* } signature

Method Overloading 4 A class can contain multiple methods that have the same name but different signatures class C { int m(int i){ return i+1; } double m(double f){ return f+2; }

Programming in Java7 Constructor 4 Constructor –Has the same name as the class –A class can have any number of constructors –Has no return value –Uses "this" to call other constructors in the class

Programming in Java8 Scope of Method Arguments class Circle { class Circle { int x, y, radius; int x, y, radius; public Circle(int x, int y, int radius) { public Circle(int x, int y, int radius) { this.x = x; this.x = x; this.y = y; this.y = y; this.radius = radius; this.radius = radius; }}

The Meaning of "static" 4 A static variable or method belongs to the class 4 A static variable is shared by all objects of the class class C { public static void main(String[] args){ int a = 10; int b = 20; System.out.println(max(a,b)); } int max(int x, int y){ return (x>y) ? x : y; } What is wrong?

Programming in Java10 Static Variables (exercise) class AnIntegerNamedX { static int x; static int x; public int x() { public int x() { return x; return x; } public void setX(int newX) { public void setX(int newX) { x = newX; x = newX; }} AnIntegerNamedX myX = new AnIntegerNamedX(); AnIntegerNamedX anotherX = new AnIntegerNamedX(); myX.setX(1); anotherX.x = 2; System.out.println("myX.x = " + myX.x()); System.out.println("anotherX.x = " + anotherX.x());