CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.

Slides:



Advertisements
Similar presentations
Polymorphism Method overriding Method overloading Dynamic binding 1.
Advertisements

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.
Object Oriented Programming
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Lecture 28: Abstract Classes & Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Read: –Chapter 9 Cahoon & Davidson.
More about classes and objects Classes in Visual Basic.NET.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Object Oriented Programming
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
ABSTRACT DATA TYPES, ABSTRACT CLASSES AND INTERFACES And abstract art….
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
1 / 41 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 5 Programming Fundamentals using Java 1.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
1 More About Derived Classes and Inheritance Chapter 9.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Sixth Lecture ArrayList Abstract Class and Interface
Inheritance ITI1121 Nour El Kadri.
Overriding Method.
Chapter 11 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Class vs Abstract vs Interface
Chapter 9 Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Polymorphism CT1513.
Week 6 Object-Oriented Programming (2): Polymorphism
Announcements & Review
Chapter 14 Abstract Classes and Interfaces
CSE 142 Lecture Notes Inheritance, Interfaces, and Polymorphism
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

CSC 205 Programming II Lecture 4 Abstract Class

The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized by concrete subtypes The abstraction might be too general to have practical usage May have constructors defined, although not meant to be used to create object May have one or more abstract methods

Abstract Method A class with one or more abstract methods must be abstract An abstract method doesn’t have a class body public abstract void draw(); Usually public. Define an common behavior yet each subtype might have a unique variation To be implemented by non-abstract subclasses

UML Representation Shape Rectangle +draw() Circle +draw() Triangle +draw()

Java Syntax public abstract class Shape { … public abstract void draw(Graphics g); //no class body … } public class Circle extends Shape { … public void draw(Graphics g) { g.setColor(getColor()); g.fillOval(getX(), getY(), diameter, diameter); } … }

Polymorphism Two definitions: Polymorphism is The facility by which a single operation may be defined upon more than one class and may take on different implementations in each of those classes Different shapes need to be draw in different ways The property where by a variable may point to objects of different classes at different times A variable in Shape type may refer to a Circle object at one time, and to a Rectangle object another time

Dynamic Binding The technique by which the exact piece of code to be executed is determined only at run-time AKA late or run-time binding run-time Shape s compile-time

Polymorphism – an example Shape Rectangle +toString() Square +toString() public String toString() { return "Edge: " + edge; } public String toString() { return "Position: (" + x + ", " + y + ")\t" + color; } public String toString() { return super.toString() + "\nWidth: " + width + "\tHeight: " + height; }

Late Binding – an example import java.awt.Color; public class ShapeTest { public static void main(String[] args) { Shape s1 = NervousShapes.createShape(); Shape s2 = new Rectangle(10, 10, Color.green, 15, 30); Shape s3 = new Square(50, 20, Color.red, 20); System.out.println(s1); System.out.println(s2); System.out.println(s3); } C:\courses\CSC205\labs\shape>java ShapeTest Position: (397, 106) java.awt.Color[r=13,g=91,b=241] Position: (10, 10) java.awt.Color[r=0,g=255,b=0] Width: 15 Height: 30 Edge: 20

Final Classes and Methods A final class cannot be extended Known final classes in Java APIs String Math Wrapper classes, such as Integer A class cannot be both abstract and final Methods declared as private or static are always final.