Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.

Slides:



Advertisements
Similar presentations
Chapter 1 Inheritance University Of Ha’il.
Advertisements

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.
Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
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.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CPSC150 Abstract Classes and Interfaces Chapter 10.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Abstract classes and Interfaces. Abstract 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.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
Inheritance Notes Chapter 6 1. Inheritance  you know a lot about an object by knowing its class  for example what is a Komondor? 2
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Inheritance ndex.html ndex.htmland “Java.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Important Annoucement 1  I messed up something in the last class  if a subclass overrides a method that throws an exception then it must either 1. throw.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Inheritance (Part 4) Abstract Classes.
Sixth Lecture ArrayList Abstract Class and Interface
Lecture 12 Inheritance.
Inheritance and Polymorphism
Polymorphism 11-Nov-18.
Polymorphism 15-Nov-18.
Interface.
Java Programming Language
Java Inheritance.
An Example of Inheritance
Java Programming: From the Ground Up
Presentation transcript:

Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes are inherited by subclasses, and subclasses can access them directly by name  private static attributes are not inherited and cannot be accessed directly by name  but they can be accessed/modified using public and protected methods  the important thing to remember about static attributes and inheritance  there is only one copy of the static attribute shared among the declaring class and all subclasses 1

// the wrong way to count the number of Dogs created public abstract class Dog { // other attributes... static protected int numCreated = 0; Dog() { //... Dog.numCreated++; } public static int getNumberCreated() { return Dog.numCreated; } // other contructors, methods... } 2

// the wrong way to count the number of Dogs created public class Mix extends Dog { // attributes... Mix() { //... Mix.numCreated++; } // other contructors, methods... } 3

// too many dogs! public class TooManyDogs { public static void main(String[] args) { Mix mutt = new Mix(); System.out.println( Mix.getNumberCreated() ); } prints 2 4

What Went Wrong?  there is only one copy of the static attribute shared among the declaring class and all subclasses  Dog declared the static attribute  Dog increments the counter everytime its constructor is called  Mix inherits and shares the single copy of the attribute  Mix constructor correctly calls the superclass constructor  which causes numCreated to be incremented by Dog  Mix constructor then incorrectly increments the counter 5

Counting Dogs and Mixes  suppose you want to count the number of Dog instances and the number of Mix instances  Mix must also declare a static attribute to hold the count  somewhat confusingly, Mix can give the counter the same name as the counter declared by Dog 6

public class Mix extends Dog { // other attributes... private static int numCreated = 0; // bad style public Mix() { super(); // will increment Dog.numCreated // other Mix stuff... numCreated++; // will increment Mix.numCreated } //... 7

Hiding Attributes  note that the Mix attribute numCreated has the same name as an attribute declared in a superclass  whenever numCreated is used in Mix, it is the Mix version of the attribute that is used  if a subclass declares an attribute with the same name as a superclass attribute, we say that the subclass attribute hides the superclass attribute  considered bad style because it can make code hard to read and understand  should change numCreated to numMixCreated in Mix 8

Static Methods and Inheritance  there is a big difference between calling a static method and calling a non-static method when dealing with inheritance  there is no dynamic dispatch on static methods 9

public abstract class Dog { // Dog stuff... public static int getNumCreated() { return Dog.numCreated; } public class Mix { // Mix stuff... public static int getNumCreated() { return Mix.numMixCreated; } 10 notice

public class WrongCount { public static void main(String[] args) { Dog mutt = new Mix(); Dog shaggy = new Komondor(); System.out.println( mutt.getNumCreated() ); System.out.println( shaggy.getNumCreated() ); System.out.println( Mix.getNumCreated() ); System.out.println( Komondor.getNumCreated() ); } } prints

What's Going On?  there is no dynamic dispatch on static methods  because the declared type of mutt is Dog, it is the Dog version of getNumCreated that is called  because the declared type of shaggy is Dog, it is the Dog version of getNumCreated that is called 12

Hiding Methods  notice that Mix.getNumCreated and Komondor.getNumCreated work as expected  if a subclass declares a static method with the same name as a superclass static method, we say that the subclass static method hides the superclass static method  you cannot override a static method, you can only hide it  hiding static methods is considered bad form because it makes code hard to read and understand 13

 the client code in WrongCount illustrates two cases of bad style, one by the client and one by the implementer of the Dog hierarchy 1. the client should not have used an instance to call a static method 2. the implementer should not have hidden the static method in Dog 14

Interfaces  recall that you typically use an abstract class when you have a superclass that has attributes and methods that are common to all subclasses  the abstract class provides a partial implementation that the subclasses must complete  subclasses can only inherit from a single superclass  if you want classes to support a common API then you probably want to define an interface 15

 in Java an interface is a reference type (similar to a class)  an interface can contain only  constants  method signatures  nested types (ignore for now)  there are no method bodies  interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces 16

Interfaces Already Seen public interface Iterable { Iterator iterator(); } public interface Collection extends Iterable { boolean add(E e); void clear(); boolean contains(Object o); // many more method signatures... } 17 access—either public or package-private (blank) interface name parent interfaces

Cell Interface  recall that the PolygonalModel class defined a shape using a collection of Triangle instances  there are many different types of geometric primitives that we might want to represent the shape with  point  line  polyline  triangle  polygon ... 18

 each primitive can be defined by a list of points and a list of edges connecting the points 19 pointlinepolylinetriangle P0P0,P1 P0,P1,P2,P3P0,P1,P2 P0 P1 P0 P1 P2 P3 P0 P1 P2 0,1 0,2 2,1 1,3 0,1 1,2 2,0

public interface Cell { int numberOfPoints(); int numberOfEdges(); Vector3d[] getPoints(); int[] getEdges(); //... } public class Point implements Cell { //... } public class Line implements Cell { //... } public class PolyLine implements Cell { //... } public class Triangle implements Cell { //... } 20

public class PolygonalModel implements Iterable { private List cells; //... } // client somewhere; reads a model from a file PolygonalModel model = new PolygonalModel("model.stl"); for(Cell c : model) { draw(c); } 21

Implementing Multiple Interfaces  unlike inheritance where a subclass can extend only one superclass, a class can implement as many interfaces as it needs to public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, Serializable 22 superclass interfaces