A Formal Presentation On Writing Classes With Rules and Examples CSC 123 Fall 2018 Howard Rosenthal

Size: px
Start display at page:

Download "A Formal Presentation On Writing Classes With Rules and Examples CSC 123 Fall 2018 Howard Rosenthal"

Transcription

1 A Formal Presentation On Writing Classes With Rules and Examples CSC 123 Fall 2018 Howard Rosenthal

2 Lesson Goals Defining a Class Defining Instance Variables Writing Methods The Object Reference this The tostring and equals Methods static Members of a Class Much of the information in this section is adapted from: Java Illuminated 5 TH Edition, Anderson, Julie and Franceschi Herve, Jones and Bartlett, 2019 Starting Out With Objects From Control Structures Through Objects, Gaddis, Tony, Pearson Publishing,

3 Why Do We Have User-Defined Classes The class methods are responsible for the validity of the data. Implementation details can be hidden. A class can be reused. Classes can emulate real world objects We move beyond primitive values and procedural calculations Note: The client of a class is a program that instantiates objects and calls the methods of the class 3

4 Basic class Syntax accessmodifier class ClassName // class definition goes here Conventions Use a noun for the class name. Begin the class name with a capital letter 4

5 Basic Terminology Fields Instance variables: the data for each object Class data: static data that all objects share Members Fields and methods Access Modifier Determines access rights for the class and its members Defines where the class and its members can be used 5

6 public vs. private Classes are usually declared to be public We need to be able to access them from other classes or they are useless Instance variables are usually declared to be private. It is good practice to make most instance variables private and only access them through meth0ds of the class We minimize the number of instance values and use methods to obtain derived values Methods that will be called by the client of the class are usually declared to be public. Methods that will be called only by other methods of the class are usually declared to be private. APIs (name and parameters of methods as well as return values) of methods are published (made known) so that clients will know how to instantiate objects and call the methods of the class. 6

7 Encapsulation And Information Hiding Another term that is often associated with encapsulation is information hiding. Many programmers regard encapsulation and information hiding as synonyms. However, object-oriented purists would define encapsulation as the technique that bundles data and methods into one unit and information hiding as the principle that hides the implementation of a class. Giving an instance variable private access has its advantages. Public access implies that the field is visible and can be changed from any other program with a simple assignment statement. Information hiding allows classes to be revised without affecting the code of its clients In general, information hiding is the principle that hides implementation details from a client class. When implementation details are hidden, all access to the attributes of a class is through its public methods. Remember, classes encapsulate but classes do not necessarily enforce information hiding. Restricting access within a class affords information hiding. 7

8 Instance Variables Each instance of a class has its own set of fields, which are known as instance fields. You can create several instances of a class and store different values in each instance s fields. The methods that operate on an instance of a class are known as instance methods. 8

9 The Syntax Of An Instance Variable Syntax: accessmodifier datatype identifierlist; datatype can be a primitive data type or a class type. identifierlist can contain: One or more variable names of the same data type Multiple variable names separated by commas Initialization values Optionally, instance variables can be declared as final. By convention capitalize any final variables. Conventions Begin the instance variable identifier with a lowercase letter and capitalize internal words as per our normal practices Define instance variables as private so that only the methods of the class will be able to set or change their values. 9

10 Methods In Classes When you write methods for any class follow all the same rules that are normally the case, including those related to overloading. If the method is going to be accessed from another class don t use the word static Special rules relating using static Static methods are convenient for many tasks because they can be called directly from the class, as needed. A static method is created by placing the key word static after the access specifier in the method header. When a class contains a static method, it isn t necessary for an instance of the class to be created in order to execute the method. The only limitation that static methods have is that they cannot refer to non-static members of the class. This means that any method called from a static method must also be static. It also means that if the method uses any of the class s fields, they must be static as well. 10

11 Constructors Constructors are special methods that are called automatically when an object is instantiated using the new keyword Constructors do not have any return value in the header (nor in the body) A constructor is automatically executed each time an object of the class is instantiated. A constructor method has the same name as the class with appendage.java. The only exception to this is when you place multiple classes into the class containing main, in which case the file has the same name as the class containing main. A class can have several constructors. Each constructor must have a different number of parameters or parameters of different types just like an overloaded method. The job of the class constructors is to initialize the instance variables of the new object. If and only if no constructor is specified a default constructor is used. The default constructor for a published class should be published as part of the class API 11

