1 Polymorphisme. 2 // Polymorphisme: illustre le polymorphisme et Vector import java.util.*; public class TestPolymorphisme { public static void main.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Lecture 4 More on Java® Data Types, Control Structures.
Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Methods and Formatted Output Chapter 4. Overview Breaking down a big program into small methods. Formatting output – using printf – using DecimalFormat.
ArrayList Difference of Array and ArrayList [Sample code] TestArrayList.java.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Copyright © Recursive GCD Demo public class.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Programming Methodology (1). Implementing Methods main.
Phil Campbell London South Bank University Java 1 First Steps.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Multi-Dispatch in the Java Virtual Machine TM What is (dynamic) multi-dispatch? Method selection based on the types of … all (or more than one) of the.
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
SCJP 6.0 Lecturer Kuo-Yi Chen 151, 153, 154, 155, 156, 157
1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public.
Java Programming Abstract classes and Interfaces.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
Objectives Understand grammar of OOP Understand equivalent Java code Discuss different implementations of classes and objects.
Chapter 1 Inheritance University Of Ha’il.
Exercise 1 Suppose C is a class that implements interfaces I and J. Which of the following Requires a type cast? C c = ……? I i = …..? J j = …..? c =
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
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.
Using Classes to Store Data Computer Science 2 Gerb.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for.
Övning 4. Repetition göra egna klasser class Rektangel { private double längd; private double bredd; public Rektangel(double l, double b) { this.längd.
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Inheritance #1 First questions Similar to Python? What about visibility and encapsulation? – can an object of the child class access private members.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Les Animaux By Rachel Egginton Le Chat J’ai un chat. I have one cat. J’ai des chats. I have some cats. J’ai un chat marron. I have one brown cat.
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!
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Inheritance, Polymorphism and Abstract Classes. Student Management System All students are CUNY Students CUNY Students are Queens College students, or.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Polymorphism.
Lecture 12 Inheritance.
Interface.
Abstract Class, Interface, Package
Computing Adjusted Quiz Total Score
March 29th Odds & Ends CS 239.
null, true, and false are also reserved.
Unit 3 - The while Loop - Extending the Vic class - Examples
Java Lesson 36 Mr. Kalmes.
Code Animation Examples
Recursive GCD Demo public class Euclid {
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
class PrintOnetoTen { public static void main(String args[]) {
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Module 2 - Part 1 Variables, Assignment, and Data Types
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
An Example of Inheritance
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

1 Polymorphisme

2 // Polymorphisme: illustre le polymorphisme et Vector import java.util.*; public class TestPolymorphisme { public static void main (String[] args) { Vector animaux = new Vector(); Animal unAnimal; Chien unChien = new Chien("Snoopy"); animaux.add(unChien); Chat unChat = new Chat("Felix"); animaux.add(unChat); animaux.add(new Chat("Garfield")); animaux.add(new Chien("Pluto")); for (int i= 0; i < animaux.size(); i++){ unAnimal = (Animal)animaux.get(i); unAnimal.afficher(); unAnimal.parle(); }

3 // Animal avec nom obtenir nom, afficher abstract class Animal{ private String nom; Animal(String chaine){ nom= chaine; } String getNom(){return nom;} abstract void parle(); void afficher(){ System.out.println(nom); }

4 class Chien extends Animal{ Chien(String chaine){ super(chaine); System.out.println("\nCreer le chien " + chaine); } void afficher(){ System.out.print("\nJe suis le chien "); super.afficher(); } void parle(){ System.out.println(" Ouarf! Ouarf!"); }

5 class Chat extends Animal{ Chat(String chaine){ super(chaine);// constructeur de Animal System.out.println("\nCreer le chat " + chaine); } void afficher(){ System.out.print("\nJe suis le chat "); super.afficher( ); } void parle(){ System.out.println(" Miaou! Miaou!"); }

6 Exécution du programme: Creer le chien Snoopy Creer le chat Felix Creer le chat Garfield Creer le chien Pluto Je suis le chien Snoopy Ouarf! Ouarf! Je suis le chat Felix Miaou! Miaou! Je suis le chat Garfield Miaou! Miaou! Je suis le chien Pluto Ouarf! Ouarf!