Agenda About Homework for BPJ lesson 36 About practice of last class

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
More about classes and objects Classes in Visual Basic.NET.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
UML Basics & Access Modifier
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Topic 4 Inheritance.
Inheritance A Review of Objects, Classes, and Subclasses.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
The GridWorld Case Study Program Section 1 - Overview of the Class Hierarchies Section 2 - The Actor Class Section 3 - The Rock and Flower Classes Section.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Classes, Interfaces and Packages
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Topics Instance variables, set and get methods Encapsulation
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
1 More About Derived Classes and Inheritance Chapter 9.
OOP: Encapsulation &Abstraction
Inheritance ITI1121 Nour El Kadri.
Objects as a programming concept
Lecture Notes – Inheritance (Ch 9-10)
CS230 Tutorial Week 3.
Inheritance in Java.
Review Session.
Phil Tayco Slide version 1.1 Created Oct 30, 2017
Computing with C# and the .NET Framework
CLASS DEFINITION (> 1 CONSTRUCTOR)
Can perform actions and provide communication
CSC 205 Java Programming II
Can perform actions and provide communication
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Can perform actions and provide communication
Inheritance Inheritance is a fundamental Object Oriented concept
Object-Oriented Programming
By Rajanikanth B OOP Concepts By Rajanikanth B
CS 200 More Classes Jim Williams, PhD.
Final and Abstract Classes
Agenda Dancing bug class Class vs object Comparable interface
Agenda About Homework for BPJ lesson 15 Overloading constructor
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Agenda About Homework for BPJ lesson 36 About practice of last class Review –superclass vs subclass Encapsulation Homework Quiz on 12/6

public class Red extends Green {. public int blue(double x). {. } public class Red extends Green { public int blue(double x) {...} public String s; private int i; } public class Green { public double peabody(double y) { return mm; } private boolean crunch( ) {...} private double mm; public long xx; }

Long data type long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int. super.peabody(11);

Is this legal. Red myObj = new Red(); boolean bb = myObj Is this legal? Red myObj = new Red(); boolean bb = myObj.Crunch(); Is this legal? the two above) Red myObj = new Red( ); int bb = myObj.blue(105.2);

Write code for the blue method that will printout the mm state variable. public int blue(double x) { double mmValue = super.peabody(x); System.out.println(mmValue); }

Write code for the blue method that will printout the xx state variable. public int blue(double x) { double xxValue = super.xx; System.out.println(xxValue); return 0; }

this keyword Within an instance method or a constructor, this is a reference to the current object. public class Point { public int x = 0; public int y = 0; public Point(int a, int b) { x = a; y = b; } } public Point(int x, int y) { this.x = x; this.y = y; } }

Using this with a Constructor public class Rectangle { private int x, y; private int width, height; public Rectangle() { this(0, 0, 0, 0); } public Rectangle(int width, int height) { this(0, 0, width, height); } public Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } ... }

Encapsulation Protecting an object’s data from code outside the class. Those instance variables marked as private. They can only be seen or modified through the use of public accessor and mutator methods. Encapsulation = information hiding

Actor class public class Actor { private Grid<Actor> grid; private Location location; private int direction; private Color color; public Actor() color = Color.BLUE; direction = Location.NORTH; grid = null; location = null; } public Color getColor() { return color; }

Flower class public class Flower extends Actor { private static final Color DEFAULT_COLOR = Color.PINK; private static final double DARKENING_FACTOR = 0.05; public Flower() setColor(DEFAULT_COLOR); } public Flower(Color initialColor) setColor(initialColor); public void act(){….}

public class Cube{ private int length; public int breadth; private int height; public Cube(int l, int b, int h) { length = l; breadth = b; height = h; } public int getVolume() { return (length * breadth * height); } public int getBreadth(){ return breadth; } public int getHeight(){ return height; } }

Golden nuggets of wisdom Private state variables and methods are not accessible from outside the class. But private things can only be accessed from within the class itself.

How to compare objects? Circle cir1 = new Circle(3.0); cir1 == cir2 ?????? Circle cir3; cir3 = cir1; cir1 == cir3 ??????

String str1 = “Hello”; String str2 = “Hello”; str 1 == str2 ???? String str3 = new String(“Hello”); str3 == str1?????

Inheritance Inheritance enables you to define a new class based upon an existing class. The new class is similar to the existing class, but has additional member variables and methods. This makes programming easier because you can build upon an existing class instead of starting out from scratch.

Is-a relationship In diagrams that show inheritance, an arrow points from the new class to the class it is based upon. The arrow is sometimes labeled "is a".

superclass subclass The class that is used to define a new class is called a parent class (or superclass or base class.) The class based on the parent class is called a child class (or subclass or derived class.)

Inheritance example BankAccount SavingsAccount checkingAccount Subclasses need all the methods and state variables of the superclass - BankAccount

BankAccount class public class BankAccount{ private double balance; public BankAccount(double amt){ balance = amt; } public void deposit(double amt){ balance += amt; } private void withdraw(double amt){ balance -= amt; }

public class SavingsAccount extends BankAccount { private double interestRate; public SavingsAccount(double amt, double rate){ super(amt); interestRate = rate; } Public void addInterest(){ double interest = getBalance()* interestRate/100; deposit(interest); } }

remarks 1. Private members of the superclass are not inherited by the subclass and can only be indirectly accessed. 2. Members that have default accessibility in the superclass are also not inherited by subclasses in other packages, as these members are only accessible by their simple names in subclasses within the same package as the superclass. 3. Since constructors and initializer blocks are not members of a class, they are not inherited by a subclass. 4. A subclass can extend only one superclass

GridWorld Actor Flower Rock Bug

Bug class If you don’t have the import statement for ex. Bug class, you have to write the whole path in order to use the class like Bug oBug = new info.gridworld.actor.Bug(); instead of Bug oBug = new Bug();

Abstract class An abstract class models an abstract concept. For example, a musical instrument is an abstract concept. An instrument is something that can be played, but there is no such thing an “instrument” instrument. There are however, flutes, drums, and cymbals. Abstract classes cannot be instantiated because they should not represent objects. They instead describe the more general details and actions of an object.

Homework Based on the BankAccount and SavingsAccount classes on this slide and answer all the questions in the word document homework-nov26 posted on the web