12 The UML For Auto - model : String - int : milesdriven - double : gallonsof Gas Auto + Auto(): + Auto(String : startmodel, int : startmilesdriven, double : startgallonsofgas): + getmodel() : String + getmilesdrivenl() : int + getgallonsofgas() : double + setmodel(string : model) : void + setmilesdrivenl(int : milesdriven) : void + setgallonsofgas(double : gallondnsofgas) : void + milespergallon() : double + tostring() : String + equals(object : obj) : boolean 12

13 Syntax Of The Constructor Syntax: public ClassName( parameter list ) // constructor body See AutoClientV1.java, AutoV1.java 13

14 Default Instance Values If the constructor does not assign values to the instance variables, they receive default values depending on the instance variable s data type. (just like arrays) The default constructor doesn t accept arguments. I. The only time that Java provides a default constructor is when you do not write your own constructor for a class. 14

15 A Note About Scope Instance variables have class scope A constructor or method of a class can directly refer to instance variables. We will review the use of the keyword this in a few slides Methods also have class scope A constructor or method of a class can call other methods of a class (without using an object reference). This is just what we have done up to now with methods A method's parameters have local scope A method can directly access its parameters, -but one method's parameters cannot be accessed by other methods. A method can define variables which also have local scope A method can access its local variables, - but one method's local variables cannot be accessed by other methods. 15

16 Accessor Methods Clients cannot directly access private instance variables, so classes provide public accessor methods with this standard form: public returntype getinstancevariable( ) return instancevariable; Where (returntype is the same data type as the instance variable.) A simple example looks like this public int getcounter( ) return counter; See AutoClientV2.java, AutoV2.java 16

17 Mutator Methods Mutator methods allow the client to change the values of instance variables. They have this general form: public void setinstancevariable( datatype newvalue ) // if newvalue is valid, // assign newvalue to the instance variable A simple example: public void setmilesdriven( int newmilesdriven ) if ( newmilesdriven >= 0 ) milesdriven = newmilesdriven; else System.out.printf( "Miles driven "cannot be negative.\n" ); System.out.printf( "Value not changed.\n" ); See AutoClientV3.java, AutoV3.java and AutoClientV4.java, AutoV4.java (adding some useful methods) 17

18 This Object Reference this How does a method know which object's data to use? The reference this refers to the current instance of a class, the object currently being used. this is an implicit parameter sent to methods. this is an object reference to the object for which the method was called. When a method refers to an instance variable name, this is implied. Thus, variablename is understood to be this.variablename this can be used in any method of the class, including any constructor. Example: public void setinstancevariable(datatype instancevariablename ) this.instancevariablename = instancevariablename; In the case above the true instance variable gets the value of the parameter 18

19 Using this (1) public void setinstancevariable(datatype instancevariablename ) this.instancevariablename = instancevariablename; Or in a constructor public newobject (datatype1 IVN1, datatype2 IVN2) this.ivn1 = IVN1; this.ivn2 = IVN2; See AutoClientV5.java, AutoV5.java 19

20 Using this (2) Some programmers like to use this pattern: public ClassName setinstancevariable( datatype instancevariablename ) this.instancevariablename = instancevariablename; return this; Example: public Auto setmodel( String model ) this.model = model; return this; See AutoClientV5.java, AutoV5.java 20

21 Calling A Constructor From A Constructor (1) If one constructor calls another constructor, no other statements can precede that call. So look at the following class: public class Room private int length; private int width; private int height; private double gallonsofpaint; public Room() length = 9; width =12; height = 8; public Room(int length,int width,int height) // three-argument constructor this.length = length; this.width = width; this.height = height; You can write it alternatively as shown on the next slide 21

22 Calling A Constructor From A Constructor (2) So look at the following rewritten class: public class Room private int length; private int width; private int height; private double gallonsofpaint; public Room() this(9. 12, 8);//does the same thing public Room(int length,int width,int height) // three-argument constructor this.length = length; this.width = width; this.height = height; 22

