Inheritance - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Inheritance

Description:

1- The base-class constructor is called. ... put a 'lock' on the method to prevent any inheriting class. from changing its meaning. ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 25
Provided by: ENCS
Category:

less

Transcript and Presenter's Notes

Title: Inheritance


1
Inheritance
  • Tutorial Comp. 249
  • Various Ressources but main is
  • http//www.codeguru.com/Java/index.php

2
Review Constructors
class Meal Meal() System.out.println("Meal(
)") class Bread Bread()
System.out.println("Bread()") class
Cheese Cheese() System.out.println("Cheese()"
) class Lettuce Lettuce()
System.out.println("Lettuce()")
3
Review Constructors
class Lunch extends Meal Lunch()
System.out.println("Lunch()") class
PortableLunch extends Lunch PortableLunch()
System.out.println("PortableLunch()")
class Sandwich extends PortableLunch Bread b
new Bread() Cheese c new Cheese() Lettuce
l new Lettuce() Sandwich()
System.out.println("Sandwich()") public
static void main(String args) new Sandwich()

4
Review Constructors
  • Output
  • Meal()
  • Lunch()
  • PortableLunch()
  • Bread()
  • Cheese()
  • Lettuce()
  • Sandwich()

5
Review Constructors
  • 1- The base-class constructor is called. This
    step is repeated recursively such that the root
    of the hierarchy is constructed first, followed
    by the next-derived class, etc., until the
    most-derived class is reached.
  • 2-Member initializers are called in the order of
    declaration.
  • 3- The body of the derived-class constructor is
    called.

6
Review Variable Scope
7
Review Final Keyword
  • The final keyword has slightly different
    meanings depending on the context, but in general
    it says This cannot be changed
  • Final Data
  • It can be a compile-time constant that wont
    ever change
  • It can be a value initialized at run-time that
    you dont want changed
  • Example
  • final int i1 9
  • final int i4 (int)(Math.random()20)
  • final int a 1, 2, 3, 4, 5, 6 //final
    array
  • final int j // allowed??? Blank final data?

8
Review Final Keyword
  • Final Argument
  • void with(final Gizmo g)
  • g new Gizmo() //legal???? Assign null? Will
    compiler catch?
  • Final Method
  • put a lock on the method to prevent any
    inheriting class
  • from changing its meaning.
  • Note that Any private methods in a class are
    implicitly final

9
Review Final Keyword
  • Final Class
  • No inheritance from this class
  • All methods are implicitly final
  • Example
  • final class Dinosaur
  • //..

10
Review Static Variables
public class VariableExample private static int
classCounter 0 private int instanceCounter
0 public void increment(int num) int
localCounter 0 classCounter
instanceCounter localCounter num
System.out.println( "classCounter\t\t"
classCounter) System.out.println("instanceCounte
r\t" instanceCounter) System.out.println("loca
lCounter\t\t" localCounter) public static
void main(String args) VariableExample var1
new VariableExample() VariableExample var2
new VariableExample() var1.increment(2)
var2.increment(3)
11
Review Static Variables
Output classCounter 1 instanceCounter 1 local
Counter 2 classCounter 2 instanceCounter 1 l
ocalCounter 3
12
Review Static Methods
public class MrHappyObject     private String
_mood _HAPPY    private final static String
_HAPPY   "happy"    private final static
String _ANNOYED "annoyed"    private final
static String _ANGRY   "angry"    public
void printMood()         System.out.println( "I
am " _mood )       
13
Review Static Methods
  public void receivePinch()         if(
_mood.equals( _HAPPY ) )             _mood
_ANNOYED         else             _mood
_ANGRY            public void receiveHug()
        if( _mood.equals( _ANGRY ) )
            _mood _ANNOYED else
            _mood _HAPPY    
14
Review Static Methods
  MrHappyObject obj1 new MrHappyObject()MrHapp
yObject obj2 new MrHappyObject()obj1.printMoo
d()obj2.printMood()   /----------------------
--------------------------------------------------
--------/ obj1.receiveHug()obj2.receivePinch()
obj1.printMood()obj2.printMood()   
15
Review Static Methods
  • Add the following to the original class
    HappyObject
  •  
  • private static int _instantiationspublic
    MrHappyObject() _instantiationspublic
    static int instances() return
    _instantiations
  • Static keyword not obligatory but convenient in
    this case
  •   

16
Abstract Classes
  • Abstract Classes have one or more abstract and
    CANNOT be instantiated
  • May have NO abstract methods
  • Think of it as a type to instantiate
  •   

17
Abstract Classe Shape
  • package shapes
  • public abstract class Shape extends Object
  • private int x
  • private int y
  • public Shape()
  • this(0, 0)
  • public Shape(int x, int y) super()
    setX(x) setY(y)
  • public int getX()
  • return x
  •   

18
  • public void setX(int x)
  • this.x x
  • public int getY()
  • return y
  • public void setY(int y)
  • this.y y
  • // Abstract method. Note semicolon, and no
    or method body
  • public abstract double area()
  • public boolean equals(Object obj2)
  • // ...
  • return super.equals(obj2) //WHAT IS IT
    ABOUT THIS EQUAL?
  • public String toString()
  • return "Class " this.getClass().getName()
    " x " x
  • " y " y

19
Concret Class Rectangle
package shapes public class Rectangle extends
Shape private double length private
double width public Rectangle(double length,
double width) this(0, 0, length, width)
public Rectangle(int x, int y, double
length, double width) super(x, y)
setLength(length) setWidth(width)   
20
Concret Class Rectangle
public double getLength() return length
public void setLength(double length)
if (length gt 0) this.length length
else throw new IllegalArgumentException(
"Length must be zero or more") public
double getWidth() return width
21
public void setWidth(double width) if
(width gt 0) this.width width
else throw new IllegalArgumentException("Width
must be zero or more") // Implement
the area method public double area()
return length width public double
perimeter() return 2 (length width)
public boolean equals(Object obj2) //
... return super.equals(obj2) public
String toString() return super.toString()
" length " length " width " width
  
22
Example of Output
Shape s new Shape() // ??? Square square1
new Square(5, 5) // ???? Rectangle rect1 new
Rectangle(4, 6) //???System.out.println("Area
of rectangle " rect1.area())   
23
Example of Output
  • Square square1 new Square(10) //???
    System.out.println("Area of square "
    square1.area())Shape shape1 new Rectangle(3,
    7) //???? System.out.println("Area of rectangle
    " shape1.area())
  • ?Static vs. Dynamic type

24
Overloading
  • True or false?public int secretCode(int
    x)public int secretCode(int x, int y)public int
    secretCode(float x)
  • public float secretCode(float x)
  • Same or Overloaded?
  • static void print(String s, int i)
  • System.out.println( "String " s ", int "
    i)
  • static void print(int i, String s)
  • System.out.println( "int " i ", String "
    s)

Write a Comment
User Comments (0)
About PowerShow.com