Properties Methods of a class can manipulate that class’s private data members Methods of a class can manipulate that class’s private data members Classes.

Slides:



Advertisements
Similar presentations
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Advertisements

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
OOP: Inheritance By: Lamiaa Said.
Inheritance Inheritance-2. Inheritance Rewriting point and circle classes class Point { protected: int x,y; public: Point(int,int); void display(void);
Esempi Ereditarietà1 // Definition of class Point #ifndef POINT_H #define POINT_H #include using std::ostream; class Point { friend ostream &operator
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Inheritance1 Inheritance Software re-use with derived classes.
 2002 Prentice Hall. All rights reserved. Some slides modified by LL 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base.
Object-Oriented Programming C programmers concentrate on writing functions.functions By contrast, MC++ programmers concentrate in creating their own user-defined.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Inheritance Is a form of software reusability in which programmers create classes that absorb an existing class’s data and behaviors and enhance them with.
Review of C++ Programming Part II Sheng-Fang Huang.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
CLASS INHERITANCE WEEK 7 Chapter 20 CS SENEM KUMOVA METIN1.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2 Base Classes and Derived.
 2003 Prentice Hall, Inc. All rights reserved 1 Object-Oriented Programming Inheritance Main Objectives: To understand how inheritance.
 Definition: The ability to derive child classes (sub- classes) from parent classes (super-classes)  Characteristics:  Methods and instance variables.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Chapter 8: User-Defined Classes and ADTs
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
Inheritance & Polymorphism1. 2 Introduction Besides composition, another form of reuse is inheritance. With inheritance, an object can inherit behavior.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
/** Feb 1996 Cay Horstmann */ import java.util.*; import corejava.*; public class EmployeeTest { public static void main(String[]
Jozef Goetz contribution,  2011 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
Jozef Goetz contribution,  2011, 2014 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.
Inheritance ndex.html ndex.htmland “Java.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
1 Inheritance inheritance –a mechanism that build a new class by deriving from an existing class –derived class (or subclass) inherit from based class.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Inheritance.
Classes and Objects Introduced
Inheritance ITI1121 Nour El Kadri.
Inheritance and Encapsulation
Chapter 9 – Object-Oriented Programming: Inheritance
User-Defined Classes and ADTs
Can perform actions and provide communication
Can perform actions and provide communication
Week 6 Object-Oriented Programming (2): Polymorphism
Chapter 9 - Object-Oriented Programming: Inheritance
Chapter 8: User-Defined Classes and ADTs
Can perform actions and provide communication
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

properties Methods of a class can manipulate that class’s private data members Methods of a class can manipulate that class’s private data members Classes provide public properties to allow clients to set ( assign value to) or get(obtain value of) private data members Classes provide public properties to allow clients to set ( assign value to) or get(obtain value of) private data members __property void set_Hour( int val) __property void set_Hour( int val) { hour = (val >=0 &&val =0 &&val<24)?val:0); } __property int get_Hour() __property int get_Hour() { return hour; return hour; }

properties Providing set and get capabilities appears to be the same as making the variables public Providing set and get capabilities appears to be the same as making the variables public Every property method name must be preceded by either set_ or get_ Every property method name must be preceded by either set_ or get_ __property void set_propertyName() __property void set_propertyName() PropertyName specifies the name of property. PropertyName specifies the name of property. Hour is scalar property Hour is scalar property Scalar property is property that can be accessed like variables. We assign value to scalar properties using = operator. When assignment occurs, the code in the set accessor for that property executes Scalar property is property that can be accessed like variables. We assign value to scalar properties using = operator. When assignment occurs, the code in the set accessor for that property executes Time *time = new Time(); Time *time = new Time(); time->Hour = Int32::Parse(Console::ReadLine()); time->Hour = Int32::Parse(Console::ReadLine());

Static members Static variable: all class objects share only one copy of a static variable. Static variable: all class objects share only one copy of a static variable. Static variable represents class-wide information Static variable represents class-wide information Declaration Declaration static in count; static in count; Static member can be initialized in its declaration Static member can be initialized in its declaration Public static data can be accessed from client code through the class name using the scope resolution operator:: Public static data can be accessed from client code through the class name using the scope resolution operator:: The private static data can be accessed only through methods or property of the class The private static data can be accessed only through methods or property of the class

Static methods To enable a client to access a private static member when no objects of the class exists, the class must provide a public static method or property To enable a client to access a private static member when no objects of the class exists, the class must provide a public static method or property A static method cannot access non- static data A static method cannot access non- static data Static variable and static methods exist even when there are no objects of that class in which the static members are defined Static variable and static methods exist even when there are no objects of that class in which the static members are defined

Inheritance The previously defined class is called the base class The previously defined class is called the base class New class is referred to as the derived class New class is referred to as the derived class (other language, Java refer to base as superclass and derived class as subclass) (other language, Java refer to base as superclass and derived class as subclass)

Point class public __gc class Point public __gc class Point { public: public: Point(); Point(); Point(int, int); Point(int, int); __property int get_X(){return x;} __property int get_X(){return x;} __property void set_X(int val){x = val;} __property void set_X(int val){x = val;} __property int get_Y(){ return y;} __property int get_Y(){ return y;} __property void set_Y(int val){ y = val;} __property void set_Y(int val){ y = val;} String *ToString(); String *ToString(); private: private: int x,y; int x,y; }; };

String * Point::ToString() String * Point::ToString() { return String::Concat(S”[“,x.ToString(), S”,”,y.ToString(), S”]”); return String::Concat(S”[“,x.ToString(), S”,”,y.ToString(), S”]”); } Point::Point(){ } Point::Point(){ } Point::Point( int xval, int yval) Point::Point( int xval, int yval) { x = xval; x = xval; y = yval; y = yval; }

Circle – inherit Point class public __gc class Circle :public Point public __gc class Circle :public Point { public: public: Circle(); Circle(); Circle( int,int,double); Circle( int,int,double); __property double get_Radius(){return radius;} __property double get_Radius(){return radius;} __property void set_Radius(double r) __property void set_Radius(double r) {if( r > 0) radius = r;} {if( r > 0) radius = r;} double Diameter(); double Diameter(); double Area(); double Area(); double Circumference(); double Circumference(); String *ToString(); String *ToString(); private: private: double radius; double radius; }; };

Circle Circle::Circle(){} Circle::Circle(){} Circle::Circle(int xval,int yval, double r) :Point(xval,yval) Circle::Circle(int xval,int yval, double r) :Point(xval,yval) { radius = r;} { radius = r;} double Circle::Diameter(){ return radius *2;} double Circle::Diameter(){ return radius *2;} } double Circle::Circumference() double Circle::Circumference() { return Math::PI *Diameter(); return Math::PI *Diameter(); } double Circle::Area() double Circle::Area() { return Math::PI * Math::Pow(radius, 2);} { return Math::PI * Math::Pow(radius, 2);} String *Circle::ToString() String *Circle::ToString() { return String::Concat(S”center= “, __super::ToString(), S”;Radius = “, Radius.ToString()); { return String::Concat(S”center= “, __super::ToString(), S”;Radius = “, Radius.ToString()); }

int _tmain() int _tmain() { Circle *cir = new Circle(4,5,2.5); Circle *cir = new Circle(4,5,2.5); String *output = String::Concat(S”X coordinate is “, cir->X.ToString(), S”\nY coordinate is “, cir- >Y.ToString(), S”\nRadius is “, cir- >Radius.ToString()); String *output = String::Concat(S”X coordinate is “, cir->X.ToString(), S”\nY coordinate is “, cir- >Y.ToString(), S”\nRadius is “, cir- >Radius.ToString()); Return 0; Return 0; }

Assignment Define a class called Cylinder that inherits from Circle. Define a class called Cylinder that inherits from Circle. Has data member height Has data member height Methods: Volume() that return volume of the cylinder Methods: Volume() that return volume of the cylinder Area() return the cylinder’s surface area Area() return the cylinder’s surface area Properties: set_Height and get_Height Properties: set_Height and get_Height

int _tmain() { Cylinder *cyn = new Cylinder(12,23,2.5,5.7); String *out = String::Concat (S"X coordinate is ", cyn->X.ToString(), S"\nY coordinate is ", cyn->Y.ToString(),S"\nRadius is ", cyn->Radius.ToString(),S"\nHeight is ", cyn->Height.ToString()); out = String::Concat (out, S"\nDiameter is ", cyn->Diameter().ToString(S"F"), S"\n"); out = String::Concat (out, S"Volume is", cyn->Volume().ToString(S"F")); MessageBox::Show (out, S"Demonstrating class Cylinder"); return 0; }