23 tostring (1) The tostring() method returns a String representing the data of an object Clients can call tostring() explicitly by coding the method call. Clients can call tostring() implicitly by using an object reference where a String is expected. Example client code: Auto compact = new Auto( ); // instantiate an object // explicit tostring call System.out.printf( %s\n, compact.tostring( ) ); // implicit tostring call System.out.printf( %s\n, compact ); 23

24 tostring (2) Most classes can benefit from having a method named tostring, which is implicitly called under certain circumstances. Typically, the method returns a string that represents the state of an object. Every class has a default tostring() if one isn t provided In general the default it is not all that useful, so we write what is called an overriding method When we look at inheritance we will be understand what is inherited and what is new in tostring() See AutoClientV6.java, AutoV6.java 24

25 Using equals() In Classes The purpose of equals() is to determine if the data in another object is equal to the data in this object. You cannot determine whether two objects contain the same data by comparing them with the == operator. Instead, the class must have a method such as equals for comparing the contents of objects. Like tostring(), equals(object obj) is inherited by every object. However, we almost always want to write an overriding method for your class, based on your definition of equality See AutoClientV6.java, AutoV6.java 25

26 instanceof Is A Special Binary Operator We can use the instanceof operator to determine if an object reference refers to an object of a particular class. instanceof is not really a method, rather it is a special binary operator Syntax: objectreference instanceof ClassName evaluates to true if objectreference is of ClassName type; false otherwise. if (! ( obj instanceof AutoV6 ) ) return false; else o o 0 See AutoClientV6.java, AutoV6.java 26

27 What Does Overriding Mean In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. We will learn about this in greater detail when we discuss classes and subclasses Some programmer use annotation, (see V6) but it is not required 27

28 Rules For Overriding In Java The argument list should be exactly the same as that of the overridden method. The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass. The access level cannot be more restrictive than the overridden method's access level. For example: If the superclass method is declared public then the overriding method in the sub class cannot be either private or protected. Instance() methods can be overridden only if they are inherited by the subclass. A method declared final cannot be overridden. A method declared static cannot be overridden but can be re-declared. If a method cannot be inherited, then it cannot be overridden. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. A subclass in a different package can only override the non-final methods declared public or protected. An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method. Constructors cannot be overridden. 28

29 static Variables static variables are also known as class variables. A static variable belongs to the class and not to any particular object; a class or static variable is shared by all objects of the class. One copy of a static variable is created per class. static variables are not associated with an object. static constants are often declared as public. When they are they are accessed as classname.variablename i.e. Math.PI To define static data, include the keyword static in its definition: Syntax: accessspecifier static datatype variablename ; Example: private static int countautos = 0; 29

30 Some Uses of static Variables If the class is employee, keep track of the total number of employees and total payroll with static variables If the class is car, use a static variable to keep track of the total number of cars sold Keep track of the total number of letters going though a post office with a static variable 30

31 static Methods static methods are also called class methods Often defined to access and change static variables static methods cannot access instance variables: static methods are associated with the class, not with any object. static methods do not have access to the implicit parameter this remember this is a reference to the object. static methods cannot access non-static methods To be accessed outside the class the method must be public You access a class method as follows classname.method(parameter list) classname is not needed within the class, only for external access Example: Math.sqrt(value) See AutoClientV7.java, AutoV7.java 31

32 Access Restrictions For static And Non-static Methods Exception: A static method may be called whether or not an object of the class exists, but a static method cannot invoke an instance method except via an object. For example, main is static, but it can invoke nonstatic method of a class via the object reference. 32

33 Some Notes On Garbage Collection (1) The Problem If unreferenced objects accumulate, a gargantuan program with thousands of objects could run out of memory. Even if a program does not run out of memory, if too much memory is allocated, program performance can deteriorate. We have seen how immutable objects, such as String objects, can quickly create large numbers of unreferenced objects. Fortunately, Java manages memory automatically, and this helps alleviate any potential disaster. The Java Virtual Machine automatically reclaims all memory allocated to unreferenced objects for future use. In other words, if an object is no longer referenced and accessible, the memory allocated to that object is freed and made available for the creation of other objects. This clean-up process is called garbage collection. 33

