Java Classes and Inheritance

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
ITEC200 – Week03 Inheritance and Class Hierarchies.
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.
CSE S. Tanimoto Java Classes and Inheritance 1 Java Classes and Inheritance Object (again): A computational unit consisting of some data elements.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Inheritance using Java
Chapter 12: Adding Functionality to Your Classes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Cs3180 (Prasad)L8Packages1 Packages Organizing large programs => From monolithic programs to partitioning class name space Access control of names => Enforcing.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
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.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
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 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Topics Instance variables, set and get methods Encapsulation
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Chapter 15 Abstract Classes and Interfaces
OOP: Encapsulation &Abstraction
Inheritance and Polymorphism
Nested class.
Computer Science II Exam 1 Review.
Java Programming Language
Classes & Objects: Examples
Java – Inheritance.
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Java Classes and Inheritance
Java Inheritance.
Java Classes and Inheritance
Java Classes and Inheritance
Java Classes and Inheritance
Fundaments of Game Design
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Chapter 5 Classes.
Presentation transcript:

Java Classes and Inheritance Object: A computational unit consisting of some data elements to which are associated some specific methods for operating on them. Class: A category of objects having a single formal description in a software system. Instance of a class: An object created as an element of a class and therefore having all of the member variables and associated methods defined for instances of that class. CSE 373 -- S. Tanimoto Java Classes and Inheritance

CSE 373 -- S. Tanimoto Java Classes and Inheritance A Class Hierarchy ThreeDShape CurvedShape Polyhedron MixedShape Sphere RectangularParallelopiped Tetrahedron Cylinder Cube CSE 373 -- S. Tanimoto Java Classes and Inheritance

