Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Written by: Dr. JJ Shepherd
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
ITEC200 – Week03 Inheritance and Class Hierarchies.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Java: Chapter 1 Computer Systems Computer Programming II.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
The Java Programming Language
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Inheritance in the Java programming language J. W. Rider.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Programming in Java CSCI-2220 Object Oriented Programming.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
OOP: Encapsulation &Abstraction
JAVA MULTIPLE CHOICE QUESTION.
Chapter 4 Assignment Statement
Lecture 2: Data Types, Variables, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3 Assignment Statement
Computer Science II Exam 1 Review.
Chapter 9 Inheritance and Polymorphism
null, true, and false are also reserved.
Java Programming Language
Introduction to Java Programming
An overview of Java, Data types and variables
Instructor: Alexander Stoytchev
Chapter 1: Computer Systems
Interfaces.
JavaScript Reserved Words
Instructor: Alexander Stoytchev
Java Inheritance.
Focus of the Course Object-Oriented Software Development
Module 2 - Part 1 Variables, Assignment, and Data Types
Java Programming Language
Instructor: Alexander Stoytchev
Zorah Fung University of Washington, Spring 2015
Chap 2. Identifiers, Keywords, and Types
Agenda Types and identifiers Practice Assignment Keywords in Java
Chapter 11 Inheritance and Encapsulation and Polymorphism
Zorah Fung University of Washington, Winter 2016
Presentation transcript:

Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class Fields:  Variables of an object that store data Methods:  Parts of an object that define instructions Constructors:  Special methods that initialize instance fields

OOP Review Constructors  These are special methods that are most commonly used to initialize instance fields.  Definitions of constructors must obey these rules: The name of the constructor must match (exactly) the name of the class. The constructor must not be given any return type, and it must not contain any return statement. If the class extends another class that has no zero-argument constructor, then the first line of the constructor must be a call to the super(<args) constructor.  Constructors are called differently from other methods, by using the new keyword: MyClass myObject = new MyClass( );

OOP Review Instance vs. static fields  If a field is not declared to be static, it is an instance field.  To decide if a field should be static or instance, consider: Does each object potentially have a different value for that field?  If so, the field should be instance.  For example, BankAccount objects might all have different values for a balance field. Do all objects share the same value for that field?  If so, the field should be static.  For example, all BankAccount objects might share the same value for the interest rate. So the interestRate field should be declared static.  If a class defines an instance field, every object (or variable) has a separate location in memory to store data for that field.  If a class defines a static field, every object has a reference to a single location in memory, shared by all objects of that class, to store data for that field.

OOP Review Instance vs. static methods  Instance and static methods differ in how they are called: To call an instance method: myObject.instanceMethod(); To call a static method: MyClass.staticMethod(); Technically, it is OK to call a static method using an object rather than a class name. The compiler will give you a warning, but not an error. The opposite is an error: to call an instance method, you MUST supply an object name.  Instance methods have an implicit parameter, the object which was used to call the method. Static methods have no implicit parameter. In the example above, myObject becomes the implicit parameter for instanceMethod(). Instance methods can use the this keyword to refer to the implicit parameter.  Static methods CANNOT refer to instance fields, or to the this keyword. Because static methods don’t have an implicit parameter, this doesn’t mean anything inside the body of a static method. However, it is OK for instance methods to refer to static fields.

OOP Review Access specifiers:  public: any class can refer to it  private: only the class that defines it can refer to it  protected: the class that defines it, and any direct or indirect subclass, can refer to it  : package-level access, meaning that any class in the same directory can refer to it Specifiers can be used for:  Classes or interfaces  Methods  fields

OOP Review Reference Semantics  Object variables in Java are references to locations in memory  Shallow copies (via assignment or parameter passing) creates duplicate references, not duplicate objects  If the data of one object reference is changed, it changes the data of any other reference to the same location  Primitive types (int, short, double, etc.) do not use reference semantics

OOP Review Inheritance  Subclasses can extend superclasses A subclass inherits all the fields and methods of the superclass  Subclasses can override definitions in the superclass If a subclass defines a new version of an inherited method or field, it overrides the superclass’s version  Final Final methods and fields can’t be overridden Final classes can’t be extended  Abstract Abstract methods must be overridden to be usable If a class contains an abstract method, it must be abstract If a class is abstract, it can’t have a constructor

OOP Review Interfaces  Much like an abstract class that has all abstract methods  It declares, but does not define, a set of methods  Classes that define the methods implement the interface

OOP Review Polymorphism  A variable of a class type can hold an object of that type or any indirect subclass type  A variable of an interface type can hold an object of any implementing type  The behavior of a method call on a class or interface variable depends on the run-time type of the object that the variable is a reference to.

10 Identifiers: Keywords keyword: An identifier that you cannot use, because it already has a reserved meaning in the Java language. Complete list of Java keywords: abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized