Create a Java project named week10

Similar documents
Create a Java project named week9

Spring 2019 Discussion 4: February 11, 2019

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

First IS-A Relationship: Inheritance

Abstract class & Interface

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

More Relationships Between Classes

INHERITANCE Mrs. K.M. Sanghavi

Week 5-1: ADT Design

Software and Programming 1

What is Inheritance?

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

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

type conversion polymorphism (intro only) Class class

Informatik II (D-ITET) Tutorial 6

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

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

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

Exercise: Singleton 1

Object-Oriented Programming More Inheritance

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Example: Count of Points

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Object-Oriented ProgrammingInheritance & Polymorphism

C08: Inheritance and Polymorphism

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

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Inheritance and Polymorphism

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

Example: Count of Points

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

Informatik II Tutorial 6. Subho Shankar Basu

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

Chapter 14 Abstract Classes and Interfaces

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

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

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

Object-Oriented Programming (Java)

Lecture 6 Introduction to Objects and Classes

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

25. Generic Programming

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

Object-Oriented Programming

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

Java Object Oriented Design. CSC207 Fall 2014

Programming Exercise 14: Inheritance and Polymorphism

CMSC 132: Object-Oriented Programming II. Inheritance

Object-Oriented Programming

Inheritance, polymorphism, interfaces

Object Orientated Programming Details COMP360

C08: Inheritance and Polymorphism

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

Polymorphism. Arizona State University 1

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

Binghamton University. CS-140 Fall Dynamic Types

C# Programming for Developers Course Labs Contents

Object Oriented Programming

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

STATIC, ABSTRACT, AND INTERFACE

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

Distributed Systems Recitation 1. Tamim Jabban

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

Type Hierarchy. Lecture 6: OOP, autumn 2003

Distributed Systems Recitation 1. Tamim Jabban

Answer1. Features of Java

Java Magistère BFA

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

CSC207 Week 3. Larry Zhang

CS-202 Introduction to Object Oriented Programming

Subtype Polymorphism

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

Classes and Inheritance Extending Classes, Chapter 5.2

Introduction to Object-Oriented Programming

An example from Zoology

CS 61B Discussion 4: Inheritance Fall 2018

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

Introduction to Inheritance

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Instance Members and Static Members

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Static, Final & Memory Management

COMP 110/L Lecture 19. Kyle Dewey

Inheritance and Testing Spring 2018 Exam Prep 4: February 11, 2019

CMSC131. Objects: Designing Classes and Creating Instances

Software Development (cs2500)

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Practice for Chapter 11

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Rules and syntax for inheritance. The boring stuff

CMSC 202. Generics II

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

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

EXAMINATION FOR THE DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 2

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

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Transcription:

Objectives of today s lab: Through this lab, students will examine how casting works in Java and learn about Abstract Class and in Java with examples. Create a Java project named week10