CSE 373 -- S. Tanimoto Java Classes and Inheritance Class Polyhedron public abstract class Polyhedron { protected int nFaces, nVertices, nEdges; protected double width, length, height; public void describe() { System.out.println("This polyhedron has " + nFaces + " faces, " + nVertices + " vertices, and " + nEdges + " edges. Its volume is " + volume()); } public abstract double volume(); CSE 373 -- S. Tanimoto Java Classes and Inheritance

Class RectangularParallelopiped public class RectangularParallelopiped extends Polyhedron { private double width, length, height; // A constructor - It sets protected parent variables: RectangularParallelopiped (double theWidth, double theLength, double theHeight) { width=theWidth; length=theLength; height=theHeight; nFaces = 6; nVertices = 8; nEdges = 12; } // A concrete method for the parent’s abstract method: public double volume() { return width * length * height; CSE 373 -- S. Tanimoto Java Classes and Inheritance

CSE 373 -- S. Tanimoto Java Classes and Inheritance Class Cube public class Cube extends RectangularParallelopiped{ // A constructor that calls its parent constructor: Cube(double side) { super(side, side, side); } // Cube inherits the volume() method of its parent. CSE 373 -- S. Tanimoto Java Classes and Inheritance

CSE 373 -- S. Tanimoto Java Classes and Inheritance Class Tetrahedron public class Tetrahedron extends Polyhedron{ // A constructor: Tetrahedron(double theWidth, double theLength, double theHeight) { width = theWidth; length = theLength; height = theHeight; nFaces = 4; nVertices = 4; nEdges = 6; } // A different concrete method for the same abstract method: public double volume() { return (width * length * height) / 6.0; // This method overrides the parent’s, but calls it, too. public void describe() { System.out.print(“(Tetrahedron) “); super.describe(); CSE 373 -- S. Tanimoto Java Classes and Inheritance

Class Poly (The Application) public class Poly { private Polyhedron p1, p2, p3; public static void main(String [] argv) { Poly thisApp = new Poly(); thisApp.p1 = new Cube(5); thisApp.p2 = new Tetrahedron(10, 8, 7); thisApp.p3 = new RectangularParallelopiped(2, 3, 4); thisApp.p1.describe(); thisApp.p2.describe(); thisApp.p3.describe(); } CSE 373 -- S. Tanimoto Java Classes and Inheritance

CSE 373 -- S. Tanimoto Java Classes and Inheritance Output This polyhedron has 6 faces, 8 vertices, and 12 edges. Its volume is 125.0 (Tetrahedron)This polyhedron has 4 faces, 4 vertices, and 6 edges. Its volume is 93.33333333333333 This polyhedron has 6 faces, 8 vertices, and 12 edges. Its volume is 24.0 CSE 373 -- S. Tanimoto Java Classes and Inheritance

Class (Static) Variables and Methods Class variables (not instance variables) and class methods are shared by all instances of the class. There is only one copy of a class variable. protected static int numInstances = 0; // a class variable MyObject() { numInstances++; } // a constructor public static void main(String [] argv) {} // a class method Class variables and methods can be used even if there are no instances of the class. Class variables can serve as global variables in a program. CSE 373 -- S. Tanimoto Java Classes and Inheritance

CSE 373 -- S. Tanimoto Java Classes and Inheritance Java Packages A package is a group of related classes. Some standard packages are java.awt and java.util To create a package, put a package declaration at the beginning of each file containing the class definitions that are to belong to the package. package geometry; public class Dodecahedron { // ... } public class Icosahedron { // ... Nested packages are permitted. When imported, their class files must be located in subdirectories of their outer package directories. CSE 373 -- S. Tanimoto Java Classes and Inheritance

Scopes of Member Names in Java public: Accessible inside & outside of its class and subclasses. private: Accessible only within its class definition. protected: Accessible within its class definition and those of its descendant classes. package: Accessible within the same package (possibly from otherwise unrelated classes). CSE 373 -- S. Tanimoto Java Classes and Inheritance

Scopes of Identifiers in Methods Scope of an identifier introduced within a method defaults to the block containing it. { for (int i=0; i<10; i++) { System.out.println(“Now i = “ + i); } doSomething(i); double i = 100.0; CSE 373 -- S. Tanimoto Java Classes and Inheritance

CSE 373 -- S. Tanimoto Java Classes and Inheritance Method Inheritance Suppose public class ChildClass extends ParentClass. By default, each method of ParentClass is inherited by ChildClass. An abstract method in ParentClass is one for which no implementation is provided, and that must be overridden by a method of the same name in ChildClass in order to be used. If ParentClass contains any abstract methods, it must be declared an abstract class, and it cannot be directly instantiated. If ChildClass overrides a method m(args) of ParentClass, the ParentClass version is still accessible within ChildClass using super.m(args) A method qualified as final cannot be overridden. CSE 373 -- S. Tanimoto Java Classes and Inheritance

CSE 373 -- S. Tanimoto Java Classes and Inheritance Classes and Types A type can be considered to be a restriction on the set of values that a variable is permitted to store. A class can be used as a type. String name = “Washington“; Cube myBlock = new Cube(10); In Java, there are four primitive types that are not classes: int, float, double, boolean. These permit “lightweight” values to be used, which can be stored and manipulated more efficiently than can bona fide objects. CSE 373 -- S. Tanimoto Java Classes and Inheritance

Upcasting and Downcasting Upcasting occurs when an object of one type is assigned to a variable declared with a supertype of the object. No explicit casting is needed: Polyhedron p = new Cube(5); Downcasting, however, requires an explicit cast: Cube c = (Cube) p; CSE 373 -- S. Tanimoto Java Classes and Inheritance

CSE 373 -- S. Tanimoto Java Classes and Inheritance Summary A class is a formal category of program objects within a software system. Instances have all the member variables and methods of their class, including those inherited from superclasses. Subclasses can contain member variables and methods in addition to those inherited. Inherited methods can be overridden with versions specific to a subclass. Java provides mechanisms for hiding or not hiding names across the class hierarchy. Classes are related to types. CSE 373 -- S. Tanimoto Java Classes and Inheritance