Download presentation
Presentation is loading. Please wait.
1
Topics OOP Review Inheritance Review Abstract Classes Interface Classes
2
Quick Review of OOP Base class equivalent terms:
Superclass Parent class Sub class equivalent terms: Derived class Child class
3
Quick Review of OOP Child class inherits ALL the data and method members of its Parent class.
4
Quick Review of OOP The keyword super refers to the superclass
of the class in which you use it.
5
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.
6
Quick Review of Inheritance
A child object cannot directly access a private member inherited from a parent.
7
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.
8
Object Class The Object class provides a collection methods that descendant classes can use or override. toString(): Converts an Object into a String equals(): Takes a single argument that is compared to the calling object.
9
What is an Abstract Class?
10
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.
11
Animal Dog Cat Abstract Specific type of Animal
12
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.
13
Example: Abstract Animal Class
public abstract class Animal { private String animalName; public String getAnimalName(){ return animalName; } public void setAnimalName(String animalName) { this.animalName = animalName; public abstract String speak(); TIP 1: Abstract classes usually have one or more empty abstract methods. TIP 2: An abstract method has no body.
14
Extending an Abstract Class
Dog is a concrete class that can be extended from Animal. Dog must implement (override) the abstract Animal method speak()
15
//DOG IS AN EXTENSION OF THE ABSTRACT CLASS ANIMAL
public class Dog extends Animal { public String speak(){ return "Bark, Bark, Bark!!!"; }
16
Question 1 : What output is produced by the code below?
public class MyMain { public static void main(String[] args) { Dog dog = new Dog(); Cat cat = new Cat(); dog.setAnimalName("Rex"); System.out.println(dog.getAnimalName() + " says " + dog.speak()); cat.setAnimalName("Fluffy"); System.out.println(cat.getAnimalName() + " says " + cat.speak()); }
17
Rex says Bark, Bark, Bark!!! Fluffy says Meow, Meow, Meow!!!
Answer 1 Rex says Bark, Bark, Bark!!! Fluffy says Meow, Meow, Meow!!!
18
Interface Class
19
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.
20
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.
21
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.
22
Interface Example Assume that Vehicle is an interface.
Car and Airplane are both implemented as Vehicles. Bicycle is not implemented as a Vehicle.
23
public interface Vehicle {
//DATA MEMBERS ARE IMPLICITLY PUBLIC AND FINAL // int REVERSE = -1; //METHODS ARE IMPLICITLY PUBLIC AND ABSTRACT String start(); String stop(); void changeSpeed(int newSpeed); }
24
public class Car implements Vehicle{
private int speed; public Car(){ speed = 0; } public int getSpeed(){ 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 changeSpeed(int newSpeed) { this.speed = newSpeed;
25
Question 2: What output is produced by the code below?
public class MyMain { public static void main(String[] args) { Car carA = new Car(); System.out.println(carA.start()); carA.changeSpeed(Vehicle.REVERSE); System.out.println(carA.stop()); }
26
Car is started and is in motion Car has come to a halt.
Answer 2 Car is started and is in motion Car has come to a halt.
27
Question 3: Which of the following statements are invalid?
public static void main(String[] args) { Vehicle myCar1 = new Car(); Car myCar2 = new Vehicle(); Car myCar3 = new Car(); }
28
Car myCar2 = new Vehicle();
Answer 3 Answer: Car myCar2 = new Vehicle(); The declaration of myCar1 indicates that there is an 'is a ' relationship between the implementing classes and the interface. The declaration of myCar3 is a Car instance.
29
Consider the Bicycle Class
public class Bicycle { private int speed; public Bicycle(){ speed = 0; } public int getSpeed(){ 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 changeSpeed(int newSpeed) { this.speed = newSpeed; Bicycle is NOT implemented as a Vehicle.
30
Question 4: What output is produced by the code below?
public class MyMain { public static void main(String[] args) { boolean isVehicle; Car carB = new Car(); isVehicle = carB instanceof Vehicle; if (isVehicle) System.out.println("carB is an instanceof Vehicle"); else System.out.println("carB is not an instanceof Vehicle"); Bicycle bike = new Bicycle(); isVehicle = bike instanceof Vehicle; System.out.println("bike is an instanceof Vehicle"); System.out.println("bike is not an instanceof Vehicle"); }
31
carB is an instanceof Vehicle bike is not an instanceof Vehicle
Answer 4 carB is an instanceof Vehicle bike is not an instanceof Vehicle instanceOf indicates if there is an “is a” relationship between classes and the Vehicle interface.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.