Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.

Slides:



Advertisements
Similar presentations
Classes And Objects II. Recall the LightSwitch Class class LightSwitch { boolean on = true; boolean isOn() { return on; } void switch() { on = !on; }
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Java Language Quick-Reference Guide B. Oracle10g: Java Programming B - 2 Console Output Java applications and applets can output simple messages to the.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Data Structures ADT List
1 CSE 331 Enumerated types ( enum ) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Chapter 3A Review boolean fun = true; if(fun) System.out.print(“yeah!”);
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand the.
Övning 4. Repetition göra egna klasser class Rektangel { private double längd; private double bredd; public Rektangel(double l, double b) { this.längd.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Friend. Revisit the Class Money class Money { public: Money(int D, int C); int getDollars(); int getCents(); private: int dollars; int cents; }; Overload.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Some Quick Reviews of Java. Background Java was developed in the early 90s by Sun Microsystems Java is a high-level language Java programs are portable.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
CompSci Data Types & Operations. CompSci The Plan  Overview  Data Types  Operations  Code Samples  ChangeMaker.java  PolarCoordinate.java.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Data Types & Operations 1 Last Edited 1/10/04CPS4: Java for Video Games Data Types.
Java Review I. What is a stream? What is a byte stream? what is a character stream? What types of files does a program will typically handle?
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.
Overriding toString()
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
int num = 22; if (num > 0) if (num % 5 == 0) System.out.println(num); else System.out.println(num + “ is negative”);
A: A: double “4” A: “34” 4.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Homework #3: Classes and Constructors
String str1 = JOptionPane.showInputDialog(“enter a string”); String str1 = JOptionPane.showInputDialog(“enter a string”); String str2 = JOptionPane.showInputDialog(“enter.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Data Structures and Algorithms revision
Lecture 12 Inheritance.
Operator Overloading CMSC 202.
Data Types & Operations
More About Objects and Methods CS140: Introduction to Computing 1 Savitch Chapter 6 10/16/13.
CS 302 Week 11 Jim Williams, PhD.
More Object Oriented Programming
Review Operation Bingo
בניית מחלקות.
תוכנה 1 תרגול מספר 11: Static vs. Dynamic Binding
null, true, and false are also reserved.
Introduction to Java Programming
COMPUTER 2430 Object Oriented Programming and Data Structures I
An overview of Java, Data types and variables
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
בניית מחלקות.
Inheritance.
JavaScript Reserved Words
مظفر بگ محمدی دانشگاه ایلام
Simple Classes in Java CSCI 392 Classes – Part 1.
Constructors under inheritance Variable Shadowing
COMPUTER 2430 Object Oriented Programming and Data Structures I
Module 2 - Part 1 Variables, Assignment, and Data Types
Agenda Types and identifiers Practice Assignment Keywords in Java
Topics OOP Review Inheritance Review Abstract Classes
CMP 167 Programming Methods One
Presentation transcript:

public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); obj1.update (45, ‘A’); obj2.update (32, ‘B’); obj2 = obj1; obj1.output_info(); obj2.output_info(); } What is the output? information=45, moreInformation=A

public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); obj1.update (45, ‘A’); obj2 = obj1; obj2.update (32, ‘B’); obj1.output_info(); obj2.output_info(); } What is the output?

public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); obj1.update (45, ‘A’); obj2 = obj1; obj2.update (32, ‘B’); obj1.output_info(); obj2.output_info(); } What is the output? information=32, moreInformation=B Why?

public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); obj1.update (45, ‘A’); obj2 = obj1; obj2.update (32, ‘B’); obj1.output_info(); obj2.output_info(); } What is the output? information=32, moreInformation=B Why? Both obj1 and obj2 point to the same object!

public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); obj2.update (32, ‘B’); ABC obj3 = obj2; obj3.update (40, ‘A’); obj1.output_info(); obj2.output_info(); } What is the output?

public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); obj2.update (32, ‘B’); ABC obj3 = obj2; obj3.update (40, ‘A’); obj1.output_info(); obj2.output_info(); } What is the output? information=42, moreInformation=A information=40, moreInformation=A

