More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Introduction to Java 2 Programming
Introduction to Java 2 Programming Lecture 5 Quick Recap; Error-handling.
Object Oriented Programming with Java
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
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.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
Inheritance in the Java programming language J. W. Rider.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Inheritance and Access Control CS 162 (Summer 2009)
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
BY:- TOPS Technologies
Java Programming (By Rama Bhadra Rao M) You can Trace me in Day 2
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Inheritance-Basics.
Inheritance and Polymorphism
One class is an extension of another.
Types of Programming Languages
Interfaces.
Object Oriented Analysis and Design
OOP’S Concepts in C#.Net
One class is an extension of another.
Java Programming Language
Packages and Interfaces
Interfaces.
Serialization and Deserialization Bullet points from Head First Java, Ch Dec-18 serialization.ppt.
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Inheritance and Polymorphism
Final and Abstract Classes
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng

Overview Abstraction Polymorphism Interface Package Shadows Object Serialization

Quiz What is inheritance in Java? How do you organise code between classes and their sub classes?

Abstraction Some times it is advantageous to partly implement a superclass Functionality that changes among sub classes Abstraction is when a class is partly defined Interface and abstraction Interface – 100% abstraction Abstraction - <= 100% abstraction You can’t instantiate an abstract class

Abstraction abstract class MyClass { public abstract void myMethod(int in); public abstract int myOtherMethod(); public void float yetAnotherMethod(){return 1.0;} }

Abstraction class MySubClass extends MyClass { public void myMethod(int in) { … } public int myOtherMethod() { … }

Quiz What is an abstract class? How does an abstract class differ from an interface? What is the difference between overloaded and overridden methods?

Polymorphism Take on many differ forms Using parent class to refer to a child … is a … All cats are mammals therefore … All mammals are cats?

Polymorphism class Animal class Dinosaur extends Animal class Stegosaurus extends Dinosaur Animal Dinosaur Stegosaurus

Polymorphism Dinosaur d = new Stegosaurus(); Stegosaurus s = new Dinosaur();

Polymorphism Object type dictates the methods available Not it’s reference Dinosaur d = new Stegosaurus(); d can access all Animal methods and Stegosaurus methods But what about overridden methods?

Polymorphism public class Dinosaur { public void walk(){System.out.println(“Dinosaur walking”);} } public class Stegosaurus extends Dinosaur { public void walk(){System.out.println(“Stegosaurus walking”);} } Dinosaur d = new Stegosaurus(); d.walk(); Stegosaurus walking Dynamic binding

Polymorphism public class Dinosaur { public void walk(){System.out.println(“Dinosaur walking”);} } public class Stegosaurus extends Dinosaur { public void walk(){System.out.println(“Stegosaurus walking”);} public void eatGrass(){ … }; } Dinosaur d = new Stegosaurus(); d.eatGrass();

Quiz How is polymorphism implemented in Java? Why can a superclass reference not call a method defined in a subclass?

Interface Polymorphism works for interfaces as for abstract Classes can implement multiple interfaces class MyClass implements Interface1, Interface2, Interface3 Interfaces can extend other interfaces interface Interface2 extends Interface1 Interfaces cannot implement other Interfaces As they don’t implement any code!

Package Collection of classes The classes in a packet can have a related function and depend on each other (same project or sub project) No need for inheritance within the packet Java has many packets Util Awt Swing Io Lang

Package Create your own package Naming convention Hierarchical Domain name of organisation that created it then organisation name Subpackets separated by dots se.umu.ce.mypacket.mysubpacket Package keyword Start of file package se.umu.ce.mypacket.mysubpacket;

Package To use a package import packagename.classname; import packagename.*; import se.umu.ce.mypacket.mysubpacket.*; Packages map to directory structure Classpath Eleclips – new package

Shadows Variables or classes in differ blocks but with the same name Public class MyClass { intx; public void MyMethod() { x = 1; int x = 5; System.out.println(x); } }

Shadows package MyPackage; public class MyClass { … } package MyOtherPackage; Import MyPackage; public class MyClass { public void MyMethod() { MyClass m = new MyClass(); MyPackage.MyClass pm = new MyPackage.MyClass(); } }

Object Serialization Preserve the state of an object between runs Save an object to a file and the recreate it in the same state Transmit the object form one machine to another

Object Serialization Implement the Serializable interface (in java.io) All class members must be serializable else marked transient transient private String strString; Transient are derived at run time Use the ObjectOutputStream to write the object to a file writeObject Use the ObjectInputStream to deserialize the object readObject

Object Serialization class MyClass implements Serializable {} MyClass m = new MyClass(); try { FileOutputStream f = new FileOutputStream(“file.ser”); ObjectOutputStream out = new OutputObjectStrea(f); out.writeObject(m); Out.close(); f.close(); }

Quiz What is a package in Java? How can you save the state of a object in Java?

Questions?