Exam Objective : Legal return types PRESENTED BY : SRINIVAS VG.

Slides:



Advertisements
Similar presentations
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Advertisements

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.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Inheritance Inheritance allows the derivation of a new class from an existing one, for the purpose of reuse, enhancement, adaptation, etc. superclass (a.k.a.
Exam Objective : Demonstrate the use of polymorphism PRESENTED BY: SRINIVAS VG.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Java Generics.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Sun Certified Java Programmer 1 © CPU, University of Limerick 2008 OBJECT ORIENTATION.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
LECTURE 07 Programming using C# Inheritance
Abstract classes and Interfaces. Abstract classes.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OVERRIDING/OVERLOADING Srinivas. EXAM OBJECTIVES  Given a code example, determine if a method is correctly overriding or overloading another method,
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
Java Implementation: Part 3 Software Construction Lecture 8.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
Types in programming languages1 What are types, and why do we need them?
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Inheritance and Access Control CS 162 (Summer 2009)
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Java for C++ Programmers A Brief Tutorial. Overview Classes and Objects Simple Program Constructors Arrays Strings Inheritance and Interfaces Exceptions.
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.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Wel come To Seminar On C#.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Inheritance ndex.html ndex.htmland “Java.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Polymorphism in Methods
Chapter 11 Inheritance and Polymorphism
CompSci 230 S Programming Techniques
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
Subtyping Rules David Evans cs205: engineering software BlackBear
Extending Classes.
Introduction interface in Java is a blueprint of a class. It has static constants and abstract methods only. An interface is a way to describe what classes.
OBJECT ORIENTATION.
class PrintOnetoTen { public static void main(String args[]) {
Scope of variables class scopeofvars {
Java Programming Language
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 11 Inheritance and Polymorphism Part 1
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Inheritance and Polymorphism
Presentation transcript:

Exam Objective : Legal return types PRESENTED BY : SRINIVAS VG

Agenda :  Return types on overloaded methods  Overriding and Return types, and Covariant returns  Returning a value

Return types on Overloaded methods :  To overload a method, you must change the argument list Eg:- public class Foo{ void go(){} } public class Bar extends Foo{ String go(int x){ return null; }

 As long as there is change in argument list, return type doesn’t have to match with that of superclass version  Find the error – public class Foo{ void go(){} } public class Bar extends Foo{ String go(){ return null; } }  Can’t change only the return type

Return types while Overriding :  Only in JAVA 5 you’re allowed to change the return type while overriding only in case of covariant returns Eg : Look at the covariant return class Alpha { Alpha dostuff(char c){ return new Alpha(); } class Beta extends Alpha { Beta doStuff(char c){ // legal override in java 1.5 return new Beta(); }

Covariant returns :  A class could not override the return type of the methods it inherits from a superclass  Applicable only for JAVA 5 version  A method in a subclass may return an object whose type is a subclass of the type returned by the method with the same signature in the superclass Alpha Beta

Till now we know :  Overloaded methods can change the return types  Overridden methods cannot, except in the case of covariant returns

Returning a value : There are 6 rules  You can return null with an object reference return type Eg: public Button dostuff(){ return null; } Eg; class Dummy{ public Dummy dum(){ return null; } public static void main(String args[]){ Dummy n=new Dummy(); Dummy h=n.dum(); System.out.println(h); }

 An array can be returned Eg: public String[] go(){ return new String[]{"Fred","Barney","Wilma"}; } Eg: class Dummy{ public String[] dum(){ return new String[] {"fred","Barney","wilma"}; } public static void main(String args[]){ Dummy n=new Dummy(); String str[]=n.dum(); System.out.println(str[0]+" "+str[1]+" "+str[2]); }

 You can return any value or variable that can be implicitly converted to the declared return type Eg : public int foo(){ char c='c'; return c; // char is compatible with int } Eg: class Dummy{ public int dum(){ char c='a'; return c; } public static void main(String args[]){ Dummy n=new Dummy(); int x=n.dum(); System.out.println("x value is "+x ); } Valid only for primitive return types :

 You can return any value or variable that can explicitly cast to declared return type Eg : public int foo(){ float f=32.5f; return (int)f; } Eg: class Dummy{ public int dum(){ float f=32.5f; return (int)f; } public static void main(String args[]){ Dummy n=new Dummy(); int x=n.dum(); System.out.println("x value is "+x ); }

 If the declared return type is void, then you should not return anything Eg : public void bar(){ return " jai only "; // not legal } - However for void return type you can just say as return i.e. in above just write return; // no harm in writing that

 With an object reference return type, you can return any object type that can implicitly cast to the declared return type Eg : public Animal getAnimal(){ return new Horse(); //Assume Horse extends Animal } Eg: class Animal{ } class Horse extends Animal{ public Animal getAnimal(){ System.out.println("I am inside "); return new Horse(); } public static void main(String args[]){ Horse h=new Horse(); Animal animals=h.getAnimal(); }

More Examples : Eg (1) : public Object getObject() { int[] nums = {1,2,3}; return nums; // Return an int array, // which is still an object }

Eg (2) : interface Chewable{ void hello(); } class NotGum { public void bye(){ System.out.println(" bye "); } class Gum implements Chewable{ public void hello(){ System.out.println(" hello "); } public class TestChewable{ public Chewable getChewable(){ return new Gum(); } public static void main(String args[]){ TestChewable tt=new TestChewable(); Chewable ch; ch=tt.getChewable(); }

Eg (3) : public abstract class Animal { } public class Bear extends Animal { } public class Test { public Animal go() { return new Bear(); // OK, Bear "is-a" Animal } - This code will compile, the return value is a subtype