public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); changer (obj2); obj1.output_info(); obj2.output_info(); } public void changer(ABC obj) { obj.update (100, ‘A’); obj = new ABC (); obj.update (30, ‘C’); } What is the output?

public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); changer (obj2); obj1.output_info(); obj2.output_info(); } public void changer(ABC obj) { obj.update (100, ‘A’); obj = new ABC (); obj.update (30, ‘C’); } What is the output? information=42, moreInformation=A information=100, moreInformation=A

public class Money { private int dollar = 0; private int cent = 0; public Money() {} public Money(int in_d, int in_c) { dollar = in_d; cent = in_c; } } public class Test { public void main() { Money m1 = new (); Money m2 = new (100, 2); System.out.println(“The balance of m1 is: “+ m1); System.out.println(“The balance of m2 is: “+ m2); if (m1 == m2) System.out.println(“equal.”); else System.out.println(“not-equal.”); } What is the issue?

public class Money { private int dollar = 0; private int cent = 0; public Money() {} public Money(int in_d, int in_c) { dollar = in_d; cent = in_c; } } public class Test { public void main() { Money m1 = new (); Money m2 = new (100, 2); System.out.println(“The balance of m1 is: “+ m1); System.out.println(“The balance of m2 is: “+ m2); if (m1 == m2) System.out.println(“equal.”); else System.out.println(“not-equal.”); } What is the issue? need to override the equals and toString methods!

public class Money { private int dollar = 0; private int cent = 0; public Money() {} public Money(int in_d, int in_c) { dollar = in_d; cent = in_c; } public String toString() { return (dollar + “.” + cent);} public boolean equals(Money other_m) { return (dollar == other_m.dollar && cent == other_m.cent); } } public class Test { public void main() { Money m1 = new (); Money m2 = new (100, 2); System.out.println(“The balance of m1 is: “+ m1); System.out.println(“The balance of m2 is: “+ m2); if (m1.equals(m2)) System.out.println(“equal.”); else System.out.println(“not-equal.”); } What is the issue? need to override the equals and toString methods!

public class Money { private static int num_accounts = 0; private int dollar = 0; private int cent = 0; public Money() {num_accounts ++;} public Money(int in_d, int in_c) { dollar = in_d; cent = in_c; num_accounts ++;} public String toString() { return (dollar + “.” + cent);} public boolean equals(Money other_m) { return (dollar == other_m.dollar && cent == other_m.cent); } public void setDollar(int in_d) { dollar = in_d; } public void setCent (int in_c) { cent = in_c; } public static void setMoney(int in_d, int in_c) { setDollar(in_d); setCent(in_c); } public static void getDollar() { return dollar; } public static void getCent() { return cent;} public static void output_balance() { System.out.println(“Your balance is :” + getDollar() + getCent();} public static void resetCounter() { num_accounts = 0; } } What is the issue?

public class Money { private static int num_accounts = 0; private int dollar = 0; private int cent = 0; public Money() {num_accounts ++;} public Money(int in_d, int in_c) { dollar = in_d; cent = in_c; num_accounts ++;} public String toString() { return (dollar + “.” + cent);} public boolean equals(Money other_m) { return (dollar == other_m.dollar && cent == other_m.cent); } public void setDollar(int in_d) { dollar = in_d; } public void setCent (int in_c) { cent = in_c; } public static void setMoney(int in_d, int in_c) { setDollar(in_d); setCent(in_c); } public static void getDollar() { return dollar; } public static void getCent() { return cent;} public static void output_balance() { System.out.println(“Your balance is :” + getDollar() + getCent();} public static void resetCounter() { num_accounts = 0; } } What is the issue? static functions cannot access non-static members!!

public class Money { private static int num_accounts = 0; private int dollar = 0; private int cent = 0; public Money() {num_accounts ++;} public Money(int in_d, int in_c) { dollar = in_d; cent = in_c; num_accounts ++;} public String toString() { return (dollar + “.” + cent);} public boolean equals(Money other_m) { return (dollar == other_m.dollar && cent == other_m.cent); } public void setDollar(int in_d) { dollar = in_d; } public void setCent (int in_c) { cent = in_c; } public void setMoney(int in_d, int in_c) { setDollar(in_d); setCent(in_c); } public void getDollar() { return dollar; } public void getCent() { return cent;} public void output_balance() { System.out.println(“Your balance is :” + getDollar() + getCent();} public static void resetCounter() { num_accounts = 0; } }

public class A { public A() {} public void method1() {} public void method2() {} private final A method3() { return A(); } } What is the issue? public class B : public A { public B() {} private void method1() {} public boolean method2(int ii) { return false; } public B method3() { return B(); } }

public class A { public A() {} public void method1() {} public void method2() {} private final A method3() { return A(); } } What is the issue? public class B : public A { public B() {} private void method1() {} public boolean method2(int ii) { return false; } public B method3() { return B(); } }

public class A { public A() {} public void method1() {} public void method2() {} private final A method3() { return new A(); } } What is the issue? public class B extends A { public B() {} public void method1() {} public boolean method2(int ii) { return false; } }

public class A { private int val1 = 0; public A() {} public void method1() {} public void method2() {} private A method3() { return new A(); } } What is the issue? public class B extends A { public B() {} public void method1() {} public boolean method2(int ii) { val1 ++; return false; } public B method3() { return new B(); } } public Test { public void static main() { A obj1 = new A(); B obj2 = new B(); obj1.method1(); A obj3 = obj1.method3(); obj1 = obj2.method3(); }

public class A { private int val1 = 0; public A() {} public void method1() {} public void method2() {} private A method3() { return new A(); } } What is the issue? public class B extends A { public B() {} public void method1() {} public boolean method2(int ii) { val1 ++; return false; } public B method3() { return new B(); } } public Test { public void static main() { A obj1 = new A(); B obj2 = new B(); obj1.method1(); A obj3 = obj1.method3(); obj1 = obj2.method3(); }

public class Base { private int x, y = 0; public Base(int x_val, int y_val) { x = x_val; y = y_val; } public Base(int x_val) { x = x_val; } …… } public class Derived extends Base { private double x, y = 0; public Derived() { super(0.); this (0.0, 1.0); } public Derived(double x_val, double y_val) { x = x_val; y = y_val; } …… } What is the issue?

public class Base { private int x, y = 0; public Base(int x_val, int y_val) { x = x_val; y = y_val; } public Base(int x_val) { x = x_val; } …… } public class Derived extends Base { private double x, y = 0; public Derived() { super(0.); this (0.0, 1.0); } public Derived(double x_val, double y_val) { x = x_val; y = y_val; } …… } What is the issue?

public class Base { private int x, y = 0; public Base(int x_val, int y_val) { x = x_val; y = y_val; } public Base(int x_val) { x = x_val; } …… } public class Derived extends Base { private double x, y = 0; public Derived() { this (0.0, 1.0); } public Derived(double x_val, double y_val) { super (x_val, y_val); x = x_val; y = y_val; } …… } What is the issue?

public class Base { private int x, y = 0; public Base(int x_val, int y_val) { x = x_val; y = y_val; } public Base(int x_val) { x = x_val; } …… } public class Derived extends Base { private double x, y = 0; public Derived() { this (0.0, 1.0); } public Derived(double x_val, double y_val) { super ((int) x_val, (int) y_val); x = x_val; y = y_val; } …… } What is the issue?

public class Base { private int x, y = 0; public Base(int x_val, int y_val) { x = x_val; y = y_val; } public Base(int x_val) { x = x_val; } public Base() {} …… } public class Derived extends Base { private double x, y = 0; public Derived() { this (0.0, 1.0); } public Derived(double x_val, double y_val) { super(); super ((int) x_val, (int) y_val); x = x_val; y = y_val; } …… } How about this?

public class Base { private int x, y = 0; public Base(int x_val, int y_val) { x = x_val; y = y_val; } public Base(int x_val) { x = x_val; } public Base() {} …… } public class Derived extends Base { private double x, y = 0; public Derived() { this (0.0, 1.0); } public Derived(double x_val, double y_val) { super(); super ((int) x_val, (int) y_val); x = x_val; y = y_val; } …… } How about this? There can have only one super!