CIS 183, Fall 2000, Dr. Iren Valova, 1 package Chapter7; public class Circle { private double radius; public Circle(double radius) { radius=radius; } public.

Slides:



Advertisements
Similar presentations
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Advertisements

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Final Review.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Modern Programming Tools And Techniques-I
Chapter 15 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Chapter 9 Inheritance and Polymorphism
Lecture 18: Polymorphism (Part II)
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 13 Abstract Classes and Interfaces Part 01
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

CIS 183, Fall 2000, Dr. Iren Valova, 1 package Chapter7; public class Circle { private double radius; public Circle(double radius) { radius=radius; } public double getRadius() { return radius; } public double findArea() { return radius*radius*Math.PI; } } class Cylinder {private double length; Cylinder(double radius, double length) { Circle(radius); length = length; } public double findArea() { return findArea()*length; }

CIS 183, Fall 2000, Dr. Iren Valova, 2 package IrenWrap; import javax.swing.JOptionPane; public class Parse { public static void main(String[] args) { String one,two; int num1,num2,sum; JOptionPane.showMessageDialog(null,"Welcome to this \n Program"); one=JOptionPane.showInputDialog("Enter the first number\n"); num1=Integer.parseInt(one); two=JOptionPane.showInputDialog("Enter the second number\n"); num2=Integer.parseInt(two); sum=num1+num2; JOptionPane.showMessageDialog(null,"The sum is "+sum,"Results form summation", JOptionPane.PLAIN_MESSAGE); System.exit(0); }

CIS 183, Fall 2000, Dr. Iren Valova, 3 Integer i = new Integer("23"); Integer i = new Integer(23); Integer i = Integer.parseInt("23",8); Double d = new Double(); Double d = Double.valueOf("23.45"); int i = (Integer.valueOf("23")).intValue(); double d = (Double.valueOf("23.4")).doubleValue(); int i = (Double.valueOf("23.4")).intValue(); String s = (Double.valueOf("23.4")).toString();

CIS 183, Fall 2000, Dr. Iren Valova, 4 class Circle { private double radius; public Circle(double radius) { radius = radius; } a.The program will not compile because it does not have a main method. b.The program will compile, but you cannot create an object of Circle with specified radius. c.The program has a compilation error because you cannot assign radius to radius.

CIS 183, Fall 2000, Dr. Iren Valova, 5 class Cylinder extends Circle { double length = 1; Cylinder(double radius) { Circle(radius); } a.The program compiles fine, but you cannot create an instance of Cylinder because the constructor does not specify the length of the cylinder. b.The program has a syntax error because you attempted to invoke the Circle class's constructor illegally. c.The program compiles fine, but it has a runtime error because of invoking the Circle class's constructor illegally.

CIS 183, Fall 2000, Dr. Iren Valova, 6 Cylinder cy = new Cylinder(1,1); Circle c = cy; a.The code has a syntax error. (not dependent) b.The code has a runtime error. c.The code is fine. (dependent) Circle c = new Circle (5); Cylinder c = cy; a.The code has a syntax error. b.The code has a runtime error. c.The code is fine.

CIS 183, Fall 2000, Dr. Iren Valova, 7 package IrenData; public class Test extends R //class Test extends R { public static void main(String[] args) {Test t=new Test(); //R k=new R("Hello"); //k.print(); t.print(); }} class R { public String s; /* public R(String s) { this.s=s; } */ public R() { this.s=s; } public void print() { System.out.println(s); }}

CIS 183, Fall 2000, Dr. Iren Valova, 8 class Test extends A { public static void main(String[] args) {Test t = new Test(); t.print();} } class A { String s; A(String s) { this.s = s; } public void print() { System.out.println(s); } a.The program does not compile because Test does not have a constructor Test(). b.The program would compile if the constructor in the class A were removed. c.The program compiles, but it has a runtime error due to the conflict on the method name print. d.The program runs just fine.

CIS 183, Fall 2000, Dr. Iren Valova, 9 class C1 {}; class C2 extends C1 {}; class C3 extends C1 {}; C2 c2 = new C2(); C3 c3 = new C3(); Analyze the following statement: c2 = (C2)((C1)c3); a.c3 is cast into c2 successfully. b.You will get a compilation error because you cannot cast objects from sibling classes. c.You will get a runtime error because the Java runtime system cannot perform multiple casting in nested form. d.The statement is correct.

CIS 183, Fall 2000, Dr. Iren Valova, 10 class C1 {} class C2 extends C1 { } class C3 extends C2 { } class C4 extends C1 {} C1 c1 = new C1(); C2 c2 = new C2(); C3 c3 = new C3(); C4 c4 = new C4(); Which of the following expressions evaluates to false? a.c1 instanceof C1 b.c2 instanceof C1 c.c3 instanceof C1 d.c4 instanceof C2

CIS 183, Fall 2000, Dr. Iren Valova, 11 Which of the following statements will not convert a string s into an integer? a.i = Integer.parseInt(s); b.i = (new Integer(s)).intValue(); c.i = Integer.valueOf(s).intValue(); d.i = Integer.valueOf(s); Which of the following statements will not convert a string s into a double value d? a.d = Double.parseDouble(s); b.d = (new Double(s)).doubleValue(); c.d = Double.valueOf(s).doubleValue(); d.All of the above.

CIS 183, Fall 2000, Dr. Iren Valova, 12 Which of the following statements convert a double value d into a string s? a.s = (new Double(d)).toString(); b.s = (Double.valueOf(s)).toString(); c.s = new Double(d).stringOf(); d.s = String.stringOf(d); e.a and b

CIS 183, Fall 2000, Dr. Iren Valova, 13 interface A { void print(); } class C {} class B extends C implements A { public void print() { } } class Test extends Thread { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b is an instance of A"); if (b instanceof C) System.out.println("b is an instance of C"); } a.Nothing. b.b is an instance of A. c.b is an instance of C. d.b is an instance of A followed by b is an instance of C.

CIS 183, Fall 2000, Dr. Iren Valova, 14 When you implement a method that is defined in a superclass, you __________ the original method. a. overload b. override c.copy d. call What modifier should you use on a class so that a class in the same package can access it but a class in a different package cannot access it? a.public b.private c.protected d.Use the default modifier. What modifier should you use so that a class in a different package cannot access the class, but its subclasses in any package can access it? a.public b.private c.protected d.Use the default modifier.

CIS 183, Fall 2000, Dr. Iren Valova, 15 class Tag{ Tag(int marker){ System.out.println("Tag("+marker+")"); } class Card{ Tag t1=new Tag(6); Card(){ System.out.println("Card()"); Tag t3=new Tag(33); } Tag t2=new Tag(3); void f(){ System.out.println("f()"); } Tag t3=new Tag(9); } public class Order{ public static void main(String[] args) { Card t = new Card(); t.f(); }

CIS 183, Fall 2000, Dr. Iren Valova, 16  A final class can have instances.  You cannot create an instance of an abstract class using the new operator.  A final class can be extended.  An abstract class can be extended.  You can always successfully cast a subclass to a superclass.  You can always successfully cast a superclass to a subclass.  An interface can be a separate unit and can be compiled into a bytecode file.  The order in which modifiers appear before a class or a method is important.  Every class has a toString() method and an equals() method.  The instanceof operator is used to determine whether an object is an instance of a certain class.