Create a package named basicoop3 in the project week10 Exercise 1: Upcasting and Downcasting Upcasting: casting a subtype to a supertype, upward to the inheritance tree. Downcasting: casting from base class to child class. Task 1.1: Write the following program, save, compile, and run it. /* file name should be TestInheritance.java in the basicoop3 package*/ class Animal{ System.out.println("Animal Eating!"); class Cat extends Animal{ public void meow (){ System.out.println("Meowing!"); System.out.println("Cat Eating!"); class Dog extends Animal{ public void bark (){ System.out.println("Barking!"); System.out.println("Dog Eating!"); public class TestInheritance{ public static void main(string args[]){ Animal animal = new Cat(); animal.eat();

Dog dog = new Dog(); animal = dog; animal.eat(); If you can successfully run your program, you should see the following output: Cat Eating! Dog Eating! Observation: First a Cat instance is created. Second, the Cat instance is assigned to a variable of type Animal. Now the Animal variable (reference) points to the Cat instance. This is possible because the Cat class inherits from the Animal class. Same things happen for the Dog class. As you can see, it is possible to use an instance of some subclass as if it were an instance of its base class. This way, we don't need to know exactly what subclass the object is an instance of. We could treat e.g., both Cat and Dog instances as Animal instances.

Task 1.2: Comment out the eat() method in the Animal class. Then save, compile, and run the code. class Animal{ /* System.out.println("Animal Eating!"); */ Does your code work correctly? If not, why? Now Un comment the eat() method in the Animal class. Then, invoke the meow()method before creating the Dog object. animal.eat(); animal.meow(); Dog dog = new Dog(); Does your code work correctly? If not, why? These are common mistakes: After casting, you will have access only to the current reference type class members even your object is from type Cat and your reference of type Animal. Any unique class member in class Cat will not be visible to you.

Task 1.3: Now create the following object in the last working code. Cat cat = new Animal(); Does your code work correctly? If not, why? Here, an Animal instance is created. Second, the Animal instance is assigned to a variable of type Cat. By definition Cat is-a Animal, but can you say Animal is-a Cat?

Task 1.4: The downcasting is the casting from base class to child class and is not automatic. Write the following code in the main() method, save, compile, and run the program. Animal animal = new Cat(); animal.eat(); /*down casting manually */ Cat cat = (Cat) animal; cat.eat(); If you can successfully run your program, you should see the following output: Cat Eating! Cat Eating! Now you remove (Cat) from the following Cat cat = (Cat) animal; So the statement becomes Cat cat = animal; Does your code work correctly? If not, why?..

Exercise 2: Abstract class Basic concepts: 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. If you don t implement an abstract method (declared in the abstract class) in the sub-class, then the sub-class becomes abstract too. Task 2.1: Write the following code, save, compile, and run it. /* file name should be TestAbstract.java in the basicoop3 package*/ abstract class Animal{ System.out.println("Animal Eating!"); class Cat extends Animal{ public void meow (){ System.out.println("Meowing!"); System.out.println("Cat Eating!"); class Dog extends Animal{ public void bark (){ System.out.println("Barking!");

System.out.println("Dog Eating!"); public class TestAbstract{ public static void main(string args[]){ Cat cat = new Cat(); cat.eat(); Dog dog = new Dog(); dog.eat(); If you can successfully run your program, you should see the following output: Cat Eating! Dog Eating!

Task 2.2: Create the following object. Animal animal = new Animal(); Does your code work correctly? If not, why? (If you don t know, read again the basic concepts given above) Task 2.3: Modify the eat() method in the Animal class as below (make it abstract). Save, compile, and run the program. public abstract void eat(); If you can successfully run your program, you should see the following output: Cat Eating! Dog Eating! Task 2.4: Comment out the eat() method in the Cat class as below. Save, compile, and run the program. class Cat extends Animal{ public void meow (){ System.out.println("Meowing!"); /* System.out.println("Cat Eating!"); */ Does your code work correctly? If not, why? (If you don t know, read again the basic concepts given above)

Task 2.5: In the following program, insert the missing code to get the expected output (at the end). /* file name should be Student.java in the basicoop3 package*/ abstract class Person { private String name; private String gender; public Person(String name, String gender){ //code missing //abstract method public abstract void study(); @Override public String tostring(){ //code missing public void setname(string name) { this.name = name; public String getname(string name) { return this.name;

public class Student extends Person { private int studentid; { public Student(String name, String gender, int studentid) //code missing @Override public void study() { if(studentid == 0){ System.out.println("The person is Not a Student"); else{ System.out.println("The person is a Student"); public static void main(string args[]){ Person per1 = new Student("Tomas Maul","Male",0); Person per2 = new Student("Sheema","Female",13579); per1.study(); per2.study(); per2.setname("sheema Das"); System.out.println(per2.toString());

Expected output: The person is Not a Student The person is a Student Person's name is: Sheema Das and Gender is: Female

Exercise 3: Java Basic concepts: A Java is just a definition of behaviour, similar to a Java abstract class It is not a class but it is defined in a way similar to the class definition An 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. Task 3.1: Write the following code, save, compile, and run it. /* file name should be Test.java in the basicoop3 package*/ interface Anima{ public abstract void eat(); class Cat implements Animal{ public void meow (){ System.out.println("Meowing!"); @Override System.out.println("Cat Eating!");

class Dog implements Animal{ public void bark (){ System.out.println("Barking!"); @Override System.out.println("Dog Eating!"); public class Test{ public static void main(string args[]){ Cat cat = new Cat(); cat.eat(); Dog dog = new Dog(); dog.eat(); If you can successfully run your program, you should see the following output: Cat Eating! Dog Eating!

Task 3.2: Create a default constructor in the Animal interface interface Anima{ public Animal(){; public abstract void eat(); Does your code work correctly? If not, why? (If you don t know, read again the basic concepts given above) Task 3.3: Now modify the eat() method of the Animal interface Animal{ System.out.println("Animal Eating!"); Does your code work correctly? If not, why? (Same reason as above?) Congratulations! Today, you have learned other important features of the Java language. Now please help your fellow classmates who have been struggling to complete these exercises or you can work on your group coursework.