Inheritance cont. - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Inheritance cont.

Description:

A base class can be declared as a abstract class it has no real instances. ... Abstract methods must be implemented in derived class with keyword override. ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 7
Provided by: name9
Category:
Tags: cont | inheritance

less

Transcript and Presenter's Notes

Title: Inheritance cont.


1
Inheritance cont.
  • Abstract classes
  • Abstract methods
  • Example
  • Abstract class - Shape
  • Derived concrete classes - Circle, Rect
  • Prepare for the Exam.

2
Abstract classes
  • Abstract classes are classes without instances.
  • For abstract class Animal
  • pubic abstract class Animal
  • public virtual void Eat()
  • We can not create Animal objects
  • Animal error new Animal()
  • An abstract class is often derived by concrete
    classes.
  • public class Cat Animal
  • public class Dog Animal
  • .
  • Example file Example58

3
Abstract classes
  • A base class can be declared as a abstract class
    it has no real instances.
  • For instance, we can declare BankAccount as an
    abstract class.
  • So abstract classes are also used to specify
    common data and operations to derived concrete
    derived classes.

4
Abstract methods
  • Moreover, we can declare abstract methods in an
    abstract class.
  • public abstract void Eat()
  • Abstract methods has no body in the abstract
    class
  • Abstract methods must be implemented in derived
    class with keyword override.
  • Compare to virtual methods
  • An virtual method must have a body.
  • Derived may or may not override a virtual methods
  • Example Animal
  • Example58 - public abstract string Speak()

5
Programming exercise
  • Given class Point
  • public class Point
  • private double x
  • private double y
  • public Point(double x, double y)
  • this.x x
  • this.y y
  • public double X
  • get return x
  • public double Y
  • Define abstract class Shape
  • An instance variable
  • private Point location
  • An abstract method
  • public abstract double Area()
  • Define 2 derived concrete classes Rect and Circle
  • Rect
  • One more instance variable
  • private Point toPoint
  • Circle
  • One more instance variable
  • private double radius

6
Exam 3 (4/15, Fri)
  • Topics covered since exam 2
  • The String class and string operations (chapter
    6.16.2)
  • ToUpper, ToLower, IndexOf, Replace, SubString,
    CompareTo
  • The Random class and random number generation
    (chapter 6.5)
  • Next, NextDouble
  • Interfaces and implementation (chapter 6.4)
  • Implement interface IComparable , CompareTo
    method , sorting an array
  • Two dimensional array (chapter 7.4)
  • Inheritance base classes, derived classes,
    abstract classes. (chapter 10)
Write a Comment
User Comments (0)
About PowerShow.com