Inheritance
This presentation is the property of its rightful owner.
Sponsored Links
1 / 70

inheritance PowerPoint PPT Presentation


  • 97 Views
  • Uploaded on
  • Presentation posted in: General

03/03/57. Outline. ?????????????????????? protected???????????????????????????????????? Object. 03/03/57. ?????????. . 03/03/57. ????????? (Inheritance). ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Download Presentation

inheritance

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Inheritance

(inheritance)

Nerissa Onkhum


Outline

Outline

  • protected

  • Object


Inheritance


Inheritance1

(Inheritance)

  • 2


Inheritance

    • () ()

    • ( )

    • () ()

    • () () ()


Inheritance


Inheritance


Inheritance

  • class {

    //

    ;

    ;

    //

    () { }

    () { }

    }


Inheritance

    • classextends

  • class extends {

    }

    class extends {

    }

    class extends {

    }


Inheritance

class extends

class extends


Inheritance

      • class extends {

        //

        }


Inheritance

= new ();

.();

.(); ()

()


Inheritance

= new ();

.();

()


Inheritance

class extends {

//

() { }

}

= new ();

.();

= new ();

.();


Inheritance


Inheritance

class extends {

//

;

}


Inheritance

    • private

    • private

      • private

      • protected


Inheritance

  • protected

  • protected

    • private

    • public

class {

protected ;

}

class {

(){

+=10;

}

}


Inheritance

class extends {

//

;

//

() {

//

}

}


Inheritance

  • ();

  • ();

  • (override)

  • = new ();

    .();


Inheritance

  • = new ();

  • = new ();


Inheritance

  • = new ();

    .():

  • = new ()

    .();

    .();


Polymorphism

(Polymorphism)


Inheritance

.()


Inheritance

  • poly

  • morphism morph


Inheritance

  • (reuse)


Inheritance


Inheritance


Inheritance

    • getArea()

    • public enum Color { Red, Green, Blue }

    • getColor()


Inheritance

public enum Color{

Red, Green, Blue

}

public class Shape {

private Color color;

public Shape() {

color = Color.Red;

}

public double getArea() {

return 0;

}

public void setColor(Color c) {

color = c;

}

public Color getColor() {

return color;

}

}


Inheritance

public class TestShape {

public static void main(String[] args) {

Shape s1 = new Shape();

System.out.println(s1.getColor());

System.out.println(s1.getArea());

Shape s2 = new Shape();

s2.setColor(Color.Blue);

System.out.println(s2.getColor());

System.out.println(s2.getArea());

}

}


Inheritance


Inheritance

class Rectangle extends Shape{

private double width;

private double height;

public Rectangle(int w, int h){

width = w;

height = h;

}

public double getWidth(){

return width;

}

public double getHight(){

//

}

}


Inheritance

class Rectangle extends Shape{

public double getArea() {

return width * height;

}

}


Inheritance

class Rectangle extends Shape{

public String toString(){

String str = Rectangle;

str += color = +getColor();

str += width = +width;

str += height = +height;

str += area = +getArea();

return str;

}

}


Inheritance

class TestShape1

{

public static void main(String[] args)

{

Rectangle r = new Rectangle(3,4);

System.out.println(r);

System.out.println(r.toString());

}

}


Object

Object


Tostring

toString()

public class Rectangle extends Shape {

...

public String toString() {

String str = "Rectangle";

str += " color=" + getColor();

str += " width=" + width;

str += " height=" + height;

str += " area=" + getArea();

return str;

}

}


Tostring1

toString()

  • Object


Object1

Object

    • toString() //

    • equals() //

    • clone() //

    • hashCode() //


Equals

equals()

public boolean equals(Object otherObject) {

if (otherObject instanceof Rectangle) {

Rectangle otherRect = (Rectangle) otherObject;

boolean equalWidth = width == otherRect.width;

boolean equalHeight = height == otherRect.height;

return equalWidth && equalHeight;

}

return false;

}


Hashcode

hashCode()

  • public int hashCode() {

    return (int) (width + height * 17);

    }


Clone

clone()

public Object clone() {

Rectangle clone = new Rectangle(width, height);

return clone;

}


Object2

Object

public class TestRectangle3 {

public static void main(String[] args) {

Rectangle r1 = new Rectangle(2, 5);

System.out.println(r1);

System.out.println(r1.hashCode());

Rectangle r2 = new Rectangle(5, 2);

System.out.println(r1.equals(r2));

Rectangle r3 = (Rectangle) r1.clone();

System.out.println(r1.equals(r3));

}

}


Inheritance


Inheritance

Public class Rectangle extends Shape{

public Rectangle(double w, double h){

.

}

}

public class Square extends Rectangle {

public Square( double w) {

super(w,w);

}

...

}

Rectangle


Super

Super();

  • super()

  • super(w,w)

    Rectangle(double w, double h);

  • super () super class () subclass


Inheritance

public class Square extends Rectangle {

public Square(double w){

super(w,w)

}

public String toString() {

String str = "Rectangle";

str += " color=" + getColor();

str += " width=" + getWidth();

str += " area=" + getArea();

return str;

}

}

Rectangle

Shape

Rectangle

Rectangle


Inheritance


Inheritance

  • public final class Square

Public class SquareChild extends Square


Final class

final class

  • java.lang.*

    • Final class

      • String

      • StringBuffer

      • Math

      • System

      • Integer

      • Double


Inheritance

  • publicclassShape {

    publicfinalColorgetColor() {

    returncolor;

    }

    ...

    }

    public class Rectangle extends Shape{

    public Color getColor(){

    return Color.Blue;

    }

    }


Protected

protected


Protected1

protected

  • protected

  • protected

  • private public


Inheritance

public class Circle extends Shape {

protected double radius;

public Circle(double r) {

radius = r;

}

public double getRadius() {

return radius;

}

public double getArea(){

return Math.PI* radius * radius;

}

}


Inheritance

public class Ring1 extends Circle {

public double getArea() {

double outerArea = Math.PI * radius * radius;

double innerArea = Math.PI * innerRadius * innerRadius;

return outerArea - innerArea;

}

...

}


Inheritance

public class Ring2 extends Circle {

public double getArea() {

double outerArea = super.getArea();

double innerArea = Math.PI * innerRadius * innerRadius;

return outerArea - innerArea;

}

...

}


Abstract class

(abstract class)


Abstract class1

(AbstractClass)

  • getArea() Shape Shape

    public abstractdouble getArea();

  • (abstract) abstract Shape

    public abstract class Shape {

    public abstract double getArea();

    ...

    }


Abstract class2

abstract class

  • Shape s = new Shape();

  • Shape s;

    s = new Rectangle(3,2);

    s = new Square(5);


Inheritance

public class TestPolymorphism {

public static void main(String[] args) {

Shape shape;

shape = new Rectangle(10, 20);

System.out.println(shape.getArea());

shape = new Circle(10);

System.out.println(shape.getArea());

}

}


Inheritance

public class ShapeArray {

public static double sumArea(Shape[] shapes) {

double sum = 0;

for( Shape s : shapes) {

sum += s.getArea();

}

return sum;

}

}


Inheritance

Shape[] s = new Shape[3];

s[0] = new Triangle(10);

s[1] = new Rectangle(10,20);

s[2] = new Pentagon(20);

double d = ShapeArray.sumArea(s);


Inheritance


Inheritance


Inheritance

    • (overhead)

    • (Yo-yoproblem)

    • private protected


Inheritance


Inheritance

  • public protected


Inheritance


  • Login