Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
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 Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
Abstract Classes and Interfaces
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Lecture 7 Polymorphism. Review: Constructors ●Parenthesis after constructor, not in class declaration ●The constructor makes the SamBot – do not “ new.
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.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
Internet Software Development Classes and Inheritance Paul J Krause.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Programming in Java CSCI-2220 Object Oriented Programming.
Types in programming languages1 What are types, and why do we need them?
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
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 and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
Banking Service class BankingService { LinkedList accounts; LinkedList customers; double getBalance(int forAcctNum) { for (Account acct:accounts) { if.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Object-Oriented Concepts
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Review What is an object? What is a class?
Java Primer 1: Types, Classes and Operators
Agenda Warmup AP Exam Review: Litvin A2
Chapter 3: Using Methods, Classes, and Objects
CS240: Advanced Programming Concepts
Java Programming Language
Advanced Java Programming
Chapter 14 Abstract Classes and Interfaces
Java Programming Language
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Review: libraries and packages
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Subtype Substitution Principle
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers

Types 0 Primitive types 0 boolean, int, double, etc. 0 Classes are also types for variables 0 Dillo (Yes, more dillos) 0 Person 0 Spreadsheet 0 Etc. 0 Interfaces are also to be types for variables 2

Who can extend classes? 0 Classes 0 Abstract classes 3

Who can implement interfaces? 0 Classes 0 Abstract classes 4

Objects 0 What does an object get from its class type? 0 Both fields and methods 0 What does an object get from its superclass type? 0 Both fields and methods 5

Interfaces: A Promise 0 A promise to implement certain methods 0 Example: 0 class A implements ImyInterface 0 A needs to have all methods that are inside of ImyInterface 0 class B extends A 0 B also needs to have all methods that are inside of ImyInterface 6

Interfaces: How You Assign Their Types to Objects 0 For example, say you have these two classes: 0 class A implements ImyInterface 0 class B implements ImyInterface 0 This is okay: 0 ImyInterface myObjectA = new A(); 0 ImyInterface myObjectB = new B(); 0 This is not okay: 0 ImyInterface myObject = new ImyInterface(); 7

Interfaces: What They Offer 0 Can refer to multiple classes as a common type 0 Example (same as before): 0 class A implements ImyInterface 0 class B implements ImyInterface 0 I can then do: LinkedList myList = new LinkedList (); And this list can contain objects of class A, of class B, or both: myList.add(new A()); myList.add(new B()); 8

When interfaces get tricky: What’s wrong with this? interface ImyInterface { int returnTheNumberSeven(); } class C implements ImyInterface {... boolean returnFalse() {... }.... } If I try to do: ImyInterface myObject = new C(); Why will Java be upset if I write: myObject.returnFalse()? 9

What’s wrong with this? (2) interface ImyInterface { int returnTheNumberSeven(); } class C implements ImyInterface {... boolean returnFalse() {... }.... } ImyInterface myObject = new C(); myObject.returnFalse(); Java sees myObject as being of type ImyInterface. Java only knows that objects of type ImyInterface have the method returnTheNumberSeven and they don’t have a method called returnFalse. Casting could let you correct this problem: ((C)myObject).returnFalse(); Here I’ve told Java, “I promise that myObject is of type “ C ” and has this method.” 10

Casting 0 Again, imagine you have these two lines of code: 1. ImyInterface myObject = new C(); 2. ((C)myObject).returnFalse(); 0 Casting makes Java do this type check at runtime rather than when compiling 0 Java sees line #1 above and says, “Okay, so myObject is of type ImyInterface.” 0 In line #2 you’re saying, “No Java, I’ve got this, I promise this object is of type C specifically.” 11

Casting 0 One last note regarding two lines of code: 1. ImyInterface myObject = new C(); 2. ((C)myObject).returnFalse(); 0 Note the two sets of parentheses: ((C)myObject).returnFalse(); 0 One goes around the name of the class you’re casting this object to 0 Another goes around the cast AND the name of the object 0 You can then do the.returnFalse() call 0 To reiterate, this will make Java angry: 0 (C)myObject.returnFalse(); 0 And this will make Java happy: 0 ((C)myObject).returnFalse(); 12

Superclasses and Subclasses: Example class Dog { Dog (){}; /* Just leaving constructor empty for now. Wouldn’t be empty if I wanted to pass in arguments when making a new Dog.*/ // Methods would be here, if I added them } class GoldenRetriever extends Dog { GoldenRetriever(){}; /* Just leaving constructor empty for now. be empty if I wanted to pass in arguments when making a new GoldenRetriever. */ // Methods would be here, if I added them } 13

Superclasses and Subclasses: What You Can Do With Casting class Dog { Dog (){}; /* Just leaving constructor empty for now. Wouldn’t be empty if I wanted to pass in arguments when making a new Dog.*/ // Methods would be here, if I added them } class GoldenRetriever extends Dog { GoldenRetriever(){}; /* Just leaving constructor empty for now. be empty if I wanted to pass in arguments when making a new GoldenRetriever. */ // Methods would be here, if I added them } If you did this: Dog myDog = new GoldenRetriever(); It’s okay to make this cast: (GoldenRetriever) myDog Why? Because myDog was originally created as a GoldenRetriever 14

Superclasses and Subclasses: What You Can Do With Casting class Dog { Dog (){}; /* Just leaving constructor empty for now. Wouldn’t be empty if I wanted to pass in arguments when making a new Dog.*/ // Methods would be here, if I added them } class GoldenRetriever extends Dog { GoldenRetriever(){}; /* Just leaving constructor empty for now. be empty if I wanted to pass in arguments when making a new GoldenRetriever. */ // Methods would be here, if I added them } If you did this: GoldenRetriever myDog = new GoldenRetriever(); It’s okay to do this: (Dog) myDog Why? Because myDog ’s original type, GoldenRetriever, extends Dog. 15

Superclasses and Subclasses: What You Cannot Do With Casting class Dog { Dog (){}; /* Just leaving constructor empty for now. Wouldn’t be empty if I wanted to pass in arguments when making a new Dog.*/ // Methods would be here, if I added them } class GoldenRetriever extends Dog { GoldenRetriever(){}; /* Just leaving constructor empty for now. be empty if I wanted to pass in arguments when making a new GoldenRetriever. */ // Methods would be here, if I added them } This is not okay: Dog myDog = new Dog(); and then doing (GoldenRetriever) myDog Why? Because myDog was originally created as a Dog Error will say: java.lang.reflect.InvocationTargetException Caused by: java.lang.ClassCastException: Dog cannot be cast to GoldenRetriever Takeaway: You can only refer to classes as their types or their “supertypes.” 16

What To Remember About Access Modifiers Remember: Set all fields to private (as a default) unless there is a good reason to set it to protected or public. One more time: Set all fields to private (as a default) unless there is a good reason to set it to protected or public. 17