Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
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 Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism = slide covered in class – others for reference.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
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.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance and Access Control CS 162 (Summer 2009)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
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.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance and Polymorphism
Object Oriented Programming
Object Oriented Analysis and Design
Chapter 9 Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Polymorphism and access control
Java Inheritance.
Inheritance.
Advanced Inheritance Concepts
Chapter 8 Class Inheritance and Interfaces
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism

Class vs Instance Objects Class members –class fields –class methods Instance members –instance fields –instance methods

Class Members Class members are associated with the class in which they are defined –exist only once, a single copy –declared using the static keyword –class members are accessed using the class name ClassName.classMemberName

Class fields and Methods Class fields –a class field has the same value for all instances of the object –a class field declared with a final modifier is a constant static final double PI= ; Class methods –a class method cannot use any instance members (fields or methods)

Instance members Instance members are associated with instances of the class –exist for each separate instance –copied as the object is instantiated –instance fields in difference instance objects can have different values

Example – class vs instance public class BankAccount{ // instance fields String name; double balance; // class field static double interestRate; // instance method public double getBalance(){ return balance; } // class method public static double getRate(){ return interestRate; }... }

Example – class vs instance Code to setup and use BankAccounts //instantiate a BankAccount BankAccount myAcc = new BankAccount(...); // access its instance field balance System.out.println(myAcc.getBalance()); // access its class field interestRate System.out.println(BankAccount.interestRate); default constructor

Method Overloading Defining methods within the same class with the same name but with different parameter lists Java compiler determines which method to call based on the parameter list Often used for constructors BankAccount(String name){ this.name=name; this.balance=0; } BankAccount(String name, double balance){ this.name=name; this.balance=balance; };

Inheritance Inheritance allows classes to be considered in a hierarchy with the data and methods of the parent (super) class being passed onto (inherited) by the child (sub) classes and not needed in their specifications implemented using the extends keyword e.g. class subClass extends superClass { … } an instance of subClass can also be treated as an instance of superClass

Object class Every class defined has a superclass If a superclass is not explicitly defined it is the superclass java.lang.Object Object is a special class –the only class that does not have a superclass –all Java classes inherit the methods of Object –instances of any class can be treated as an instance of Object –all arrays are also considered instances of Object

Inheritance example public class LoanAccount extends BankAccount{ double loanAmount; int loanPeriod;// no of yrs... }

Super Constructors are not inherited by subclasses A subclass should call the constructor of its superclass to perform the initialisation on the instance fields that the superclass is responsible for This is done using the super reference super(param1, param2,…) must always be the first line in the subclass constructor code

super Example public class BankAccount{ String name; double balance=0; public BankAccount (String name){ this.name=name; }... } public class LoanAccount extends BankAccount{ double loanAmount; int loanPeriod;// no of yrs public LoanAccount(name, amt, period){ super(name); this.loanAmount=amt; this.loanPeriod=period; }... }

Points about Inheritance Multiple Inheritance –a Java class may only extend from one superclass - multiple inheritance is not supported (see interfaces for work around) A class that is declared with the final modifier cannot be extended or subclassed

Points about Inheritance Working within a hierarchy –any object of a subclass can be assigned to an object of a superclass –any object of a superclass can be assigned to an object of a subclass with an appropriate cast BankAccount myAcc = new BankAccount(…); LoanAccount myLoan = new LoanAccount(…); myAcc = myLoan; // OK myLoan = myAcc // not OK myLoan = (LoanAccount) myAcc; // OK if(myAcc instanceOf LoanAccount) LoanAccount myLoan = (LoanAccount) myAcc; // better

Shadowing Fields A shadowed field is where a subclass replaces data from a superclass with a different version of the data Accessing shadowed fields public class ShortTermLoan extends LoanAccount{ float loanPeriod;// no of yrs loanPeriod //refers to ShortTermLoan field this.loanPeriod //refers to ShortTermLoan field super.loanPeriod //refers to LoanAccount field

Overriding Methods Overriding is where a subclass decides to supply its own version of an instance method already supplied by its superclass –general or default method provided in superclass –more specialised method provided in subclass Example - toString() method

toString() The root Object class has a toString() method which allows an object of the class to be output as a string public String toString(); The toString() method is overridden at all levels in the hierarchy Useful for outputing details of any object System.out.println(“Account details: “+myAcc); string concatenation automatically invokes a toString() on myAcc to convert it to a String

Overriding example public class BankAccount { public String toString(){ String s=“Bank Account for ”; s+=name+”\nbalance= “+balance; return s; } } public class LoanAccount { public String toString(){ String s=“\nLoan Account for ”; s+=name+”\nbalance= “+balance; s+=“Loan of ”+loanAmt; s+=“ for ”+loanPeriod+ “years”; return s; } }

super use the super keyword to invoke the superclass version of an overridden method // Bank Account toString method public String toString(){ String s=“Account for ”; s+=name+”\nbalance= “+balance; return s; } // Loan Account toString method public String toString(){ String s= super.toString(); s+=“Loan of ”+loanAmt; s+=“ for ”+loanPeriod+ “years”; return s; }

Overriding Where there are several instances of the same method in a hierarchy – the one in the closest subclass is always used. Even if an object of the subclass is assigned to an object of the superclass and method is invoked from the subclass. System.out.println(myAcc); uses BankAccount toString() System.out.println(myLoan); uses LoanAccount toString() myAcc=myLoan; System.out.println(myAcc); still uses LoanAccount toString()

Dynamic Binding Overriding uses dynamic binding or dynamic method lookup to find the correct method at run time Example: Consider an account object acc, acc.toString() which toString() method should be run? The interpreter checks the type of the object acc and then finds the toString() method that is appropriate for that type Dynamic binding is also known as virtual method invocation (what C++ does for virtual functions) Note: Overriding is not the same as Overloading Overloading does not use dynamic binding

Shadowing vs Dynamic Binding class A{ int i=1;// an instance field int f(){ return i;};// an instance method } class B extends A{ // define a subclass of A int i=2;// shadow field i in A int f(){ return –i; };// override method f in A }... B b = new B(); // create obj of type B System.out.println(b.i); // prints 2 System.out.println(b.f()); // prints –2 A a = (A) b; // cast b as a obj of class A System.out.println(a.i); // prints 1 System.out.println(a.f(g)); // still prints -2

Polymorphism Overriding + dynamic binding = polymorphism Polymorphism is the ability of different objects to respond to the same message (method) in their own way

Encapsulation Encapsulation is the packaging of the object’s fields (and internal methods) within a protective shell/capsule of its methods –an object has a “public” interface that other objects use to communicate with it –an object maintains “private” information that can be changed without affecting other objects that depend on it or use it –“blackbox” effect Encapsulation achieved using Information Hiding

Information Hiding Information Hiding – reveal only what is needed to the user of the object –hides implementation details from user –facilitates maintenance –protects against willful or accidental damage –keeps API simple and “uncluttered” –facilitates ease of understanding and use

Accessibility Modifiers Information Hiding implemented through accessibility modifiers –public = accessible to any other object –private = accessible within the object itself –protected = accessible within the object and any of its subclasses (and to all classes within the package)

Encapsulation To implement encapsulation –All fields should be private –Include ‘get’ and ‘set’ methods (accessor methods) for all fields that need to be accessed by users –Hide internal methods (i.e. methods that are not relevant to users but are only used by the class itself)