1 / 28

Up Casting & Down Casting Abstract Classes

Polymorphism Part-II. Up Casting & Down Casting Abstract Classes. Prepared by Miss Simab Shahid (s.shahid@uoh.edu.sa) Lecturer computer Science and Software Engineering department, University of Hail. Up Casting (Example). class Shape { int xpos, ypos ; public Shape( int x , int y){

otylia
Download Presentation

Up Casting & Down Casting Abstract Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Polymorphism Part-II Up Casting & Down CastingAbstract Classes Prepared by Miss Simab Shahid (s.shahid@uoh.edu.sa) Lecturer computer Science and Software Engineering department, University of Hail

  2. Up Casting (Example) class Shape { int xpos, ypos ; public Shape(int x , int y){ xpos = x ; ypos = y ; } public void Draw() { System.out.println("Draw( ) of Shape") ; } } class Circle extends Shape { int r ; public Circle(int X , int Y , int R){ super(X , Y) ; r = R ; } public void Draw( ) { System.out.println("Draw( ) of Circle") ; } publicvoid Surface( ) { System.out.println(“Surface of Circle ") } }

  3. class Demo { public static void main ( String [ ] args ){ Shape s ; s = new Shape (2,5 ); s.Draw( ); s = new Circle(10 , 20 , 4) ; s.Draw() ; } } • Reference variable type determines that which method name can be used using this variable. • But the object named by that reference variabledetermines which definition of the method name is used. • Reference variable s of shape class determines, that using this variable method named Draw( ) can be used. • The definition of which Draw( ) s will use, the object type will determine. • If the object of Shape class will assigned to s, the Shape class definition of Draw( )will be used. • If the object of Circle class will assigned to s, the Circle class definition of Draw( )will be used. This Up Casting:Object of derived class is assigned to the reference variable of base class

  4. Up Casting In the previous example by writing s = newCircle(10 , 20 , 4) ;the object of Derived Class is assigned to the reference variable of Base Class means we have type cast Circle to Shape. This is possible because Circle has been derived from Shape. From Circle ,we are moving Up to the object hierarchy to the type Shape. So we are casting our object “Upwards” to its parent type. When the object of derived class is assigned to the reference variable of base class is called Up Casting.

  5. Down Casting class Shape { int xpos, ypos ; public Shape(int x , int y){ xpos = x ; ypos = y ; } public void Draw() { System.out.println("Draw( ) of Shape") ; } } class Circle extends Shape { int r ; public Circle(int x1 , int y1 , int r1){ super(x1 , y1) ; r = r1 ; } public void Draw() { System.out.println("Draw( ) of Circle") ; } publicvoid Surface() { System.out.println(“Surface( ) of Circle ); } }

  6. class Demo { public static void main ( String [ ] args ){ Shape s ; s = new Shape (2,5 ); s.Draw( ); s = new Circle(10 , 20 , 4) ; s.Draw() ; s.Surface( ) ; ((Circle) s).Surface( ) ; } } • Reference variable s of shape class determines, that using this variable method named Draw( ) can be used only. • The definition of which Draw( ) s will use, the object type will determine. • If the object of Shape class will assigned to s, the Shape class definition of Draw( )will be used. • If the object of Circle class will assigned to s, the Circle class definition of Draw( )will be used. • But Circle class object also has the definition of Surface( ). • So it would be correct to write? • The correct code will be to write.

  7. Down Casting In the previous example by writing ((Circle) s).Surface( ) ; The reference variable s of shapeclass is type cast to Cirlce class first. Then the Surface( ) method of Circle object is invoked for s. This is done because the reference variable of shape class does not know about the name of Surface( ) method, as this method the Circle class is not inheriting from Shape class. Only the Cirlce class reference variable knows about the Surface( ) mehtod name. So before calling Surface( ) of Circle object S must be down-cast first to Circle. In this case, we will move down the object hierarchy from Shape to Circle : Hence is called Down Casting.

  8. Down Casting It is the responsibility of the programmer to use Down Casting only in situations where it makes sense The compiler does not check to see if Down Casting is a reasonable thing to do Using Down Casting in a situation that does not make sense usually results in a Run-time error.

  9. Upcasting and downcasting class Animal { int health = 100; } class Mammal extends Animal { } class Cat extends Mammal { } class Dog extends Mammal { } public class Test { public static void main(String[] args) { Cat c = new Cat(); System.out.println(c.health); Dog d = new Dog(); System.out.println(d.health); } } Simab Shahid UOH, Girls Branch 9

  10. Upcasting and downcasting Downcasting has to be preceded by upcasting Code: Cat c = new Cat( ); Mammal m = c; // upcasting Simab Shahid UOH, Girls Branch Cat c1 = new Cat( ); Animal a = c1; //automatic upcasting to Animal Cat c2 = (Cat) a; //manual downcasting back to a Cat 10

  11. Polymorphism and References Downcasting has to be preceded by upcasting it's legal for a variable of a superclass type to refer to an object of one of its subclass types Shape myShape = new Circle(); // allowed Shape myShape2 = new Rectangle(); // allowed Rectangle myRectangle = new Shape(); // NOT allowed 11

  12. Simab Shahid UOH, Girls Branch Shape s = newCircle();  Circle s = newShape();  12

  13. Abstract Method An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon) Example : abstract void moveTo(double deltaX, double deltaY); Important: void moveTo(double deltaX, double deltaY) { } is not abstract Method. Rather it is Empty Method. Simab Shahid UOH, Girls Branch 13

  14. Abstract Method • In order to postpone the definition of a method, Java allows an abstract method to be declared • An abstract method has a heading, but no method body Simab Shahid UOH, Girls Branch 14

  15. Abstract Methods • An abstract method is like a placeholder for a method that will be fully defined in a descendent class • It has a complete method heading, to which has been added the modifier abstract • It cannot be private • It has no method body, and ends with a semicolon in place of its body • public abstract double getPay( ); • public abstract void doIt(intcount); Simab Shahid UOH, Girls Branch 15

  16. Activity 01 abstractvoid Draw( ) { System.out.println(“ Draw Circle”); } Is this abstract method ? Simab Shahid UOH, Girls Branch If your answer is No give reason and also write the correct answer. 16

  17. Activity 02 abstractvoid Draw( ) { } Is this abstract method ? Simab Shahid UOH, Girls Branch If your answer is No give reason and also write the correct answer. 17

  18. Activity 03 void Draw( ); Is this abstract method ? Simab Shahid UOH, Girls Branch If your answer is No give reason and also write the correct answer. 18

  19. Abstract Classes • A class that contains ONE or MORE abstract methods is called an abstract class public abstract class Employee { private String name; private Date hireDate; public abstract double getPay( ); . . . } Simab Shahid UOH, Girls Branch 19

  20. ABSTRACT CLASSE EXAMPLE public abstractclass Animal { private String name; public Animal( String Name) { name = Name; } public String getName() { return name; } public abstract void speak(); // The abstract Method } Simab Shahid UOH, Girls Branch 20

  21. ABSTRACT CLASS AND OBJECT CREATION • It is not allowed to create object of abstract class OR abstract class cant beinstantiated public abstract class Animal { private String name; public Animal(String Name) { name = Name; } public void DisplayName() { System.out.println(name); } public abstract void speak(); // The abstract Method } Simab Shahid UOH, Girls Branch 21

  22. public class Test{ public static void main( String args[]) { Animal A = new Animal (“Manoo”); } } Output (It will give Run Time Error) Exception in thread "main" java.lang.InstantiationError: Animal at functionOverride.main(functionOverride.java:14) SimabShahid UOH, Girls Branch Important Point: We cant CREATE an object of Abstract class. 22

  23. ABSTRACT CLASSES AND INHERITANCE • It is not allowed to create object of abstract classes, but it is allowed to make an abstract class a base class • The body of the ABSTRACT method is defined in the derivedclasses public abstract class Animal { private String name; public Animal(String Name) { name = Name; } public void DisplayName() { System.out.println(name); } public abstract void speak(); // The abstract Method } Simab Shahid UOH, Girls Branch 23

  24. public class Cat extends Animal{ public Cat( String Name) { super(Name); } // Implementing the abstract method of Animal class public void speak() { System.out.println(“Maoo Maoo”); } } Simab Shahid UOH, Girls Branch public class Test{ public static void main( String args[ ]) { Cat c = new Cat(“Manoo”); c. speak(); } } Output : MaooMaoo 24

  25. If the derivedclass is also not implementing the Abstract method of abstract base class , then the derived class will also be declared as Abstract public abstract class Animal { private String name; public Animal(String Name) { name = Name; } public void DisplayName() { System.out.println(name); } public abstract void speak(); // The abstract Method } Simab Shahid UOH, Girls Branch 25

  26. public abstractclass Cat extends Animal{ public Cat( String Name) { super(Name); } } Simab Shahid UOH, Girls Branch 26

  27. ABSTRACT CLASSES AND INHERITANCE • It is not allowed to create object of abstract class, but it is allowed to make an abstract class a base class public abstract class Animal { private String name; public Animal(String Name) { name = Name; } public void DisplayName() { System.out.println(name); } public abstract void speak(); // The abstract Method } Simab Shahid UOH, Girls Branch 27

  28. If the derivedclass is also not implementing the Abstract method of abstract base class , then the derived class will also be declared as Abstract public abstractclass Cat extends Animal{ public Cat( String Name) { super(Name); } } Simab Shahid UOH, Girls Branch 28

More Related