34 Some Notes On Garbage Collection (2) Sometimes we create an object for short term use A memory leak occurs when an application maintains references to obsolete objects To avoid a memory leak, set any reference to an object equal to null when that object is no longer going to be used. A reference with value null refers to no object and holds no address; it is called a void reference. Once the object has no reference to it the JVM automatically frees up the space Remember: Managing memory use is an important part of a programmer s job. The programmer must work in tandem with Java s automatic garbage collection as described above to ensure that there are no memory leaks. 34

35 Programming Exercise 1 - Overview CheckingAccount We will follow a structured approach to creating a CheckingAccount class and a test driver for the class. Please don t turn to the next page until told to. There is a four step process that we will follow: ØRequirements Definition ØDesign ØImplementation ØTest 35

36 PE1 - Requirements Analysis - Defining The Core Data (1) Think of three data items that will be part of a checking account. 36

37 PE1 - Requirements Analysis - Defining The Core Data (2) Think of three data items that will be part of a checking account. Answer: For this example here are three basic variables: Account number Name of account holder Current balance These variables are also referred to as the state of the class Next Question: What behavior does a checking account have? 37

38 PE1 - Requirements Analysis - Identifying The Behaviors What behavior does a checking account have? Ø Accept a deposit Ø Process a check Ø Get the current balance So now we have the basic structure of the class Data Account number Name of account holder Current balance Constructor Create the object; initialize the three data items Methods Accept a deposit Process a check Get the current balance 38

39 PE1 - Requirements Analysis - Defining The Behaviors The requirements describe what each method does. The method to accept a deposit adds an amount to the current balance. The current balance can be negative or positive. The method to process a check subtracts the amount of the check from the current balance. Overdrafts are allowed, so the balance can become negative. However, if the balance is less than $ before the check is processed, $0.15 is charged for each check. To simplify the program, assume that all data is correct (so the methods do not check for errors). 39

40 PE1 - Implementing The Constructor(s) (1) The account holder and the account number should each be a String reference because it is not expected to take part in arithmetic operations. Sometimes account numbers contain dashes or other non-digit characters. The balance is kept in terms of dollars and cents, so should be a double. To enforce good modularity, remember that instance variables should be private. So far, the CheckingAccount class looks like this: public class CheckingAccount // instance variables private String accountnumber; private String accountholder; private double balance; constructors methods What does the constructor look like?? 40

41 PE1 - Implementing The Constructor(s) (2) The structure looks like this: CheckingAccount( String accnumber, String holder, double start ) initialization of data 41

42 PE1 - Implementing The Constructor(s) (3) The complete constructor looks like this: CheckingAccount( String accountnumber, String accountholder, double balance ) this.accountnumber = accountnumber ; this.accountholder = accountholder ; this.balance = balance ; 42

43 PE1 - Incremental Testing First Testing Program (1) At this point you may want to write an incremental tester to make sure that your constructor works, or at least compiles Create a simple tester class called CheckingAccountTester 43

44 PE1 - The Original Tester public class CheckingAccountTester public static void main( String[] args ) CheckingAccount account1 = new CheckingAccount( "123", "Bob", ); Now add a tostring method to the CheckingAccount class 44

45 PE1 - CheckingAccount With A tostring() (1) public class CheckingAccount // instance variables private String accountnumber; private String accountholder; private double balance; public CheckingAccount( String accountnumber, String accountholder, double balance ) this.accountnumber = accountnumber ; this.accountholder = accountholder ; this.balance = balance ; public String tostring() return String.format( Account: %s\n Owner: %s\n Balance: $%.2f\n, accountnumber, accountholder, balance) ; 45

