Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

21-Aug-14 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Inheritance Writing and using Classes effectively.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Starting Classes and Methods. Objects have behaviors In old style programming, you had: –data, which was completely passive –functions, which could manipulate.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
19-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
28-Jun-15 Everything You Ever Wanted To Know About Java O-O.
Chapter 10 Classes Continued
Access. Overview Today we will discuss: –How do we access fields and methods? –Why have access restrictions? –What can have access restrictions? –How.
16-Jul-15 Access. 2 Overview Questions covered in this talk: How do we access fields and methods? Why have access restrictions? What can have access restrictions?
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Object oriented design in Java (useful revision for final exam)
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 32 Thanks for Lecture Slides: C How to Program by Paul Deital &
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CMSC 202 Inheritance II. Version 10/102 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CSC 142 Computer Science II Zhen Jiang West Chester University
C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
10-Nov-15 Java Object Oriented Programming What is it?
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance and Access Control CS 162 (Summer 2009)
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Basic Object-Oriented Concepts – towards implementation CS3340.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Basic Object-Oriented Concepts
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP Basics Classes & Methods (c) IDMS/SQL News
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
BY:- TOPS Technologies
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Topic: Classes and Objects
OOP: Encapsulation &Abstraction
Introduction to Object-Oriented Concept
Chapter 3: Using Methods, Classes, and Objects
Java Programming Language
Java Inheritance.
Java Programming Language
Final and Abstract Classes
Classes and Methods 15-Aug-19.
Presentation transcript:

Basic Object-Oriented concepts

Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class describes those fields An object may have methods –The class describes those methods A class is like a template, or cookie cutter

Concept: Classes are like Abstract Data Types An Abstract Data Type (ADT) bundles together: –some data, representing an object or "thing" –the operations on that data Example: a Stack, with operations push, pop, etc. Classes enforce this bundling together

