Topics OOP Review Inheritance Review Abstract Classes Interface
Topics • OOP Review • Inheritance Review • Abstract Classes • Interface Classes
Quick Review of OOP • Base class equivalent terms: – Superclass – Parent class • Sub class equivalent terms: – Derived class – Child class
Quick Review of OOP Child class inherits ALL the data and method members of its Parent class.
Quick Review of OOP The keyword super refers to the superclass of the class in which you use it.
Quick Review of OOP • The keyword private is used to hide data or methods from other classes within the package. • The keyword public is used to make data or methods available to other classes. • The keyword protected is a version of public, but it is restricted only to subclasses. • The keyword static can be used with classes, variables, and methods. Static elements belong to the class instead of a specific instance.
Quick Review of Inheritance A child object cannot directly access a private member inherited from a parent.
Quick Review of Inheritance Java’s Object class is the super class for all Java classes. Every Java class is directly or indirectly derived from the Object class.
Object Class The Object class provides a collection methods that descendant classes can use or override. to. String(): Converts an Object into a String equals(): Takes a single argument that is compared to the calling object.
What is an Abstract Class?
Abstract Class • An abstract class is a general parent class. • Objects cannot be instantiated from an abstract class. • Objects can be instantiated from a specific concrete class.
Animal Abstract Dog Specific type of Animal Cat Specific type of Animal
Details: Abstract Class • Abstract classes usually contain at least one abstract method. • When creating an abstract method, use the keyword abstract. • Abstract methods have no body and must be implemented in child classes.
Example: Abstract Animal Class public abstract class Animal { private String animal. Name; public String get. Animal. Name(){ return animal. Name; } public void set. Animal. Name(String animal. Name) { this. animal. Name = animal. Name; } } TIP 1: TIP 2: public abstract String speak(); Abstract classes usually have one or more empty abstract methods. An abstract method has no body.
Extending an Abstract Class • Dog is a concrete class that can be extended from Animal. • Dog must implement (override) the abstract Animal method speak()
//DOG IS AN EXTENSION OF THE ABSTRACT CLASS ANIMAL public class Dog extends Animal { } public String speak(){ return "Bark, Bark!!!"; }
Question 1 : What output is produced by the code below? public class My. Main { public static void main(String[] args) { Dog dog = new Dog(); Cat cat = new Cat(); dog. set. Animal. Name("Rex"); System. out. println(dog. get. Animal. Name() + " says " + dog. speak()); cat. set. Animal. Name("Fluffy"); System. out. println(cat. get. Animal. Name() + " says " + cat. speak()); } }
Answer 1 Rex says Bark, Bark!!! Fluffy says Meow, Meow!!!
Interface Class
What is an interface? • Interfaces are useful in Java in that they allow inheritance from one or more parent classes. • An interface looks much like a class. • An interface is a description of what a class does, but not how it is done.
interface methods and data members • An interface contains methods, all of which are implicitly public and abstract. • An interface contains data members, all of which are implicitly public, static, and final. • Interfaces, like abstract classes and methods, provide templates of behavior that other classes are expected to implement.
Using an interface • When creating a class that uses an interface, the keyword implements must be used. • When a class implements from an interface class, the child class must implement its own version of each method.
Interface Example • Assume that Vehicle is an interface. • Car and Airplane are both implemented as Vehicles. • Bicycle is not implemented as a Vehicle.
public interface Vehicle { //DATA MEMBERS ARE IMPLICITLY PUBLIC AND FINAL // -----------------------------------int REVERSE = -1; } //METHODS ARE IMPLICITLY PUBLIC AND ABSTRACT // -----------------------------------String start(); String stop(); void change. Speed(int new. Speed);
public class Car implements Vehicle{ private int speed; public Car(){ speed = 0; } public int get. Speed(){ return speed; } // Vehicle INTERFACE METHODS MUST BE IMPLEMENTED //_________________________ public String start() { return "Car is started and is in motion"; } public String stop() { return "Car has come to a halt. "; } } public void change. Speed(int new. Speed) { this. speed = new. Speed; }
Question 2: What output is produced by the code below? public class My. Main { public static void main(String[] args) { Car car. A = new Car(); System. out. println(car. A. start()); car. A. change. Speed(Vehicle. REVERSE); System. out. println(car. A. stop()); } }
Answer 2 Car is started and is in motion Car has come to a halt.
Question 3: Which of the following statements are invalid? public static void main(String[] args) { Vehicle my. Car 1 = new Car(); Car my. Car 2 = new Vehicle(); Car my. Car 3 = new Car(); } }
Answer 3 Answer: Car my. Car 2 = new Vehicle(); • The declaration of my. Car 1 indicates that there is an 'is a ' relationship between the implementing classes and the interface. • The declaration of my. Car 3 is a Car instance.
Consider the Bicycle Class public class Bicycle { private int speed; public Bicycle(){ speed = 0; } public int get. Speed(){ return speed; } public String start() { return "Car is started and is in motion"; } public String stop() { return "Car has come to a halt. "; } public void change. Speed(int new. Speed) { this. speed = new. Speed; } } Bicycle is NOT implemented as a Vehicle.
Question 4: What output is produced by the code below? public class My. Main { public static void main(String[] args) { boolean is. Vehicle; Car car. B = new Car(); is. Vehicle = car. B instanceof Vehicle; if (is. Vehicle) System. out. println("car. B is an instanceof Vehicle"); else System. out. println("car. B is not an instanceof Vehicle"); Bicycle bike = new Bicycle(); is. Vehicle = bike instanceof Vehicle; if (is. Vehicle) System. out. println("bike is an instanceof Vehicle"); else System. out. println("bike is not an instanceof Vehicle"); } }
Answer 4 car. B is an instanceof Vehicle bike is not an instanceof Vehicle instance. Of indicates if there is an “is a” relationship between classes and the Vehicle interface.
- Slides: 31