Everything is an object. Almost, but all objects are of type Object!

Similar documents
Overriding methods. Changing what we have inherited from e.g. Object

Rules and syntax for inheritance. The boring stuff

Inheritance. Transitivity

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

C12a: The Object Superclass and Selected Methods

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Polymorphism. return a.doublevalue() + b.doublevalue();

Programming overview

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Java Object Oriented Design. CSC207 Fall 2014

COMP 250 Fall inheritance Nov. 17, 2017

Lecture 18 CSE11 Fall 2013 Inheritance

Java Magistère BFA

CS 251 Intermediate Programming Inheritance

JAVA MOCK TEST JAVA MOCK TEST II

COMP 110/L Lecture 19. Kyle Dewey

INHERITANCE. Spring 2019

PROGRAMMING LANGUAGE 2

CMSC 132: Object-Oriented Programming II

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Inheritance (Part 5) Odds and ends

The class Object. Lecture CS1122 Summer 2008

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Inheritance, polymorphism, interfaces

First IS-A Relationship: Inheritance

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

INHERITANCE AND EXTENDING CLASSES

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Object Oriented Programming. Java-Lecture 11 Polymorphism

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Inheritance and Polymorphism

Inheritance and Polymorphism. CS180 Fall 2007

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Polymorphism and Inheritance

CS111: PROGRAMMING LANGUAGE II

Practice for Chapter 11

More on Objects in JAVA TM

Super-Classes and sub-classes

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Inheritance, part 2: Subclassing. COMP 401, Spring 2015 Lecture 8 2/3/2015

Java Programming Lecture 7

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

CS-202 Introduction to Object Oriented Programming

Introduction to Computer Science II CS S-10 Inheritance

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

COMP 250. Lecture 30. inheritance. overriding vs overloading. Nov. 17, 2017

More On inheritance. What you can do in subclass regarding methods:

CS313D: ADVANCED PROGRAMMING LANGUAGE

Logistics. Final Exam on Friday at 3pm in CHEM 102

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Arrays Classes & Methods, Inheritance

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

More about inheritance

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

Inheritance (Part 2) Notes Chapter 6

AP CS Unit 6: Inheritance Notes

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

CSCE3193: Programming Paradigms

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Object Oriented Programming: Based on slides from Skrien Chapter 2

Big software. code reuse: The practice of writing program code once and using it in many contexts.

CS 61B Data Structures and Programming Methodology. July 3, 2008 David Sun

C++ Inheritance and Encapsulation

Chapter 10 Classes Continued. Fundamentals of Java

CSC9T4: Object Modelling, principles of OO design and implementation

Inheritance. The Java Platform Class Hierarchy

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

More Relationships Between Classes

Chapter 6: Inheritance

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

CS260 Intro to Java & Android 03.Java Language Basics

Introduction to Inheritance

Inheritance -- Introduction

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Computer Science 210: Data Structures

Java Fundamentals (II)

G Programming Languages - Fall 2012

What is Inheritance?

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

CSE 143 Lecture 12 Inheritance

Overview. Elements of Programming Languages. Objects. Self-Reference

Interfaces, Mixins, & Multiple Inheritance

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

CLASS DESIGN. Objectives MODULE 4

Lecture 4: Extending Classes. Concept

Asking For Help. BIT 115: Introduction To Programming 1

Overriding Variables: Shadowing

Java: introduction to object-oriented features

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Transcription:

Everything is an object Almost, but all objects are of type Object!

In Java, every class is actually a subclass of Object...or has a superclass which has Object as superclass... There is a class called java.lang.object in the Java API. The designers of Java have decided that this class is the superclass of all classes. This means that even if we write our own class, it will too be a subclass of Object (you may think of subclass as subtype). Object is the most abstract abstraction we can have in Java - it is so abstract that it merely describes what is common between all instances of any class. This level of abstraction focuses on purely programmatical features, what we can do with instances/objects in general.

Any instance can be viewed as being of type Object Since Object is the super-super-duper class of all classes, we can actually choose to view any instance as being of this type. This means that this would be perfectly legal (if we have a class called Passport, and a class called Member): Object obj = new Passport(); Object anotherobj = new Member(); But now we can only use the references as if they truly were only of Object type. We can only call methods declared in java.lang.object!

Advantages of the Object abstraction It is convenient to know that any object of any type can represent itself as a String. The method tostring() declared in Object guarantees this! And since every class ultimately is a subclass of Object, every instance of any class will have a tostring() method. However, the implementation in the class Object is not so fancy, since it is declared in a class which is very general. In fact, the only thing we get back when calling the version of tostring() which we get from Object, is the qualified class name and an @ and some hexadecimal number (all as a String of course).

Another advantage is equality check Another good thing with having every instance inheriting behavior from Object, is that every object has a method for checking if it is equal to any other object (passed as parameter to the equals method). A companion to equals() is hashcode(), which produces a unique int number for an object, and the deal here is that to objects which are considered equal, also returns the same hashcode() number. Using hashcode() is often more efficient than using the equals() method. As with tostring() the implementations of equals() and hashcode() we get from Object, are not so sophisticated. We ll soon look at how to deal with this.

How is it syntactically expressed to inherit Object? To inherit a class in Java can be expressed in two ways. The way this chapter deals with, is called extending a class using the keyword extends. So, for instance java.lang.string extends java.lang.object. In fact, all classes which do not explicitly extend some other class extends Object. When we write a class, we could actually say in the class declaration: public class MyClass extends Object But because of the rule that all classes implicitly extend Object, we don t have to.

If we d want to, we could extend some other class Using the keyword extends, we could explicitly extend some other class than Object, which then would create a new subtype (subclass) of that explicitly named class. If we go back to our example of writing a file manager, and the abstractions of File and the subtype MediaFile (with in turn its subtypes AudioFile and VideoFile), the MediaFile class could be declared as: public class MediaFile extends File (Note that there are alternative ways to achieve this flexibility and hierarchy!)

What methods are not declared in Object? Since Object is an abstraction of a runtime object of any type in the programming language Java, it has a limited set of methods declared. There is, for instance, no thumbnail() method in Object, since having a method for rendering a thumbnail image, is not typical for every Java object of any class... Also, there is no changeemail() method in Object, since the Java API designers didn t anticipate that every class would declare an instance variable for an email address and thus a need for changing it ;-) Therefore the methods in Object are more general and less specific.

So methods are inherited? Yes, the methods in a superclass are inherited in subclasses. That s just a technical way of saying that a class which extends (inherits from) a superclass, gets all the (public and protected) methods from the superclass, without the need of repeating the same code declaring the methods. And the methods inherited, are inherited as-is, so if we want to change them, we actually have to write that code ourselves (as we will soon see). Constructors are not inherited. Private methods and variables are inherited but not accessible from the subclasses, since private stuff is only usable from code in the very same class.