Topics OOP Review Inheritance Review Abstract Classes

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Chapter 1 Inheritance University Of Ha’il.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Inheritance Inheritance Reserved word protected Reserved word super
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Object Oriented Programming: Java Edition By: Samuel Robinson.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Chapter 12 Advanced Inheritance
Peyman Dodangeh Sharif University of Technology Fall 2014.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Interfaces and Inner Classes
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
BY:- TOPS Technologies
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Introduction to Object-oriented Programming
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Advanced Programming in Java
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Lecture 12 Inheritance.
Objects as a programming concept
Final and Abstract Classes
Inheritance and Polymorphism
Java Interfaces & Abstract Classes
Java Inheritance.
Week 8 Lecture -3 Inheritance and Polymorphism
Recitation 6 Inheritance.
Class Inheritance (Cont.)
More inheritance, Abstract Classes and Interfaces
Interface.
Java Inheritance.
Java Programming Language
Inheritance.
Advanced Programming Behnam Hatami Fall 2017.
METHOD OVERRIDING in JAVA
Inheritance Inheritance is a fundamental Object Oriented concept
Java Programming, Second Edition
Java Inheritance.
Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Java Inheritance.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

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. toString(): 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 Dog Cat Abstract 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 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.

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, Bark!!!"; }  

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()); }

Rex says Bark, Bark, Bark!!! Fluffy says Meow, Meow, Meow!!! Answer 1 Rex says Bark, Bark, Bark!!! Fluffy says Meow, 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 changeSpeed(int newSpeed); }

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;

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()); }

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.

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(); }

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.

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.

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"); }

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.