Class and Object: Define class: public class Circle { public double x,y; public double r; public double area() { return 1.14159 * r * r); } public Circle() {} -- Default constructor } 1. Construct a new object: Circle c; c = new Circle(); 2. Access object: data access-- c.r=25.0; c.x=0.0; c.y=0.0; method access-- double a=c.area(); Constructor: public Circle (double x, double y, double r) { this.x=x; this.y=y; this.r=r; } this means "this object" Two ways to create object now: Circle c = new Circle(); c.x=2.0; c.y=2.0; c.r=25.0; // Created first then initialized later Circle d = new Circle(2.0, 2.0, 25.0); // Created and Initialized object Copy constructor: public Circle (Circle c) { this.x=c.x; this.y=c.y; this.r=c.r; } // Created a copy object not just reference to the same one. Method overloading (Same name but different siganiture): double func(duoble x, double y); double func(double x); public (Any visiable), private(Class only) and protected(Class and subclass visiable). private Circle(); //Make default constructor not visiable to anyone ex: redefine private doulbe x; private int i; public getX() {return x;} // Use accessor to get it public setX(double x) {this.x=x;} //User accessor to set it public area() {return 3.14159*r*r + i); // No way to access i double a = c.getX(); Class variable: public static double z=1.0; public void func(double w) { Circle.z=w; // Can't use this.z=w because this means "this object" instance(object) variable: Circle c=new Circle(); c.x=2.0; Circle c1=new Circle(); c1.x=3.0; Object created and destroyed: C: malloc, free c++: new, delete java: Circle c = new Circle(); Garbage collection to free memory for you. Subclass and Inheritance. Base class (Superclass) --> subclass (Derived from base) public class GraphicCircle entends Circle { Color outline, fill; // Color is a class public void draw(DrawWindow dw) { // DrawWindow is another class dw.drawCircle(x,y,r,outline,fill); // Instance method } public void draw() {} // Overloading public double r; // Shadowing data member } public GraphicCircle(double x, double y,double r,double i) { // A // good way to create subclass constructor super(x,y,r); // Is super.Circle(x,y,r) legal? // If no use super, compiler created super() for you. this.x=x; // Bad proctice, error prone. this.i=i; } } Overloading: Same method different siganiture in a class. Overriding: Same method same siganiture but subclass overwrite it (by different methodology) (Virtual function). Every java method is virtual function/ Virtual function table. super.draw() only up one class. If it needs to use parent parent...then it is better to create utility class and let everyone in the family to use it. Circle c=new Circle; GraphicCircle gc = new GraphicCircle; c.draw(); gc.draw(); double a = c.r; double ga = gc.r; Constructor chaining (Happening at runtime so a long chain will end up "Bad Performance" Visibility: public protected package private same class y y y y class in the same package y y y n subclass in different package y y n n non-subclass different package y n n n (Everybody else) applied for class, data member and method. c++ multiple inheritance: subclass derived from base1&2 or more. Java only has single inheritance. but if B1-->D1 B2-->D2 but to prevent code duplicated between D1 and D2, try to create interface that is for D1 and D2. public class D1 extends B1, implements I (Method only) public class D2 extends B2, implements I public class D1 extends B1 { B3 b3; // B3 can be shared as utility class, for code reuse purpose. b3=new B3(); } java graphics: includes: Components, containers and layoutmanagers. Components: Button, text area, checkbox, radio buttons, Containers: window. But container can be as components too. Like window in window. Layout managers: Using it to put components into containers. java event model: includes: Listener Actioner 2 ways to implement: Listerner initiated or actioner initiated. For java using actioner initiated. Which listener will be noticed? The actioner has a list that is created by listener's request.