46 PE1 - An Updated Tester With tostring() public class CheckingAccountTester public static void main( String[] args ) CheckingAccount account1 = new CheckingAccount( "123", "Bob", 100 ); System.out.printf( %s\n, account1 ); CheckingAccount account2 = new CheckingAccount( "007", "James", ); System.out.printf(( %s\n, account2 ); Now write the three methods in the requirements, and test them via the Tester program See CheckingAccount.java and CheckingAccountTester.java 46

47 Programming Exercise 2 CellPhone and CellPhoneTester Wireless Solutions, Inc., is a business that sells cell phones and wireless service. You are a programmer in the company s information technology (IT) department, and your team is designing a program to manage all of the cell phones that are in inventory. You have been asked to design a class that represents a cell phone. The data that should be kept as fields in the class are as follows: The name of the phone s manufacturer will be assigned to the manufact field. The phone s model number will be assigned to the model field. The phone s retail price will be assigned to the retailprice field. The class will also have the following methods: A constructor that accepts arguments for the manufacturer, model number and retail price. A setmanufact method that accepts an argument for the manufacturer. This method will allow us to change the value of the manufact field after the object has been created, if necessary. A setmodel method that accepts an argument for the model. This method will allow us to change the value of the model field after the object has been created, if necessary. A setretailprice method that accepts an argument for the retail price. This method will allow us to change the value of the retailprice field after the object has been created, if necessary. A getmanufact method that returns the phone s manufacturer. A getmodel method that returns the phone s model number. A getretailprice method that returns the phone s retail price. A tostring method that prints out the phone s manufacture, model and price. Write a program that creates a CellPhone object and tests all the methods 47

48 Programming Exercise 3 CheckingAccountTester2 Modify CheckingAccountTester to CheckingAccountTester2 The new tester creates an array of 5 checking accounts For each account Read in a account number, account owner and initial balance for each account Read in a deposit and make it Read in a check and process it Print out the account information 48

Reviewing Java Implementation Of OO Principles CSC Spring 2019 Howard Rosenthal

Reviewing Java Implementation Of OO Principles CSC Spring 2019 Howard Rosenthal Reviewing Java Implementation Of OO Principles CSC 295-01 Spring 2019 Howard Rosenthal Course References Materials for this course have utilized materials in the following documents. Additional materials

More information

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Define and discuss abstract classes Define and discuss abstract methods Introduce polymorphism Much of the information

More information

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Inheritance CSC 123 Fall 2018 Howard Rosenthal Inheritance CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Defining what inheritance is and how it works Single Inheritance Is-a Relationship Class Hierarchies Syntax of Java Inheritance The super Reference

More information

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Understand Object Oriented Programming The Syntax of Class Definitions Constructors this Object Oriented "Hello

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

Interfaces CSC 123 Fall 2018 Howard Rosenthal

Interfaces CSC 123 Fall 2018 Howard Rosenthal Interfaces CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Defining an Interface How a class implements an interface Using an interface as a data type Default interfaces Implementing multiple interfaces

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

COP 3330 Final Exam Review

COP 3330 Final Exam Review COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time

More information

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming Overview of OOP Object Oriented Programming is a programming method that combines: a) Data b) Instructions for processing that data into a self-sufficient object that can be used within a program or in

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

OBJECT ORİENTATİON ENCAPSULATİON

OBJECT ORİENTATİON ENCAPSULATİON OBJECT ORİENTATİON Software development can be seen as a modeling activity. The first step in the software development is the modeling of the problem we are trying to solve and building the conceptual

More information

What is Inheritance?

What is Inheritance? Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting

More information

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW Prepared by: Dr. Abdallah Mohamed, AOU-KW 1 1. Introduction 2. Objects and classes 3. Information hiding 4. Constructors 5. Some examples of Java classes 6. Inheritance revisited 7. The class hierarchy

More information

Introduction to Inheritance

Introduction to Inheritance Introduction to Inheritance James Brucker These slides cover only the basics of inheritance. What is Inheritance? One class incorporates all the attributes and behavior from another class -- it inherits

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

INHERITANCE. Spring 2019

INHERITANCE. Spring 2019 INHERITANCE Spring 2019 INHERITANCE BASICS Inheritance is a technique that allows one class to be derived from another A derived class inherits all of the data and methods from the original class Suppose

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness. Methods There s a method in my madness. Sect. 3.3, 8.2 1 Example Class: Car How Cars are Described Make Model Year Color Owner Location Mileage Actions that can be applied to cars Create a new car Transfer

More information

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass

More information

CS260 Intro to Java & Android 03.Java Language Basics

