definition: the ability to derive child classes (sub- classes) from parent classes (super-classes) ...
TRANSCRIPT
Definition: The ability to derive child classes (sub-classes) from parent classes (super-classes)
Characteristics: Methods and instance variables in the parent
class are directly accessible from child class objects
Child classes can override parent class methods with their own customizations
Demonstrates the concept of reusable code. Prior to object oriented programming, programmers would often cut and paste sections of code. Bugs would then show up in many places in a system
The String class Overrides the Object class equals method Adds a variety of its own methods
Every class inherits from the Java Object class
public class Circle{ protected double radius;
public Circle() { radius = 0; }public Circle(double radius) {this.radius = radius;} public double area() {return radius * radius * Math.PI;}public String toString() {return "radius=" +radius);
}public class Cylinder extends Circle{ private double length; public Cylinder(double h, double r) { super(r); length = h; } public double volume() {return area() * length;} public String toString() {return super.toString()+"
length="+length;} public double area() { return 2*(super.area()
+Math.PI*radius*length); }}
public class Parent{ private int size; public Parent(int size) { this.size = size; }
public String toString() { return "size = " + size; }
public int getSize() { return size; }}public class Child extends Parent{ private int size; public Child(int size) { super(size); this.size = 0; } public Child(int sizeA, int sizeB) { this(sizeA); size = sizeB; } public @Override int getSize() { return super.getSize() +
size; }
public String toString() { return "size = " + getSize(); }
}
Note: The getSize() method of the parent class is overridden in the child class
extends: Create a new class that is an extension of a parent class
super: Access parent class method or instance variable this: access current class method or instance variable instanceof: Determine if a variable is a particular child type final: Class, method, variable cannot be extended or overridden static: A class method or variable (one copy for all objects) Scope Related variables/methods:
private : accessible only within class protected: accessible within class in and to any of its child
classes public: accessible globally Default (package friendly): accessible to all classes in the
package
public class Demo{ private static int x = 10; private int y = 20; static String out1(){ return
“o1”; } String out2() { return “o2”; }
public static void main(String[] args)
{ Demo d = new Demo(); System.out.println(d.y+" " + Demo.x +" " + x);
System.out.println(out1() + d.out2() ); } }
Question: What’s wrong with System.out.println(y)?
Static methods and variables•Go with the class, not with individual objects. •They can be accessed with dot notation using the class name. •There is one copy, even if many objects are instantiated.
Instance variables •Go with each instantiated object.
public class Top{}public class First extends Top {}public class Second extends Top {}public class Test{ private Top top; public Test() { if (Math.random()>0.5) top = new First();
else top = new Second(); } public static void main(String[] args) { Test test = new Test(); if (test.top instanceof First) System.out.println("First"); else System.out.println("Second"); } }
Note: There is an initialization block in the Test class
Note: After determining the class, we can cast objects appropriately to access overridden methods
public class Test{ public static void main(String[] args) { Object o = new GraduateStudent(); System.out.println(o); System.out.println( (GraduateStudent)o ); System.out.println( (Person)o ); System.out.println( new Person()); System.out.println(new Object()); }}class Person { public String toString() { return "Person"; }}class Student extends Person{ public String toString() { return "Student"; } }class GraduateStudent extends Student {}
Output:StudentStudentStudentPersonjava.lang.Object@9304b1
Note: Parent objects always reference overridden child methods. Access to overridden child methods is called polymorphism
Create instance variables only when necessary. Variables local to a single method should generally NOT be instance variables
Limit the scope of variables. In general, instance variables should NOT be public. Use protected variables only for those that are needed by child classes.
Static variables and methods should rarely be used. Polymorphism (next topic) is better than relying on
instanceof Java reflection (not covered in this class) enables
programs to determine methods that exist in generic objects inheriting from Object
Arrays Definition: A table of variables accessed by index Disadvantage: Arrays have a fixed lengh
ArrayList: A class for expandable arrays Advantage: Removes array fixed length
limitation Disadvantage: Slower to access Methods: add, clear, get, remove, size, set,
isEmpty, removeRange, toArray, trimToSize Instantiate: ArrayList<String> data = new
ArrayList<String>();
Why do we need this? We want to rotate our avatars Java methods use radians
A radian is the radius distance around a circle
The radius goes around the circle 2π times (Circumference = 2πr)
360 degrees = 2π radians Radians = π/180 Degrees Degrees = 180/ π Radians
Purpose: Enables complicated shapes that we can transform
Question: What is a transform? It is an operation that alters the way the area displays Examples: scale, rotate, move (translate)
Constructors: new Area() or new Area(Shape s) Note: Java Polygon, Rectangle, etc. are Shape objects
Method to extend an area: area.add(Area anotherArea)
Perform a transform: area.transform(AffineTransform tx);
Draw an area:Graphics2D g2d= (Graphics2D)graphicsObject;g2d.draw(area); or g2d.fill(area);
Definition: An affine transform is a manipulation of a figure so parallel lines remain parallel
Make a transform object: AffineTransform transform = new AffineTransform();
Edit the transform object; enable translation of coordinates, scaling, and rotationtransform.translate(300, 300);transform.rotate(degrees * Math.PI/180.);transform.scale(0.5, 0.5);
Notes Once the AffineTransform object is configured with
multiple transforms, it is ready to be used by Area objects Important: The last translation is done first The AffineTransform class has static transform methods if
only one type of transform is needed
int[] x = {-100,0,100}, y = {-100,0,-100}, r = {-100,0,100}, s = {100,0,100};
Area area=new Area(new Polygon(x,y,3)), area2=new Area(new Polygon(r,s,3));
area.add(area2);
AffineTransform tx = new AffineTransform();tx.translate(100,100);tx.scale(0.5, 0.5);tx.rotate(30 * Math.PI / 180.);
area.transform(tx);area2.transform(tx);
Graphics2D g2D = (Graphics2D)g;g2D.setColor(Color.RED);g2D.fill(area);
area.intersect(area2);g2D.setColor(Color.GREEN);g2D.fill(area2);
1. Design the figure2. Determine the corresponding Java classes that inherit from
Shape (ex: Rectangle, Polygon, Arc2D.Float, Ellipse2D.Float, etc.)
3. Create the shape objects and call the parent Sprite add method
4. Sprite parent class inherits from the Area class. Its add method:
a. instantiates an Area object using the Shape object parameterb. calls the super add method with the object just instantiatedc. adds the instantiated Area object and color object parameter
to the two respective array lists.
5. The Area class has a getBounds, intersects, and transform methods that we will be using in this and future labs