CSE 341 -- S. Tanimoto Java Classes and Inheritance 1 Java Classes and Inheritance Object (again): A computational unit consisting of some data elements.

Slides:



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

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
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.
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.
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 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.
By Waqas The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class.
Programming in Java CSCI-2220 Object Oriented Programming.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
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.
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.
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.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
OOP: Encapsulation &Abstraction
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Java Programming Language
Classes & Objects: Examples
Java – Inheritance.
Java Classes and Inheritance
Java Inheritance.
Java Classes and Inheritance
Java Classes and Inheritance
Java Classes and Inheritance
Java Classes and Inheritance
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Chapter 5 Classes.
Presentation transcript:

CSE S. Tanimoto Java Classes and Inheritance 1 Java Classes and Inheritance Object (again): 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 S. Tanimoto Java Classes and Inheritance 2 A Class Hierarchy ThreeDShape CurvedShape Tetrahedron Polyhedron RectangularParallelopiped MixedShape SphereCylinder Cube

CSE S. Tanimoto Java Classes and Inheritance 3 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 S. Tanimoto Java Classes and Inheritance 4 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 S. Tanimoto Java Classes and Inheritance 5 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 S. Tanimoto Java Classes and Inheritance 6 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 S. Tanimoto Java Classes and Inheritance 7 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 S. Tanimoto Java Classes and Inheritance 8 Output This polyhedron has 6 faces, 8 vertices, and 12 edges. Its volume is (Tetrahedron)This polyhedron has 4 faces, 4 vertices, and 6 edges. Its volume is This polyhedron has 6 faces, 8 vertices, and 12 edges. Its volume is 24.0

CSE S. Tanimoto Java Classes and Inheritance 9 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 S. Tanimoto Java Classes and Inheritance 10 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 S. Tanimoto Java Classes and Inheritance 11 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; also accessible from code within the same package. package: Accessible within the same package (possibly from otherwise unrelated classes).

CSE S. Tanimoto Java Classes and Inheritance 12 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 S. Tanimoto Java Classes and Inheritance 13 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 S. Tanimoto Java Classes and Inheritance 14 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 S. Tanimoto Java Classes and Inheritance 15 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 S. Tanimoto Java Classes and Inheritance 16 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.