CS260 Intro to Java & Android 03.Java Language Basics 03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance

More information

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017 Inheritance Lecture 11 COP 3252 Summer 2017 May 25, 2017 Subclasses and Superclasses Inheritance is a technique that allows one class to be derived from another. A derived class inherits all of the data

More information

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10A OOP Fundamentals By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Definition Pointers vs containers Object vs primitives Constructors Methods Object class

More information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L Inheritance Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 9.4 1 Inheritance Inheritance allows a software developer to derive

More information

Computer Science II (20073) Week 1: Review and Inheritance

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

Data Structures (list, dictionary, tuples, sets, strings)

Data Structures (list, dictionary, tuples, sets, strings) Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in brackets: l = [1, 2, "a"] (access by index, is mutable sequence) Tuples are enclosed in parentheses: t = (1, 2, "a") (access

More information

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at  Interfaces. Today Book-keeping Inheritance Subscribe to sipb-iap-java-students Interfaces Slides and code at http://sipb.mit.edu/iap/java/ The Object class Problem set 1 released 1 2 So far... Inheritance Basic objects,

More information

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017 Overview of OOP Dr. Zhang COSC 1436 Summer, 2017 7/18/2017 Review Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in square brackets: l = [1, 2, "a"] (access by index, is mutable

More information

EEE-425 Programming Languages (2013) 1

EEE-425 Programming Languages (2013) 1 2 Learn about class concepts How to create a class from which objects can be instantiated Learn about instance variables and methods How to declare objects How to organize your classes Learn about public

More information

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview Introduction to Visual Basic and Visual C++ Introduction to Java Lesson 13 Overview I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Overview JDK Editions Before you can write and run the simple

More information

Java: introduction to object-oriented features

Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer

More information

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass? 1. Overriding Methods A subclass can modify behavior inherited from a parent class. A subclass can create a method with different functionality than the parent s method but with the same: Name Return type

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber Chapter 10 Inheritance and Polymorphism Dr. Hikmat Jaber 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2) Summary: Chapters 1 to 10 Sharma Chakravarthy Information Technology Laboratory (IT Lab) Computer Science and Engineering Department The University of Texas at Arlington, Arlington, TX 76019 Email: sharma@cse.uta.edu

More information

Chapter 3 Classes. Activity The class as a file drawer of methods. Activity Referencing static methods

Chapter 3 Classes. Activity The class as a file drawer of methods. Activity Referencing static methods Chapter 3 Classes Lesson page 3-1. Classes Activity 3-1-1 The class as a file drawer of methods Question 1. The form of a class definition is: public class {

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba Laboratory Session: Exercises on classes Analogy to help you understand classes and their contents. Suppose you want to drive a car and make it go faster by pressing down

More information

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U C A N A D I A N I N T E R N A T I O N A L S C H O O L O F H O N G K O N G INHERITANCE & POLYMORPHISM P2 LESSON 12 P2 LESSON 12.1 INTRODUCTION inheritance: OOP allows a programmer to define new classes

More information

PROGRAMMING LANGUAGE 2

PROGRAMMING LANGUAGE 2 31/10/2013 Ebtsam Abd elhakam 1 PROGRAMMING LANGUAGE 2 Java lecture (7) Inheritance 31/10/2013 Ebtsam Abd elhakam 2 Inheritance Inheritance is one of the cornerstones of object-oriented programming. It

More information

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2 CITS2200 Data Structures and Algorithms Topic 2 Java Primer Review of Java basics Primitive vs Reference Types Classes and Objects Class Hierarchies Interfaces Exceptions Reading: Lambert and Osborne,

More information

CH. 2 OBJECT-ORIENTED PROGRAMMING

CH. 2 OBJECT-ORIENTED PROGRAMMING CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED

More information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Chapter 12 OOP: Creating Object-Oriented Programs McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter Objectives - 1 Use object-oriented terminology correctly Create a two-tier

More information

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS Chapter 1 : Chapter-wise Java Multiple Choice Questions and Answers Interview MCQs Java Programming questions and answers with explanation for interview, competitive examination and entrance test. Fully

More information

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Lecture Notes Chapter #9_b Inheritance & Polymorphism Lecture Notes Chapter #9_b Inheritance & Polymorphism Inheritance results from deriving new classes from existing classes Root Class all java classes are derived from the java.lang.object class GeometricObject1

More information

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

CS 251 Intermediate Programming Inheritance

CS 251 Intermediate Programming Inheritance CS 251 Intermediate Programming Inheritance Brooke Chenoweth University of New Mexico Spring 2018 Inheritance We don t inherit the earth from our parents, We only borrow it from our children. What is inheritance?

More information

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information

More information

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Covered in this chapter Classes Objects Methods Parameters double primitive type } Create a new class (GradeBook) } Use it to create an object.