Example of a class class Employee { String name; double salary; void pay () { System.out.println ("Pay to the order of " + name + " $" + salary); }

Terminology instance = object field = variable method = function (approximately) sending a message to an object = calling a function (approximately)

Concept: Classes form a hierarchy Classes are arranged in a treelike structure The class at the root is called Object Every class, except Object, has a superclass A class may have several ancestors, in a line up to Object If a class doesn't specify its superclass, Object is assumed Every class may have one or more subclasses

Example of part of a hierarchy Object Throwable Exception ClassNotFoundException RuntimeException Error... An Exception is a Throwable is an Object.

C++ is different In C++ there may be more than one root –but not in Java! In C++ an object may have more than one parent (immediate superclass) –but not in Java! Java has a single, strict hierarchy

Concept: Objects inherit from their superclasses A class describes fields and methods Objects of that class have those fields and methods But an object also has: –the fields described in the class's superclasses –the methods described in the class's superclasses A class is not a complete description of its objects!

Example of inheritance class Person { String name; String age; void birthday () { age = age + 1; } class Employee extends Person { double salary; void pay () {...} } Every Employee has a name, age, and birthday method as well as a salary and a pay method.

Concept: Objects must be created int n; does two things: –it declares that n is an integer variable –it allocates space to hold a value for n Employee secretary; does one thing –it declares that secretary is type Employee secretary = new Employee ( ); allocates the space

Notation: How to declare and create objects Employee secretary; // declares secretary secretary = new Employee (); // allocates space Employee secretary = new Employee(); // does both But the secretary is still "blank" secretary.name = "Adele"; // dot notation secretary.birthday (); // sends a message

Notation: How to reference a field or method Inside a class, no dots are necessary –class Person {... age = age + 1;...} Outside a class, you need to say which object –if (john.age < 75) john.birthday (); If you don't have an object, you cannot use its fields or methods!

Concept: "this" object Inside a class, no dots are necessary, because –you are working on this object If you wish, you can make it explicit: –class Person {... this.age = this.age + 1;...} this is like an extra parameter to the method You usually don't need to use this

Concept: A variable can hold subclass objects Suppose B is a subclass of A A objects can be assigned to A variables B objects can be assigned to B variables B objects can be assigned to A variables, but A objects can not be assigned to B variables –Every B is also an A but not every A is a B You can cast: bVariable = (B) aObject; –In this case, Java does a runtime check

Example: Assignment of subclasses class Dog {... } class Poodle extends Dog {... } Dog myDog; Dog rover = new Dog (); Poodle yourPoodle; Poodle fifi = new Poodle (); myDog = rover; // ok yourPoodle = fifi; // ok myDog = fifi; //ok yourPoodle = rover; // illegal yourPoodle = (Poodle) rover; //runtime check

Concept: Methods can be overridden So birds can fly. Except penguins. class Bird extends Animal { void fly (String destination) { location = destination; } class Penguin extends Bird { void fly (String whatever) { } }

Concept: Don't call functions, send messages Bird someBird = pingu; someBird.fly ("South America"); Did pingu actually go anywhere? –You sent the message fly(...) to pingu –If pingu is a penguin, he ignored it –otherwise he used the method defined in Bird You did not directly call any method

Sneaky trick: You can still use overridden methods class FamilyMember extends Person { void birthday () { super.birthday (); // call overridden method givePresent (); // and add your new stuff }

Concept: Constructors make objects Every class has a constructor to make its objects Use the keyword new to call a constructor Example: secretary = new Employee (); Java provides a default constructor with no arguments You can write your own constructors The syntax is almost like writing a method

Syntax for constructors Instead of a return type and a name, just use the class name You can supply arguments Employee (String theName, double theSalary) { name = theName; salary = theSalary; }

Trick: Use the same name for a parameter as for a field A parameter overrides a field with the same name But you can use this. name to refer to the field Person (String name, int age) { this.name = name; this.age = age; } This is a very common convention

Internal workings: Constructor chaining If an Employee is a Person, and a Person is an Object, then when you say new Employee () –The Employee constructor calls the Person constructor –The Person constructor calls the Object constructor –The Object constructor creates a new Object –The Person constructor adds its own stuff to the Object –The Employee constructor adds its own stuff to the Person

The case of the vanishing constructor If you don't write a constructor for a class, Java provides one (the default constructor) The one Java provides has no arguments If you write any constructor for a class, Java does not provide a default constructor Adding a perfectly good constructor can break a constructor chain You may need to fix the chain

Example: Broken constructor chain class Person { String name; Person (String name) { this.name = name; } } class Employee { // here it tries to call new Person (); double salary; }

Fixing a broken constructor chain Special syntax: super(...) calls the superclass constructor When one constructor calls another, that call must be first Now you can only create Employees with names This is fair, because you can only create Persons with names class Employee { Employee (String name) { super(name); } double salary; }

Trick: one constructor calling another this(...) calls another constructor for this same class It is poor style to have the same code more than once If you call this(...), that call must be the first thing in your constructor class Something { Something (int x, int y, int z) { // do a lot of work here } Something ( ) { this (0, 0, 0); } }

Concept: You can control access class Person { public String name; private String age; protected double salary; public void birthday { age++; } } We will discuss access control shortly

Concept: Classes themselves can have fields and methods Usually a class describes fields and methods for its instances A class can have its own fields and methods These are called "class variables" and "class methods" There is one copy of a class variable, not one per object Use the special keyword static

Example of a class variable class Person { String name; int age; static int population; Person (String name) { this.name = name; this.age = 0; population++; }

Advice: You should restrict access Always, always strive for a narrow interface Follow the principle of information hiding: –the caller should know as little as possible about how the method does its job –the method should know little or nothing about where or why it is being called Make as much as possible private

Advice: Use setters and getters This way you still have some control Setters and getters have conventional names class Employee extends Person { private double salary; public void setSalary (double newSalary) { salary = newSalary; } public double getSalary () { return salary; } }

Kinds of access Java provides four levels of access: –public : available everywhere –protected : available within the package (in the same subdirectory) and to all subclasses –[default]: available within the package –private : only available within the class itself The default is called "package" visibility In small programs this isn't important...right?

The End