Computer Science A 14: 31/3. Inheritence You can extend a class or change behaviour. Inheritance is a method to reuse code. Concept. Extending classes.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
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.
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.
Chapter 11 – Interfaces and Polymorphism. Chapter Goals Learn about interfaces Learn about interfaces Convert between class and interface references Convert.
Computer Science A 9: 3/11. Inheritance Today: Inheritance (JC – CCJ ) I have to leave at 11am (but you can stay)
What is an Interface?? An interface is a ‘container’ which contains ONLY abstract methods. public interface Capitalizable { public abstract String outCaps()
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Computer Science A 15: 17/4. Today Overview of java How to decribe programming languages Main message: The core of java is small Even a small language.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Computer Science A 11: 20/2. Java and swing Graphics programming: Windows with menus Buttons, textfields, scroll panels etc Animations, images, Events.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Further OO Concepts (Part I) Further OO Concepts I - Polymorphism Overview l Polymorphism is Wonderful. l Usefulness of Up casting? l Method call binding?
Class Hierarchy Discussion D. Constructor public class X { private int capacity; public X() { capacity = 16;} public X(int i) {capacity = i;} public int.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
1 Inheritance in Java CS 3331 Fall Outline  Overloading  Inheritance and object initialization  Subtyping  Overriding  Hiding.
1 Pertemuan 6 Object Oriented Programming Matakuliah: T0053/Web Programming Tahun: 2006 Versi: 2.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Module 7: Essentials of Object-Oriented Programming.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Intro to Java 2 By Geb Thomas Based on the Java TutorialJava Tutorial.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Programming With Java ICS Chapter 8 Polymorphism.
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.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
Chapter 9 Interfaces and Polymorphism. Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand.
Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes.
Chapter 10 Inheritance and Polymorphism
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 7.
More OOP. Extending other’s classes extend Java platform classes, e.g. class Applet public class MyApplet extends Applet { public void init() { } public.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Inheritance ndex.html ndex.htmland “Java.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Polymorphism Lecture - 9.
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.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Import javax.swing.*; class Check { public static void main(String[] args) { JFrame frame = new JFrame("Check"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//DO_NOTHING_ON_CLOSE.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
DEVRY COMP 220 iLab 7 Polymorphism Lab Report and Source Code Check this A+ tutorial guideline at
Web Design & Development Lecture 9
Polymorphism.
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Interface.
Lecture 17: Polymorphism (Part II)
C# - Inheritance and Polymorphism
Chapter 11 – Interfaces and Polymorphism
Polymorphism and access control
Example with Static Variable
Method Overriding in Java
Polymorphism CT1513.
Polymorphism Polymorphism - Greek for “many forms”
class PrintOnetoTen { public static void main(String args[]) {
Lecture 18: Polymorphism (Part II)
Inheritance in Java CS 3331 Fall 2009.
Computer Science II for Majors
Inheritance and Polymorphism
Presentation transcript:

Computer Science A 14: 31/3

Inheritence You can extend a class or change behaviour. Inheritance is a method to reuse code. Concept. Extending classes with fields and methods Late binding Casting objects

Example class Point{ int x; int y; Point(int x,int y){ this.x=x; this.y=y; } public String toString(){ return "("+x+","+y+")"; } toString is called when an object is used as a string

Example class Point3D extends Point{ int z; Point3D(int x,int y,int z){ super(x,y); this.z=z; } public String toString(){ return "("+x+","+y+","+z+")";} }

Example Point a=new Point(2,4); System.out.println(a.toString()); Point3D b=new Point3D(7,9,13); System.out.println(b.toString()); A Point3D ”is-a” Point object and may be used as a Point object

Object-oriented design When analysing a problem area you can often identify a hierarchy in the structure and use inheritance in the description. Model the world and find “is-a” relations: A circle is a shape A textfield is a component

Polymorphism Point x = new Point3D(1,2,3); System.out.println(x.toString()); Depends on the actual object. If x refers to a Point3D object it print 3 values If x refers to a Point object it prints 2 values Polymorphism (many shapes): Behavior can vary depending on the actual type of an object Called late binding: resolved at runtime Different from overloading; overloading is resolved by the compiler (early binding)

Polymorphism JSlider extends JComponent JSpinner extends JComponent Swing will call a method to drw the oject but the methods are different

interface MyShape interface MyShape{ void drawOn(JCanvas canvas); }

class Tree class Tree implements MyShape{ Tree(int x,int y){this.x=x; this.y=y;} int x,y; public void drawOn(JCanvas canvas){ canvas.drawArc(x,y,200,200,290,320); canvas.drawLine(x+80,y+300,x+80,y+180); canvas.drawLine(x+120,y+300,x+120,y+180); }

class Car class Car implements MyShape{ Car(int x,int y){this.x=x; this.y=y;} int x,y; public void drawOn(JCanvas canvas){ canvas.drawRect(x,y+50,300,50); canvas.drawLine(x+50,y+50,x+100,y); canvas.drawLine(x+100,y,x+200,y); canvas.drawLine(x+200,y,x+250,y+50); canvas.drawOval(x+50,y+100,50,50); canvas.drawOval(x+200,y+100,50,50); }

class MyShapeTest port javax.swing.*; public class MyShapeTest{ public static void main(String args[]){ JFrame frame=new JFrame("MyShapes"); frame.setSize(600,600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JCanvas canvas=new JCanvas(); frame.add(canvas); MyShape s1=new Car(10,300); MyShape s2=new Car(270,400); MyShape s3=new Tree(330,40); s1.drawOn(canvas); s2.drawOn(canvas); s3.drawOn(canvas); frame.setVisible(true); }

Example

And more… An interface is a placeholder for a number of classes. A class can be an extension of only one class but it may implement several interfaces