More information

EEE-425 Programming Languages (2013) 1

EEE-425 Programming Languages (2013) 1 2 Learn about class concepts How to create a class from which objects can be instantiated Learn about instance variables and methods How to declare objects How to organize your classes Learn about public

More information

Java Fundamentals (II)

Java Fundamentals (II) Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class

More information

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles, Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design

More information

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs. Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar

More information

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com CHAPTER 8 OBJECTS AND CLASSES Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/2011 Chapter Goals To understand the concepts of classes, objects and encapsulation To implement instance variables,

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

CS112 Lecture: Defining Instantiable Classes

CS112 Lecture: Defining Instantiable Classes CS112 Lecture: Defining Instantiable Classes Last revised 2/3/05 Objectives: 1. To describe the process of defining an instantiable class 2. To discuss public and private visibility modifiers. Materials:

More information

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods. Inheritance Inheritance is the act of deriving a new class from an existing one. Inheritance allows us to extend the functionality of the object. The new class automatically contains some or all methods

More information

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10B Class Design By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Encapsulation Inheritance and Composition is a vs has a Polymorphism Information Hiding Public

More information

Chapter 10 Introduction to Classes

Chapter 10 Introduction to Classes C++ for Engineers and Scientists Third Edition Chapter 10 Introduction to Classes CSc 10200! Introduction to Computing Lecture 20-21 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this

More information

Cpt S 122 Data Structures. Introduction to C++ Part II

Cpt S 122 Data Structures. Introduction to C++ Part II Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function

More information

3.1 Class Declaration

3.1 Class Declaration Chapter 3 Classes and Objects OBJECTIVES To be able to declare classes To understand object references To understand the mechanism of parameter passing To be able to use static member and instance member

More information

Software Development With Java CSCI

Software Development With Java CSCI Software Development With Java CSCI-3134-01 D R. R A J S I N G H Outline Week 8 Controlling Access to Members this Reference Default and No-Argument Constructors Set and Get Methods Composition, Enumerations

More information

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages Preliminaries II 1 Agenda Objects and classes Encapsulation and information hiding Documentation Packages Inheritance Polymorphism Implementation of inheritance in Java Abstract classes Interfaces Generics

More information

Inheritance (continued) Inheritance

Inheritance (continued) Inheritance Objectives Chapter 11 Inheritance and Polymorphism Learn about inheritance Learn about subclasses and superclasses Explore how to override the methods of a superclass Examine how constructors of superclasses

More information

Super-Classes and sub-classes

Super-Classes and sub-classes Super-Classes and sub-classes Subclasses. Overriding Methods Subclass Constructors Inheritance Hierarchies Polymorphism Casting 1 Subclasses: Often you want to write a class that is a special case of an

More information

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

More About Objects. Zheng-Liang Lu Java Programming 255 / 282 More About Objects Inheritance: passing down states and behaviors from the parents to their children. Interfaces: requiring objects for the demanding methods which are exposed to the outside world. Polymorphism

More information

ENCAPSULATION AND POLYMORPHISM

ENCAPSULATION AND POLYMORPHISM MODULE 3 ENCAPSULATION AND POLYMORPHISM Objectives > After completing this lesson, you should be able to do the following: Use encapsulation in Java class design Model business problems using Java classes

More information

Operators and Expressions

Operators and Expressions Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions

More information

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Things to Review Review the Class Slides: Key Things to Take Away Do you understand

More information

Rules and syntax for inheritance. The boring stuff

Rules and syntax for inheritance. The boring stuff Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for

More information

