This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Advertisements

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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CPSC150 Abstract Classes and Interfaces Chapter 10.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
Multiple Choice Solutions True/False a c b e d   T F.
Abstract classes and Interfaces. Abstract classes.
Polymorphism & Interfaces
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Inheritance in the Java programming language J. W. Rider.
ECE122 Feb. 22, Any question on Vehicle sample code?
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Classes - Intermediate
Constructors A constructor is a method that has the same name as the class itself It is automatically called when an object is instantiated (in other words,
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Inheritance Notes: Using Object Oriented Design
Haskell Chapter 2.
Interfaces.
Interface.
Agenda Warmup AP Exam Review: Litvin A2
Methods Attributes Method Modifiers ‘static’
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Interfaces.
Interface.
Interfaces.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Advanced Java Programming
METHOD OVERRIDING in JAVA
Unit 3 Test: Friday.
Java Inheritance.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Overview of C++ Polymorphism
Chapter 14 Abstract Classes and Interfaces
2009 Test Key.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

this In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or constructor is being called. Example: public class Chess { // more code up here public void capture (Piece x) { if (x.equals(this)) return false; }

Another this example public class Sample { public int x = 0; public int y = 0; public Sample (int x, int y) { this.x = x; this.y = y; } // now, there are local copies of the parameters x and y in this class.

The List Interface In addition to the Comparable interface, another interface that you must be aware of is the List interface. ArrayList is a class that implements List. We will soon learn about polymorphism, which is used when a parent class or interface has a method that gets overridden by its child/subclass. For now, all you need to know about List is that it’s an interface that gets implemented by ArrayList.

Abstract Class We know what an interface is – a class that contains abstract (empty) methods, and can also contain constants. can An abstract class is similar, but one major difference is that it can contain methods that actually contain code. – In other words, an abstract class can implement methods, while an interface cannot. Like an interface, an abstract class CANNOT be instantiated. (In other words, you cannot, in a client, create an object of an abstract class.)

public abstract class Sample2 { public int sum (int num1, int num2) { return (num1 + num2) } public char something(int z); } /* note that this abstract class contains a method with actual code in its body, which is not allowed in an interface. */

Interface vs. Abstract Class You cannot instantiate (create an object of) an abstract class, but you can extend an abstract class, and then use its methods. So, what’s the point of an abstract class? Why not just use an interface, if you want to create a “blueprint” that programmers must follow? The answer is: If a bunch of programmers will be using the same method, in the same way, then it makes more sense to put this method in an abstract class and have them extend this class. But, if those programmers will be using the same method signature, but writing code for the method body in different ways, then it makes more sense to put that method in an interface and have them implement that interface.

We learned previously that all methods and variables in an interface must be public. (it wouldn’t make sense to have a private method in an interface, because where would this empty method be called from?) An abstract class, however, can have private methods and variables.

Static Methods A static method is a method that can be called by using the name of the class itself. You do not need to create an object in order to call the method. We have been using static methods for a long time: SavitchIn.readLine(); Math.pow(), etc. This explains why you don’t have to “connect” (create an object of) the SavitchIn class in order to use it. Open: StaticMethodClass, StaticMethodClient

Assignment Continue working on Independent Projects.