Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance.

Slides:



Advertisements
Similar presentations
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Advertisements

Java Review Interface, Casting, Generics, Iterator.
Comp 212: Intermediate Programming Lecture 18 – Java Generics By: Anupam Chanda.
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.
Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation.
Inheritance Inheritance Reserved word protected Reserved word super
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
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.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Programming Languages and Paradigms Object-Oriented Programming.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Internet Software Development Classes and Inheritance Paul J Krause.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Inheritance The Basics in Java. Definition  A class that is derived from another class is called a subclass (also a derived class, extended class, or.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
JAVA COURSE LESSON2 BY OMPUTER ENGINEEING ASSOCIATION.
Programming in Java CSCI-2220 Object Oriented Programming.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
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.
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,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
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.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
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.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Software Technology - I Classes Objects Methods (Cont.) A Baby is a Monkey.
OOP Basics Classes & Methods (c) IDMS/SQL News
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Chapter 9 Inheritance and Polymorphism
More inheritance, Abstract Classes and Interfaces
Java Programming Language
CNT 4007C Project 2 Good morning, everyone. In this class, we will have a brief look at the project 2. Project 2 is basically the same with project 1.
Lecture 10 Concepts of Programming Languages
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Applications Development Objects in Java Key concepts and metaphors Implicit subclassing Static methods Constructors Instance variables Dangers of uninitialised object references

Applications Development Metaphors - inheritance A small UK company (Watsonian Squire Ltd) imports “Royal Enfield” motorcycles from India where they are still in productionWatsonian Squire Ltd The basic design has not changed substantially since 1949! The UK company does two things: checks the imported bikes and sells them on directly or modifies the bikes into “Clubman” and “Trailie” styles by changing some components

Applications Development Metaphors - inheritance The company obtains an instance of a “basic motorcycle” superclass object and can use it directly… or it can subclass the object and “override” a feature by providing a new or modified part or function This would not be possible without the cooperation of the Indian factory. The style of the motorcycle is not final.

Applications Development What about a simple object? To subclass we do this: public class Spoon extends Cutlery {.. } But what does this do? public class TaxCalcLib { … } Any class definition with no explicit extends clause is inheriting from java.lang.Object This provides the bare minimum framework for a class to function

Structure of a simple class import java.net.*; public class HostName {public static void main(String[] args) {InetAddress iaLocal; try {iaLocal = InetAddress.getLocalHost(); System.out.println(“Name:" + iaLocal.getHostName()); } catch (UnknownHostException e) {System.err.println("Don't know about local host"); System.exit(0); } }// end of class HostName

Applications Development Static methods Note that the example program is part of an object called “HostName” Thus the filename must be HostName.java But how does the object get instantiated? Surprise - it doesn’t! The example uses a static method (code that’s part of the class, not the object)

Object instantiation public class Egg {private String myColour; public Egg(String sEggColour)// NB no return type! { myColour = sEggColour; } public Egg() // NB no return type! { myColour = “Orange”; } public static void main(String[] args) {Egg e1 = new Egg(“Green”);// create some objects… Egg e2 = new Egg(); // what colour is this? Egg e[] = new Egg[6];// no objects created here! … } }// end of class Egg

Applications Development Constructors A “constructor” is an initialisation method It runs when a new object is created A class may define many object constructors Each constructor differs only in its input parameters All methods can have multiple “flavours” However, unlike other methods, constructors have no return type

public class Egg {private String myColour;// each instance gets its own copy public Egg(String sEggColour) {myColour = sEggColour; } public Egg() { myColour = "Orange"; } public void reportColour() {System.out.println(myColour); } public static void main(String[] args)// static – all eggs share this {Egg e1 = new Egg("Green");// green egg Egg e2 = new Egg();// orange egg Egg e[] = new Egg[6];// six references to eggs // … e1.reportColour();// should be green e2.reportColour();// should be orange e[0].reportColour();// runtime error – no eggs here yet } }// end of class Egg

Applications Development Comment on the examples The idea of using a static method of a class to instantiate objects of the same type is common practice (e.g. in small programs, for testing) It seems counter-intuitive to some people A clear separation between the concepts of static, class-based code and dynamic, object-based code …helps us gain a better perspective

Applications Development Conclusion We have seen: some of the concepts and language associated with object-orientation some practical issues of program structure, including the use of static methods constructors and their use