CS1150 Principles of Computer Science Objects and Classes

CS1150 Principles of Computer Science Objects and Classes CS1150 Principles of Computer Science Objects and Classes Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Object-Oriented Thinking Chapters 1-8

More information

Inheritance. Transitivity

Inheritance. Transitivity Inheritance Classes can be organized in a hierarchical structure based on the concept of inheritance Inheritance The property that instances of a sub-class can access both data and behavior associated

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed 1 1 COMP200 INHERITANCE OOP using Java, from slides by Shayan Javed 2 Inheritance Derive new classes (subclass) from existing ones (superclass). Only the Object class (java.lang) has no superclass Every

More information

Outline. Object-Oriented Design Principles. Object-Oriented Design Goals. What a Subclass Inherits from Its Superclass?

Outline. Object-Oriented Design Principles. Object-Oriented Design Goals. What a Subclass Inherits from Its Superclass? COMP9024: Data Structures and Algorithms Week One: Java Programming Language (II) Hui Wu Session 1, 2014 http://www.cse.unsw.edu.au/~cs9024 Outline Inheritance and Polymorphism Interfaces and Abstract

More information

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. Data structures Collections of related data items. Discussed in depth in Chapters 16 21. Array objects Data

More information

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini 24. Inheritance Java Fall 2009 Instructor: Dr. Masoud Yaghini Outline Superclasses and Subclasses Using the super Keyword Overriding Methods The Object Class References Superclasses and Subclasses Inheritance

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Dr. M. G. Abbas Malik Assistant Professor Faculty of Computing and IT (North Jeddah Branch) King Abdulaziz University, Jeddah, KSA mgmalik@kau.edu.sa www.sanlp.org/malik/cpit305/ap.html

More information

STUDENT LESSON A5 Designing and Using Classes

STUDENT LESSON A5 Designing and Using Classes STUDENT LESSON A5 Designing and Using Classes 1 STUDENT LESSON A5 Designing and Using Classes INTRODUCTION: This lesson discusses how to design your own classes. This can be the most challenging part of

More information

CSE 401/M501 Compilers

CSE 401/M501 Compilers CSE 401/M501 Compilers Code Shape II Objects & Classes Hal Perkins Autumn 2018 UW CSE 401/M501 Autumn 2018 L-1 Administrivia Semantics/type check due next Thur. 11/15 How s it going? Reminder: if you want

More information

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Making New instances of Classes

Making New instances of Classes Making New instances of Classes NOTE: revised from previous version of Lecture04 New Operator Classes are user defined datatypes in OOP languages How do we make instances of these new datatypes? Using

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each)

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each) PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each) 1. An example of a narrowing conversion is a) double to long b) long to integer c) float to long d) integer to long 2. The key

More information

CS 1302 Chapter 9 (Review) Object & Classes

CS 1302 Chapter 9 (Review) Object & Classes CS 1302 Chapter 9 (Review) Object & Classes Reference Sections 9.2-9.5, 9.7-9.14 9.2 Defining Classes for Objects 1. A class is a blueprint (or template) for creating objects. A class defines the state

More information

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8 Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1 Inheritance you know a lot about an object by knowing its class for example what is a Komondor? http://en.wikipedia.org/wiki/file:komondor_delvin.jpg

More information

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS PAUL L. BAILEY Abstract. This documents amalgamates various descriptions found on the internet, mostly from Oracle or Wikipedia. Very little of this

More information

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are built on top of that. CMSC131 Inheritance Object When we talked about Object, I mentioned that all Java classes are "built" on top of that. This came up when talking about the Java standard equals operator: boolean equals(object

More information

Example: Count of Points

Example: Count of Points Example: Count of Points 1 public class Point { 2... 3 private static int numofpoints = 0; 4 5 public Point() { 6 numofpoints++; 7 } 8 9 public Point(int x, int y) { 10 this(); // calling Line 5 11 this.x

More information

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation COMP-202 Unit 8: Defining Your Own Classes CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation Defining Our Own Classes (1) So far, we have been creating

More information

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7 Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7 1 Problem Ralph owns the Trinidad Fruit Stand that sells its fruit on the street, and he wants to use a computer

More information