Presentation is loading. Please wait.

Presentation is loading. Please wait.

More inheritance, Abstract Classes and Interfaces

Similar presentations


Presentation on theme: "More inheritance, Abstract Classes and Interfaces"— Presentation transcript:

1 More inheritance, Abstract Classes and Interfaces
Week 9 More inheritance, Abstract Classes and Interfaces

2 Lecture Outline Inheritance Upcasting and downcasting instanceof
Abstract class and methods Java Interface final keyword in Java

3 Upcasting and downcasting
Upcasting Java supports an object of a subclass type to be treated as an object of any superclass type. It is done automatically. Downcasting It is to be performed manually by the developers.

4 Upcasting and downcasting contd.
Object implicit Animal Cat Bird Downcasting

5 Upcasting and downcasting contd.
Upcasting and downcasting are NOT like casting primitive datatypes from one to other By casting we are not actually changing the object itself, you are just labelling it differently

6 Upcasting and downcasting contd.
Object implicit Animal Cat Bird Cat is-a Animal √ Animal is-a Cat X Animal is-a Bird X Downcasting Bird is-a Animal √

7 Upcasting and downcasting contd.
Animal -name: String +sound(): void Cat +sound(): void Bird + sound() : void

8 Upcasting and downcasting contd.
public class Animal { String name; Animal(){} Animal(String name){ this.name = name; } public void sound() { System.out.println("No sound!");

9 Upcasting and downcasting contd.
class Cat extends Animal { Cat (String name) { super(name); } public void sound() { System.out.println(name+" Meowing!");

10 Upcasting and downcasting contd.
class Bird extends Animal { Bird (String name) { super(name); } public void sound() { System.out.println(name+" Tweeting!");

11 Upcasting and downcasting contd.
class TestAnimal{ public static void main(String[] args) { Cat cat = new Cat("Bela"); cat.sound(); Bird bird = new Bird("Happy"); bird.sound(); Animal animal animal.sound(); } = new Animal(); Output: ? }

12 Upcasting and downcasting contd.
class TestAnimal{ public static void main(String[] args) { Cat cat = new Cat("Bela"); cat.sound(); Bird bird = new Bird("Happy"); bird.sound(); Animal animal animal.sound(); } = new Animal(); Output: Bela Meowing! Happy Tweeting! No sound! }

13 Upcasting example class TestAnimal{
public static void main(String[] args) { Cat cat = new Cat("Bela"); cat.sound(); Bird bird = new Bird("Happy"); bird.sound(); Animal animal = new Animal(); animal = cat; //automatic upcasting animal.sound(); }

14 Upcasting example class TestAnimal{
public static void main(String[] args) { Cat cat = new Cat("Bela"); cat.sound(); Bird bird = new Bird("Happy"); bird.sound(); Animal animal = new Animal(); animal = cat; //automatic upcasting animal.sound(); Output: } Bela Meowing! Happy Tweeting! Bela Meowing! }

15 Downcasting example class TestAnimal{
public static void main(String[] args) { Cat cat = new Cat("Bela"); cat.sound(); Bird bird = new Bird("Happy"); bird.sound(); Animal animal = new Animal(); animal = cat; //automatic upcasting Cat cat2 = (Cat) animal;//downcasting manually cat2.sound(); } Output: Bela Meowing! Happy Tweeting! Bela Meowing! }

16 Observation (1) class TestAnimal{
public static void main(String[] args) { Cat cat = new Cat("Bela"); cat.sound(); Bird bird = new Bird("Happy"); bird.sound(); Animal animal = new Animal(); animal = cat; //automatic upcasting Bird bird2 = (Bird) animal;//downcasting?? bird2.sound(); } Output: ??

17 Observation(2) As in the Hierarchy we can have many different Animals, and if we want to downcast them all to Cat, what happens? For example, Cat to Bird But Cat is definitely not-a Bird Exception in thread "main" java.lang.ClassCastException: OOSD1.Cat cannot be cast to OOSD1.Bird This is because of Dynamic binding (although code will compile)

18 instanceof class TestAnimal{
public static void main(String[] args) { Cat cat = new Cat("Bela"); cat.sound(); Bird bird = new Bird("Happy"); bird.sound(); Animal animal = new Animal(); animal = cat; //automatic upcasting if(animal instanceof Cat){ Cat cat2 = (Cat) animal;//downcasting } cat2.sound();

19 Observation(3) Incompatible types: Shape cannot be converted to Square
class TestAnimal { public static void main(String[] args) { Cat cat = new Animal(); } Incompatible types: Shape cannot be converted to Square

20 Abstract class

21 How classes are designed
Super classes are created through the process called "generalization" Common features (methods/variables) are factored out of object classifications Those features are formalized in a class This becomes the superclass The classes from which the common features were taken become subclasses to the newly created super class

22 How classes are designed contd.
Often, the superclass does not have a "meaning" or does not directly relate to a "thing" in the real world It is an artefact of the generalization process Because of this, abstract classes cannot be instantiated They act as place holders for abstraction

23 Example Abstract base class named in Italic
Animal -name: String +sound(): void Abstract base class named in Italic Cat +sound(): void Bird + sound() : void In this example, the subclasses represent objects (classes) taken from the problem domain. The superclass represents an abstract concept that does not exist "as is" in the real world.

24 Observation In the following, doesn’t seem like we have enough information to get the sound() if all we know is it is an Animal. public class Animal { String name; Animal(){} Animal(String name){ this.name = name; } public void sound() { System.out.println("No sound!");

25 Potential Solutions Just leave it for the sub classes.
– Have each sub class define sound() Define sound() in Animal as in the previous slide Sub classes override the method with more meaningful behavior as in the last example.

26 A better solution We know we want to be able to find the sound of objects that are instances of Animal The problem is we don’t know how to do that if all we know is its an Animal Make sound() an abstract method-Java Keyword public class Animal { String name; Animal(){} Animal(String name){ this.name = name; } public abstract void sound();

27 Abstract method public class Animal { String name; Animal(){} Animal(String name){ this.name = name; } public abstract void sound(); // We know we want it, but don’t know how, yet…

28 Abstract method Methods that are declared abstract have no body an undefined behavior. We can override this sound() method in the Cat and Square subclasses, and implement details

29 Abstract method contd. Now we know how to define the sound()
class Cat extends Animal { Cat (String name) { super(name); } public void sound() { System.out.println(name+" Meowing!"); Now we know how to define the sound()

30 Abstract method contd. Now we know how to define the sound()
class Bird extends Animal { Bird (String name) { super(name); } public void sound() { System.out.println(name+" Tweeting!"); Now we know how to define the sound()

31 Abstract method contd. sound() is now an abstract method in Animal
what is wrong with the following code? Animal animal = new Animal(); System.out.println(animal.sound());

32 Abstract method contd. sound() is now an abstract method in Animal
what is wrong with the following code? Animal animal = new Animal(); System.out.println(animal.sound()); Undefined behavior! BAD

33 Abstract class Not good to have undefined behaviors
If a class has ONE or more abstract methods, the class must also be declared abstract. abstract class Animal { String name; Animal(){} Animal(String name){ this.name = name; } public abstract void sound();

34 Abstract class what is wrong with the following code?
abstract class Animal { String name; Animal(){} Animal(String name){ this.name = name; } public abstract void sound(); what is wrong with the following code? Animal animal = new Animal();

35 Animal animal = new Animal();
Abstract class abstract class Animal { String name; Animal(){} Animal(String name){ this.name = name; } public abstract void sound(); what is wrong with the following code? Animal animal = new Animal(); //Syntax error If a class is abstract, then we cannot create instances of that class

36 What is an abstract class?
An abstract class is a class that is declared abstract. Important: Even if a class has zero abstract methods a programmer can still choose to make it abstract Abstract classes cannot be instantiated, but they can be sub classed.

37 Abstract class example
public abstract class Animal { String name; Animal(){} Animal(String name){ this.name = name; } public abstract void sound();

38 Abstract class example contd.
class Cat extends Animal { Cat (String name) { super(name); } public void sound() { System.out.println(name+" Meowing!");

39 Abstract class example contd.
class Bird extends Animal { Bird (String name) { super(name); } public void sound() { System.out.println(name+" Tweeting!");

40 Abstract class example contd.
class TestAnimal{ public static void main(String[] args) { Cat cat = new Cat("Bela"); cat.sound(); Bird bird = new Bird("Happy"); bird.sound(); //Animal animal = new Animal(); //animal.sound(); } } Output: Bela Meowing! Happy Tweeting!

41 Abstract class properties
Abstract classes may or may not contain abstract methods If a class contains at least one abstract method, then the class must be declared abstract If a class is declared abstract, it cannot be instantiated To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it. If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.

42 Abstract class properties contd.
If you don’t implement an abstract method (declared in the abstract class ) in the sub-class, then what will happen? public abstract class Animal{ String name; Animal(){} Animal(String name){ this.name = name; } public abstract void sound(); class Cat extends Animal { Cat (String name) { super(name);

43 Abstract class properties contd.
If you don’t implement an abstract method (declared in the abstract class ) in the sub-class, then what will happen? class TestAnimal { public static void main(String[] args) { Cat cat = new Cat("Bela"); }

44 Abstract class properties contd.
If you don’t implement an abstract method (declared in the abstract class ) in the sub-class, then what will happen? class TestAnimal { public static void main(String[] args) { Cat cat = new Cat("Bela"); //Error }

45 Week 10 Java Interfaces

46 Learning Outcomes At the end of this session, You will able to:
Understand Java Interface; To Analyse how Interfaces and different then Abstract classes; Design and Implement Interfaces and Abstracts.

47 Lecture Outline Java Interface Interface vs Abstract
final keyword in Java

48 What is an Interface? A Java Interface is just a definition of behaviour, similar to a Java abstract class (we will see their differences shortly) It is not a class but it is defined in a way similar to the class definition An Interface describes the functions and behaviors of a specific object type but doesn't implement them When a class implements an interface it has to implement all methods declared in that interface.

49 Java Interface Example
Interface Animal // interface declaration public interface Animal { // constant public static final String type = "Mammal"; //abstract method public void sound (); }

50 Java Interface Example
Cat implements Interface Animal class Cat implements Animal { private String name; Cat (String name) { this.name = name; } // class declaration public void sound () { System.out.println(name+" Meowing!"); }

51 Java Interface Example
Bird implements Interface Animal class Bird implements Animal { private String name; Bird (String name) { this.name = name; } // class declaration public void sound () { System.out.println(name+" Tweeting!"); }

52 Java Interface Example
class TestAnimal { public static void main (String[] args) { Animal animal1 = new Cat ("Bela"); Animal animal2 = new Bird ("Happy"); animal1.sound(); animal2.sound(); } Output: Bela Meowing! Happy Tweeting!

53 Abstract class vs. Interface

54 Interface Implementation
interface MotorVehicle { void run(); int getFuel(); } // My team mate complies and writes vehicle looking that way class Car implements MotorVehicle int fuel; void run() print(“Running…………….."); int getFuel() return this.fuel;

55 Abstract Class abstract class MotorVehicle { int fuel;
// They ALL have fuel, so lets implement this for everybody. int getFuel() return this.fuel; } // That can be very different, force them to provide their // own implementation. abstract void run(); // My teammate complies and writes vehicle looking that way class Car extends MotorVehicle void run() print(“Running………………");

56 final keyword in Java

57 final Variable Final variable is just like constant in C
class FinalVariable { public static void main(String []args) { final double PI= Math.PI; //final integer variable //try to change value of PI PI= 4.14; System.out.println("Value of PI is: " + PI); } Error: cannot assign a value to final variable PI

58 final Method A final method can not be overridden
class BaseClass { final public void display(){ System.out.println("I'm in Base class"); } class SubClass extends BaseClass public void display(){ System.out.println("I'm in Sub class"); Error: display()cannot override in BaseClass

59 final Class A final class can not be inherited
final class BaseClass { public void display(){ System.out.println("I'm in Base class"); } class SubClass extends BaseClass public void display(){ System.out.println("I'm in Sub class"); Error: SubClass cannot inherit from final BaseClass

60 Reference Chapters 11&13, Introduction to Java Programming, Liang.


Download ppt "More inheritance, Abstract Classes and Interfaces"

Similar presentations


Ads by Google