OOP in Java Review. CS356 Object-Oriented Design and Programming October 1, 2014

Similar documents
Object-Oriented ProgrammingInheritance & Polymorphism

Object-Oriented Programming More Inheritance

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Midterm Exam CS 251, Intermediate Programming October 8, 2014

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Midterm Exam CS 251, Intermediate Programming March 12, 2014

public UndergradStudent(String n, String m, String p) { programme = p; super(n, m);

Inheritance (Outsource: )

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

OVERRIDING. 7/11/2015 Budditha Hettige 82

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

Chapter 5. Inheritance

SUN Certified Programmer for J2SE 5.0 Upgrade. Download Full Version :

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Java Session. Day 2. Reference: Head First Java

Programming II (CS300)

Programming II (CS300)

Create a Java project named week10

What is Inheritance?

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

Abstract class & Interface

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Inheritance and Polymorphism

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

The Java language has a wide variety of modifiers, including the following:

Tutorial 8 Date: 15/04/2014

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Arrays Classes & Methods, Inheritance

Chapter 14 Abstract Classes and Interfaces

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

COP 3330 Final Exam Review

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

Object oriented programming Concepts

JAVA MOCK TEST JAVA MOCK TEST II

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Java Object Oriented Design. CSC207 Fall 2014

Programming overview

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Midterm Exam CS 251, Intermediate Programming March 6, 2015

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Exam Percentage: / 55 = %

Exercise: Singleton 1

Object Orientated Programming Details COMP360

INHERITANCE Mrs. K.M. Sanghavi

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Answer1. Features of Java

Object-Oriented Concepts

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

Object Oriented Programming. Java-Lecture 11 Polymorphism

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

CS1150 Principles of Computer Science Objects and Classes

Lecture 18 CSE11 Fall 2013 Inheritance

Inheritance and Substitution (Budd chapter 8, 10)

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Inheritance. Transitivity

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Classes, Objects, and OOP in Java. June 16, 2017

Inheritance (Part 5) Odds and ends

Introduction to Object-Oriented Programming

More Relationships Between Classes

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Android/Java Lightning Tutorial JULY 30, 2018

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

About 1. Chapter 1: Getting started with oop 2. Remarks 2. Examples 2. Introduction 2. OOP Introduction 2. Intoduction 2. OOP Terminology 3.

Inheritance & Polymorphism

Clarifying Roles. Jonathan Worthington German Perl Workshop 2007

Lecture 02, Fall 2018 Friday September 7

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Instance Members and Static Members

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Logistics. Final Exam on Friday at 3pm in CHEM 102

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

UML & OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 3 09/04/2012

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CS 1331 Fall 2016 Exam 2

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism

Object-Oriented Programming

Binghamton University. CS-140 Fall Dynamic Types

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

Polymorphism and Interfaces. CGS 3416 Spring 2018

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

Data Structures (list, dictionary, tuples, sets, strings)

Chapter 5 Object-Oriented Programming

A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

Inheritance (an intuitive description)

CS260 Intro to Java & Android 03.Java Language Basics

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Transcription:

OOP in Java Review CS356 Object-Oriented Design and Programming http://cs356.yusun.io October 1, 2014 Yu Sun, Ph.D. http://yusun.io yusun@csupomona.edu

Announcement Submit your GitHub username as soon as possible

Important The basic Java OOP features will be used through the whole course These features are frequently asked in tech interviews

Interface An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement

Web iclicker http://answer.yusun.io Test Question: What grade do you think you can get from this course?

Interface Question 1 Which of the following variable declarations are correct? public interface TestInterface1 { A B C D E int var1; final int var2; public int var3 = 100; private int var4 = 100; public static final int var5 = 100;

Interface Question 1 Which of the following variable declarations are correct? public interface TestInterface1 { int var1; Only constants can be declared in an interface final int var2; public int var3 = 100; private int var4 = 100; public static final int var5 = 100;

Interface Question 1 Which of the following variable declarations are correct? public interface TestInterface1 { int var1; final int var2; final requires an initial value to make a variable as constant. public int var3 = 100; private int var4 = 100; public static final int var5 = 100;

Interface Question 1 Which of the following variable declarations are correct? public interface TestInterface1 { int var1; final int var2; public int var3 = 100; private int var4 = 100; public static final int var5 = 100;

Interface Question 1 Which of the following variable declarations are correct? public interface TestInterface1 { int var1; final int var2; public int var3 = 100; private int var4 = 100; Interface only contains public declarations, even without public keyword. public static final int var5 = 100;

Interface Question 1 Which of the following variable declarations are correct? public interface TestInterface1 { int var1; final int var2; public int var3 = 100; private int var4 = 100; public static final int var5 = 100;

Interface Question 2 What s wrong with the following interface? public interface TestInterface2 { void amethod(int avalue) { System.out.println("Hello World!");

Interface Question 2 What s wrong with the following interface? public interface TestInterface2 { void amethod(int avalue) { System.out.println("Hello World!"); Interface contains method signatures, not implementations.

Interface Question 2 How to fix it? public interface TestInterface2 { A B C D E void amethodfix0(int avalue); protected amethodfix1(int avalue); abstract void amethodfix2(int avalue); static void amethodfix3(int avalue) { System.out.println("Hello World!"); default void amethodfix4(int avalue) { System.out.println("Hello World!");

Interface Question 2 How to fix it? public interface TestInterface2 { void amethodfix0(int avalue); protected amethodfix1(int avalue); abstract void amethodfix2(int avalue); static void amethodfix3(int avalue) { System.out.println("Hello World!"); default void amethodfix4(int avalue) { System.out.println("Hello World!");

Interface Question 2 How to fix it? public interface TestInterface2 { Interface only contains public methods, even without the public keyword. void amethodfix0(int avalue); protected amethodfix1(int avalue); abstract void amethodfix2(int avalue); static void amethodfix3(int avalue) { System.out.println("Hello World!"); default void amethodfix4(int avalue) { System.out.println("Hello World!");

Interface Question 2 How to fix it? public interface TestInterface2 { void amethodfix0(int avalue); protected amethodfix1(int avalue); abstract void amethodfix2(int avalue); static void amethodfix3(int avalue) { System.out.println("Hello World!"); default void amethodfix4(int avalue) { System.out.println("Hello World!");

Interface Question 2 How to fix it? public interface TestInterface2 { Start from Java 8, interface can contain static and default method bodies. void amethodfix0(int avalue); protected amethodfix1(int avalue); abstract void amethodfix2(int avalue); static void amethodfix3(int avalue) { System.out.println("Hello World!"); default void amethodfix4(int avalue) { System.out.println("Hello World!");

Interface Question 3 Is the following interface valid? public interface TestInterface3 { A. Valid B. Invalid

Interface Question 3 Is the following interface valid? public interface TestInterface3 { Empty interface is often used as a class marker. For instance, java.io.serializable java.lang.cloneable

Abstract Class Abstract classes are similar to interfaces. You cannot instantiate them. They may contain a mix of methods declared with or without an implementation. Mark Rothko No. 13 (White, Red on Yellow) Oil and Acrylic on canvas -1958. It was with the utmost reluctance that I found the figure could not serve my purposes. But a time came when none of us could use the figure without mutilating it.

Abstract Class vs Interface Use Abstract Class or Interface? Choose the ones that should use Abstract Class A. You want to share code among several closely related classes. B. You want to take advantage of multiple inheritance of type. C. You expect that classes that implement/extend your have many common methods or fields, or require access modifiers other than public (such as protected and private). D. You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong. E. You want to specify the behavior of a particular data type, but not concerned about who implements its behavior. F. You expect that unrelated classes would implement/extends your.

Abstract Class vs Interface Use Abstract Class or Interface? A. You want to share code among several closely related classes. B. You want to take advantage of multiple inheritance of type. C. You expect that classes that implement/extend your have many common methods or fields, or require access modifiers other than public (such as protected and private). D. You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong. E. You want to specify the behavior of a particular data type, but not concerned about who implements its behavior. F. You expect that unrelated classes would implement/extends your.

Inheritance A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

Inheritance Question 1 Which of the the following are valid class/interface definitions? interface InterfaceA { interface InterfaceB { interface InterfaceC { class ClassA { class ClassB { class ClassC { A. class TC1 implements InterfaceA, InterfaceB { B. class TC2 extends InterfaceA { C. class TC3 extends Class B, ClassC { D. class TC4 extends ClassB implements InterfaceA, InterfaceB { E. class TC5 extends Class B, ClassC implements InterfaceA, InterfaceB { F. class TC6 implements InterfaceA, InterfaceB extends ClassB { G. interface TI1 implements InterfaceA, InterfaceB { H. interface TI2 extends InterfaceA, InterfaceB { I. interface Ti3 extends ClassB implements InterfaceA, InterfaceB {

Inheritance Question 1 Which of the the following are valid class/interface definitions? interface InterfaceA { interface InterfaceB { interface InterfaceC { class ClassA { class ClassB { class ClassC { A. class TC1 implements InterfaceA, InterfaceB { B. class TC2 extends InterfaceA { C. class TC3 extends Class B, ClassC { D. class TC4 extends ClassB implements InterfaceA, InterfaceB { E. class TC5 extends Class B, ClassC implements InterfaceA, InterfaceB { F. class TC6 implements InterfaceA, InterfaceB extends ClassB { G. interface TI1 implements InterfaceA, InterfaceB { H. interface TI2 extends InterfaceA, InterfaceB { I. interface Ti3 extends ClassB implements InterfaceA, InterfaceB {

Inheritance Question 2 Which method overrides a method in the superclass? class ClassParent { public void method1(int i) { public void method2(int i) { public static void method3(int i) { public static void method4(int i) { public void method5(int i) { protected void method6(int i) { class ClassChild extends ClassParent { A. public static void method1(int i) { B. public void method2(int i) { C. public void method3(int i) { D. public static void method4(int i) { E. protected void method5(int i) { F. public void method6(int i) {

Inheritance Question 2 Which method overrides a method in the superclass? Non-static method cannot be override with static. class ClassParent { public void method1(int i) { public void method2(int i) { public static void method3(int i) { public static void method4(int i) { public void method5(int i) { protected void method6(int i) { class ClassChild extends ClassParent { public static void method1(int i) { public void method2(int i) { public void method3(int i) { public static void method4(int i) { protected void method5(int i) { public void method6(int i) {

Inheritance Question 2 Which method overrides a method in the superclass? class ClassParent { public void method1(int i) { public void method2(int i) { public static void method3(int i) { public static void method4(int i) { public void method5(int i) { protected void method6(int i) { class ClassChild extends ClassParent { public static void method1(int i) { public void method2(int i) { public void method3(int i) { public static void method4(int i) { protected void method5(int i) { public void method6(int i) {

Inheritance Question 2 Which method overrides a method in the superclass? class ClassParent { public void method1(int i) { public void method2(int i) { public static void method3(int i) { public static void method4(int i) { Static method cannot be override with public void method5(int i) { non-static. protected void method6(int i) { class ClassChild extends ClassParent { public static void method1(int i) { public void method2(int i) { public void method3(int i) { public static void method4(int i) { protected void method5(int i) { public void method6(int i) {

Inheritance Question 2 Which method overrides a method in the superclass? class ClassParent { public void method1(int i) { public void method2(int i) { public static void method3(int i) { public static void method4(int i) { public void method5(int i) { Static method can hide the parent protected void method6(int i) { static method, not override. class ClassChild extends ClassParent { public static void method1(int i) { public void method2(int i) { public void method3(int i) { public static void method4(int i) { protected void method5(int i) { public void method6(int i) {

Inheritance Question 2 Which method overrides a method in the superclass? class ClassParent { public void method1(int i) { public void method2(int i) { public static void method3(int i) { public static void method4(int i) { public void method5(int i) { protected void method6(int i) { Override cannot reduce the visibility. class ClassChild extends ClassParent { public static void method1(int i) { public void method2(int i) { public void method3(int i) { public static void method4(int i) { protected void method5(int i) { public void method6(int i) {

Inheritance Question 2 Which method overrides a method in the superclass? Override can increase the visibility. class ClassParent { public void method1(int i) { public void method2(int i) { public static void method3(int i) { public static void method4(int i) { public void method5(int i) { protected void method6(int i) { class ClassChild extends ClassParent { public static void method1(int i) { public void method2(int i) { public void method3(int i) { public static void method4(int i) { protected void method5(int i) { public void method6(int i) {

Polymorphism Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class

Polymorphism - Basics class Animal { public void makenoise() { System.out.println("Some sound"); class Dog extends Animal{ public void makenoise() { System.out.println("Bark"); class Cat extends Animal{ public void makenoise() { System.out.println("Meawoo"); What s the output of the following piece of code? Animal a1 = new Cat(); a1.makenoise(); Animal a2 = new Dog(); a2.makenoise();

Polymorphism - Basics class Animal { public void makenoise() { System.out.println("Some sound"); class Dog extends Animal{ public void makenoise() { System.out.println("Bark"); class Cat extends Animal{ public void makenoise() { System.out.println("Meawoo"); What s the output of the following piece of code? Animal a1 = new Cat(); a1.makenoise(); Animal a2 = new Dog(); a2.makenoise(); Meawoo Bark

Polymorphism Tricky Question abstract class A { void test(a a) { System.out.println("You are in A"); class B extends A { void test(b b) { System.out.println("You are in B"); public class TrickyPoly { public static void main(string[] args) { A a1 = new B(); A a2 = new B(); B b1 = new B(); a1.test(a2); b1.test(a2); a1.test(b1); b1.test(b1);