1. Class Relationships
|
|
- Chloe Moody
- 5 years ago
- Views:
Transcription
1 1. Class Relationships See also: Class declarations define new reference types and describe how they are implemented. Constructors are similar to methods, but cannot be invoked directly by a method call; they are used to initialize new class instances. Like methods, they may be overloaded. Static initializers are blocks of executable code hat may be used to help initialize a class when it is first loaded. The body of a class declares members, static initializers, and constructors. The scope of the name of a member is the entire declaration of the class to which the member belongs. Field, method, and constructor declarations may include the access modifiers public, protected, or private. The members of a class include both declared and inherited members. Newly declared fields can hide fields declared in a superclass or super interface. Newly declared methods can hide, implement, or override methods declared in a superclass. visibility modifier: public/protected/private Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N N Return type: void/primitive type/reference to a object Class methods/class variables are declared with static. Static declaration inside a method change the lifetime of a variable.
2 Example 1 public class X { 2 static X ax = null; // use before define 3 static int ci = 0; 4 int oi = 1; 5 6 public X() { 7 } 8 static public void cm(int i){ 9 ci = i; 10 System.out.println("cm: " + ci ); 11 } 12 public void om(int i){ 13 ci = i; 14 oi = i; 15 System.out.println("om: " + this); 16 System.out.println(" ci " + ci); 17 System.out.println(" oi " + oi); 18 } public static void main(string args[] ) { 21 X ax = new X(); 22 X aax = new X(); ax.cm(1); 25 aax.cm(2); ax.om(3); 28 aax.om(4); 29 } 30 } Source Code: Src/5/X.java % java X cm: 1 cm: 2 om: X@1ad086a ci 3 oi 3 om: X@10385c1 ci 4 oi Class Details 1.3. Static in Classes/Methods Lifetime Class Variables: If a variable is declared static, there exists exactly one incarnation of the field, Static Methods: A method that is declared static is called a class method. A class method is always invoked without reference to a particular object.
3 -3- Non Static Methods: A method that is not declared static is called an instance method, and sometimes called a non-static method. An instance method is always invoked with respect to an object, which becomes the current object to which the keywords this and super refer during execution of the method body. Variables can be declared: static: class variable final: can be assigned once or zero time transient: not stored or saved via the standard serialization process volatile: A variable may be modified by multiple threads. This gives a hint to the compiler to fetch the value each time, rather store a locale copy. This also prohibits same optimization procedures. See also: 1 2 public class Overview { 3 int instancevariable; 4 static int classvariable; 5 final int finalvariable; // static? 6 volatile int volatilevariable; 7 transient int transientvariable; public Overview() { 11 finalvariable = 42; 12 } 13 public Overview(int alocalvariable) { 14 finalvariable = 43; 15 } 16 void instancemethod() { 17 finalvariable = 43; 18 instancevariable = 22; 19 classvariable = 33; 20 } 21 static void classmethod() { 22 classvariable = 3; 23 } public static void main(string args[] ) { 26 Overview aoverview = new Overview(); 27 Overview boverview = new Overview(); 28 Overview coverview = new Overview(1); 29 coverview = boverview; 30 aoverview.instancemethod(); 31 instancemethod(); 32 boverview.classmethod(); 33 // values of aoverview.? boverview.? 34 // aoverview.finalvariable?? } 37 } Source Code: Src/5/Overview.java
4 -4- how many instances of the variables to exist? How many objects do exist?
5 Parameter Passing The formal parameters of a method, if any, are specified by a list of comma-separated parameter specifiers. Each parameter specifier consists of a type and an identifier (optionally followed by brackets) that specifies the name of the parameter. If a method has no parameters, only an empty pair of parentheses appears in the method s declaration. If two formal parameters are declared to have the same name (that is, their declarations mention the same identifier), then a compile-time error occurs. When the method is invoked, the values of the actual argument expressions initialize newly created parameter variables, each of the declared type, before execution of the body of the method. The scope of formal parameter names is the entire body of the method. These parameter names may not be redeclared as local variables or exception parameters within the method; that is, hiding the name of a parameter is not permitted. call by value
6 Example I 1 public class ExampleClass { 2 int alocalvariable = 3; 3 4 public ExampleClass() { 5 alocalvariable = 2; 6 } 7 public ExampleClass(int alocalvariable) { 8 this.alocalvariable = alocalvariable; 9 alocalvariable = 6; 10 } public static void main(string args[] ) { 13 ExampleClass aexampleclass = new ExampleClass(); 14 aexampleclass = new ExampleClass(3); 15 System.out.println("the value is: " + aexampleclass.alocalvariable); 16 System.out.println(aLocalVariable); 17 } 18 } Source Code: Src/5/ExampleClass.java Questions: How does the the JVM find the main method, when you execute java ExampleClass? Describe the execution order of the constructors. Which variables will be modified when? 1.6. Example II 1 import java.util.vector; 2 3 public class Args { 4 5 String astring; 6 Vector avector = new Vector(); public void teststring(string arg ) { 10 arg = "b"; 11 } 12 public void testvector(vector arg ) { 13 arg.add("b"); 14 } public void test1() { 17 astring = "a"; 18 System.out.println("1. " + astring); 19 teststring(astring); 20 System.out.println("2. " + astring); 21 } 22 public void test2() {
7 -7-23 avector.add("a"); 24 System.out.println("3. " + avector); 25 testvector(avector); 26 System.out.println("4. " + avector); 27 } 28 public static void main(string args[] ) { 29 new Args().test1(); 30 new Args().test2(); 31 } 32 } Source Code: Src/5/Args.java Questions: How does the the JVM find the the variable astaticlocalvariable? Which variables will be modified when?
8 Example III 1 public class ExampleClassIII { 2 3 String astring = null; 4 5 public void method(string a) { 6 a = new String("set in method"); 7 System.out.println("2. method:a:" + a ); 8 } 9 public void test() { 10 String astring = new String("set in test"); System.out.println("1. test:astring:" + astring ); 13 method(astring); 14 System.out.println("3. test:astring:" + astring ); 15 } 16 public static void main(string args[] ) { 17 new ExampleClassIII().test(); 18 } 19 } Source Code: Src/5/ExampleClassIII.java 1.8. Example IV 1 2 public class XX { 3 int instancev = 1; 4 5 static XX b; 6 static XX a; 7 8 public XX() { 9 } 10 public void method(int i){ 11 instancev = i; 12 } public String tostring() { 15 return "instancev = " + instancev; 16 } public void m2(int i){ 19 a.method(-9); 20 method(12); 21 System.out.println(" "); 22 System.out.println("print itself : " + this); 23 System.out.println("print a: " + a); 24 System.out.println("========================================="); 25 } 26
9 -9-27 public static void main(string args[] ) { 28 b = new XX(); 29 a = new XX(); b.m2(3); 32 a.m2(24); 33 } 34 } Source Code: Src/5/XX.java does it compile? Output? print itself : instancev = 12 print aaaaaa: instancev = -9 ========================================= print itself : instancev = 12 print aaaaaa: instancev = 12 ========================================= 1 2 public class ClassXX { 3 static int instancev = 1; 4 5 static ClassXX bbbbb; 6 static ClassXX aaaaaa; 7 8 public ClassXX() { 9 } 10 public void method(int i){ 11 instancev = i; 12 } public String tostring() { 15 return "instancev = " + instancev; 16 } public void m2(int i){ 19 aaaaaa.method(-9); 20 method(12); 21 System.out.println(" "); 22 System.out.println("print itself : " + this); 23 System.out.println("print aaaaaa: " + aaaaaa); 24 System.out.println("========================================="); 25 } public static void main(string args[] ) { 28 bbbbb = new ClassXX(); 29 aaaaaa = new ClassXX(); bbbbb.m2(3);
10 aaaaaa.m2(24); 33 } 34 } Source Code: Src/5/ClassXX.java 1.9. Example VI 1 2 public class XXX { 3 int oi = 1; 4 5 XXX axxx = new XXX();; 6 7 public XXX() { 8 } 9 public static void main(string args[] ) { 10 XXX aaxxx = new XXX(); 11 } 12 } Source Code: Src/5/XXX.java does it compile? ja. Draw the memory pic during execution Does it execute? Ja, aber es wird sterben, weil im C. ein wort fehlt Output? A Point Class Anexample without any comment: I. Use of a Point Class: 1 /** 2 * This class implements a point test program. 3 * 4 $Id$ 5 * 6 hp bischof 7 * 8 * Revisions: 9 * $Log$ 10 */ public class TestPoint { 15 private static Point apoint; /** 19 * The main program.
11 * 21 args command line arguments (ignored) 22 */ 23 public static void main(string args[]) 24 { 25 System.out.println("Point.soManyPoints = " + Point.soManyPoints() ); 26 apoint = new Point(2, 3); 27 System.out.println("x = " + apoint.getx() ); 28 System.out.println("y = " + apoint.gety() ); apoint = new Point(); 31 apoint.initpoint(4, 5); 32 System.out.println("x = " + apoint.getx() ); 33 System.out.println("y = " + apoint.gety() ); apoint.move(6, 7); 36 System.out.println("x = " + apoint.getx() ); 37 System.out.println("y = " + apoint.gety() ); System.out.println("nPoints = " + apoint.getnpoints() ); 40 System.out.println("aPoint.soManyPoints = " + apoint.somanypoints() ); 41 } 42 } Source Code: Src/5/TestPoint.java
12 -12- II. The Point Class: 1 /** 2 * This class implements a point in a two dimensional 3 * area. 4 * All method print when they are called. 5 * 6 $Id$ 7 * 8 hp bischof 9 * 10 * Revisions: 11 * $Log$ 12 */ public class Point { 15 // class variable 16 static int npoints; // so many points were created private int x; // x coordinate of the point 19 private int y; // y cooridnate of the point /** 22 * Default Constructor. 23 * Increases the counter npoints by * 25 Point a Point object 26 */ 27 public Point(){ 28 super(); 29 System.out.println(" in Point() constructor"); 30 npoints ++; 31 } /** 34 * Constructor. 35 * initialize x and y values of a point 36 * 37 x x coordinate 38 y y coordinate 39 * 40 Point a Point object 41 */ 42 public Point(int x, int y){ 43 super(); 44 int i ++; 45 this.x = x; 46 this.y = y; 47 System.out.println(" in Point(int, int) constructor"); 48 } /** 51 * So many points have been created.
13 * 53 int So many points have been created 54 */ 55 public static int somanypoints(){ 56 return npoints; 57 } /** 60 * initialzes x and y of a point. 61 * 62 x int x coordinate 63 y int y coordinate 64 * 65 Point a Point object 66 */ 67 public Point initpoint(int x, int y){ 68 System.out.println(" in initpoint(int, int)"); this.x = x; 71 this.y = y; return this; 74 } /** 77 * move a point 78 * 79 x int delta x value 80 y int delta y value 81 * 82 Point a Point object 83 */ 84 public Point move(int x, int y){ 85 System.out.println(" in move(int, int)"); this.x += x; 88 this.y += y; return this; 91 } /** 94 * Returns the x coordinate of a point 95 * 96 int x value 97 */ 98 public int getx(){ 99 System.out.println(" in getx()"); 00 return this.x; 01 } /** 04 * Returns the y coordinate of a point 05 *
14 int x value 07 */ 08 public int gety(){ 09 System.out.println(" in gety()"); 10 return this.y; 11 } /** 14 * Returns how many points are created so far. 15 * 16 int npoints 17 */ 18 public int getnpoints(){ 19 System.out.println(" in getnpoints()"); 20 return this.npoints; 21 } 22 } Source Code: Src/5/Point.java II. Execution of the test program: Point.soManyPoints = 0 in Point() constructor in Point(int, int) constructor in getx() x = 2 in gety() y = 3 in Point() constructor in initpoint(int, int) in getx() x = 4 in gety() y = 5 in move(int, int) in getx() x = 10 in gety() y = 12 in getnpoints() npoints = 2 apoint.somanypoints = 2 Youmay find the javadoc pages
15 Additional Examples See 1 public class Scope_1 { 2 3 String astring = null; 4 5 public void method(string astring) { 6 this.astring = new String("set in method"); 7 System.out.println("2. method:astring:" + this.astring ); 8 } 9 public void test() { 10 String astring = new String("set in test"); System.out.println("1. test:astring:" + astring ); 13 method(astring); // is there a way that "set in method" 14 System.out.println("3. test:astring:" + astring ); 15 } 16 public static void main(string args[] ) { 17 new Scope_1().test(); 18 } 19 } Source Code: Src/5/Scope_1.java 1 // see example 2 public class Scope_2 { 3 4 String astring = null; 5 6 public void test() { 7 String astring = new String("set in test"); 8 //if ( true ) 9 { 10 String astring = new String("set in test"); 11 } 12 } 13 public static void main(string args[] ) { 14 new Scope_2().test(); 15 } 16 } Source Code: Src/5/Scope_2.java 1 // see example 2 public class Scope_3 { 3 4 String astring = null; 5 6 public void test() { 7 int i;
16 class Test { 10 int i; 11 } for (int index = 0; index < 10; index ++ ) { 14 System.out.println("index = " + index ); 15 } 16 } 17 public static void main(string args[] ) { 18 new Scope_3().test(); 19 } 20 } Source Code: Src/5/Scope_3.java 1 // see example 2 public class Scope_4 { 3 4 String astring = null; 5 6 public static void test_2() { 7 int k = 0; 8 for (int index = 0; index < 10 ; index ++ ) { 9 int k = 3; 10 } 11 } 12 public static void test() { 13 int i; 14 int k = 0; 15 switch (k) { 16 case 1: { 17 int i=1; 18 System.out.println("1: i == " + i); 19 } 20 break; 21 default: 22 System.out.println("something went wrong!"); 23 break; 24 } 25 } public static void main(string args[] ) { 28 test(); 29 } 30 } Source Code: Src/5/Scope_4.java Question arrived 22:44 Newly declared fields can hide fields declared in a superclass or super interface. Newly declared
17 -17- methods can hide, implement, or override methods declared in a superclass. Would hiding just mean that new declared things would be accessed first, because we still can access the superclass members by means of (super.field) and/or (super.method(arg))? Static declaration inside a method change the lifetime of a variable. Can static variables be declared inside of a method? How does the the JVM find the variable astaticlocalvariable?(question from the second link) System.out.println("om: " + this); What kind of address is this? -> om: X@1ad086a Is it the value of the reference to this object? Is it some virtual address provided by the JVM? What are transient and volatile variables? When are they used? If I assign a value to the final variable at the time of declaration but I don t declare it to be static, will still there be copies of this variable with the objects or this variable will behave like astatic final? This program is confusing for me. I am not able to comprehend the concept that it introduces. Some confusions regarding scopes. void func(){ int index=2; for (int index=0; index<5; index++) {//some code} } It doesn t compile saying that the variable "index" is already defined. void func1(){ for(int index=0; index<5; index++){//some code} } System.out.println(index); // Does not compile saying that the variable "index" cannot Similar thing happens in switch case. I also read somewhere that hiding of parameter inside the method body is illegal. What happens in the case of inner classes? s.findinline("(\d+) fish (\d+) fish (\w+) fish (\w+)"); Which of the following method executes it? String findinline(pattern pattern) OR String findinline(string pattern) Is it a regular expression? If yes, why isthe argument using the delimiter to be " fish ". It should have taken the entire thing as one. How does the auto-boxing actually proceeds in the following program from this link:- public class SimpleBoxingI { public static void main( String[] args ) { Integer n1 = new Integer( 42 ); int i1 = n1; int i2 = 31; Integer n2 = i1 + i2 + n1; // are all of them boxed? System.out.println(n1 + " " + n2 + " " + i1 + " " + i2 ); }} One more question regarding boxing:-
18 -18- class StringToInt { public static void main(string args[]) { int i; Integer aint = new Integer("4"); i=aint; //Will the difference between the line above and the line below be auto unboxing and u i = aint.intvalue(); i = Integer.parseInt("4"); } } public class SimpleBoxingTypes { public static void main( String[] args ) { Float f1 = new Float( 42F ); Integer i1 = new Integer( 42 ); Double d1 = new Double( 42.0 ); } } double f = f1 + (double)i1 + (double)d1; // float ff = f1 + (float)i1 + ((float)d1); System.out.println(f); Boxing - primitive to Object type Unboxing - Object to primitive type What is the concept I am missing in this example? I see only type casting. Secondly, why does the commented float line doesn t compile? Why does it start compiling when I use the primitive types instead of object types? Why do the boxing and unboxing things don t help? Does this demonstrate some type of a difference between the two types? I see a break statement at the end of the default case everywhere? Does anything significantly change if I don t provide it. Each parameter specifier consists of a type and an identifier (optionally followed by brackets) that specifies the name of the parameter. Where can we use brackets?
3.1 Class Declaration
Chapter 3 Classes and Objects OBJECTIVES To be able to declare classes To understand object references To understand the mechanism of parameter passing To be able to use static member and instance member
More informationClass, Variable, Constructor, Object, Method Questions
Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx
More informationMore Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.
More Java Basics 1. INHERITANCE AND DYNAMIC TYPE-CASTING Java performs automatic type conversion from a sub-type to a super-type. That is, if a method requires a parameter of type A, we can call the method
More informationComputer Science II (20073) Week 1: Review and Inheritance
Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system
More informationAP Computer Science Chapter 10 Implementing and Using Classes Study Guide
AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within
More informationPractice for Chapter 11
Practice for Chapter 11 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Object-oriented programming allows you to derive new classes from existing
More informationObject-Oriented Concepts
JAC444 - Lecture 3 Object-Oriented Concepts Segment 2 Inheritance 1 Classes Segment 2 Inheritance In this segment you will be learning about: Inheritance Overriding Final Methods and Classes Implementing
More informationLanguage Features. 1. The primitive types int, double, and boolean are part of the AP
Language Features 1. The primitive types int, double, and boolean are part of the AP short, long, byte, char, and float are not in the subset. In particular, students need not be aware that strings are
More informationJava Primer 1: Types, Classes and Operators
Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,
More informationAdministrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments
CMSC433, Spring 2004 Programming Language Technology and Paradigms Java Review Jeff Foster Feburary 3, 2004 Administrivia Reading: Liskov, ch 4, optional Eckel, ch 8, 9 Project 1 posted Part 2 was revised
More informationClasses. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.
Classes Introduce to classes and objects in Java. Classes and Objects in Java Understand how some of the OO concepts learnt so far are supported in Java. Understand important features in Java classes.
More informationDeclarations and Access Control SCJP tips
Declarations and Access Control www.techfaq360.com SCJP tips Write code that declares, constructs, and initializes arrays of any base type using any of the permitted forms both for declaration and for
More informationOOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.
OOPs Concepts 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. Type Casting Let us discuss them in detail: 1. Data Hiding: Every
More informationGraphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub
Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java
More informationComputer Science II (20082) Week 1: Review and Inheritance
Computer Science II 4003-232-08 (20082) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Syntax and Semantics of Formal (e.g. Programming) Languages Syntax
More informationQUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer
QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer STUDENT NAME: ID: The exam consists of five questions. There are a total of 10 points. You may use the back
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationAgenda. Objects and classes Encapsulation and information hiding Documentation Packages
Preliminaries II 1 Agenda Objects and classes Encapsulation and information hiding Documentation Packages Inheritance Polymorphism Implementation of inheritance in Java Abstract classes Interfaces Generics
More informationLesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014
Lesson 10A OOP Fundamentals By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Definition Pointers vs containers Object vs primitives Constructors Methods Object class
More informationInheritance & Polymorphism Recap. Inheritance & Polymorphism 1
Inheritance & Polymorphism Recap Inheritance & Polymorphism 1 Introduction! Besides composition, another form of reuse is inheritance.! With inheritance, an object can inherit behavior from another object,
More informationCS260 Intro to Java & Android 03.Java Language Basics
03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance
More informationBinghamton University. CS-140 Fall Problem Solving. Creating a class from scratch
Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods
More informationObject-Oriented Programming (Java)
Object-Oriented Programming (Java) Topics Covered Today 2.1 Implementing Classes 2.1.1 Defining Classes 2.1.2 Inheritance 2.1.3 Method equals and Method tostring 2 Define Classes class classname extends
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques (CIS120) Lecture 26 March 26, 2015 Inheritance and Dynamic Dispatch Chapter 24 public interface Displaceable { public int getx(); public int gety(); public void move
More informationChapter 6 Introduction to Defining Classes
Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract
More informationCHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA
1. JIT meaning a. java in time b. just in time c. join in time d. none of above CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA 2. After the compilation of the java source code, which file is created
More informationJava. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)
Summary: Chapters 1 to 10 Sharma Chakravarthy Information Technology Laboratory (IT Lab) Computer Science and Engineering Department The University of Texas at Arlington, Arlington, TX 76019 Email: sharma@cse.uta.edu
More informationCS 61B Discussion 4: Inheritance Fall 2018
CS 61B Discussion 4: Inheritance Fall 2018 1 Creating Cats Given the Animal class, fill in the definition of the Cat class so that it makes a "Meow!" noise when greet() is called. Assume this noise is
More informationDarshan Institute of Engineering & Technology for Diploma Studies Unit 3
Class A class is a template that specifies the attributes and behavior of things or objects. A class is a blueprint or prototype from which objects are created. A class is the implementation of an abstract
More informationInstance Members and Static Members
Instance Members and Static Members You may notice that all the members are declared w/o static. These members belong to some specific object. They are called instance members. This implies that these
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques (CIS120) Lecture 25 November 1, 2017 Inheritance and Dynamic Dispatch (Chapter 24) Announcements HW7: Chat Client Available Soon Due: Tuesday, November 14 th at 11:59pm
More informationClass definition. complete definition. public public class abstract no instance can be created final class cannot be extended
JAVA Classes Class definition complete definition [public] [abstract] [final] class Name [extends Parent] [impelements ListOfInterfaces] {... // class body public public class abstract no instance can
More informationProgramming overview
Programming overview Basic Java A Java program consists of: One or more classes A class contains one or more methods A method contains program statements Each class in a separate file MyClass defined in
More informationJava Object Oriented Design. CSC207 Fall 2014
Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract
More information3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.
3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.
More informationCS 231 Data Structures and Algorithms, Fall 2016
CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard
More informationObject Oriented Programming. Week 1 Part 3 Writing Java with Eclipse and JUnit
Object Oriented Programming Part 3 Writing Java with Eclipse and JUnit Today's Lecture Test Driven Development Review (TDD) Building up a class using TDD Adding a Class using Test Driven Development in
More informationHAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.
HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all
More informationQuestions Answer Key Questions Answer Key Questions Answer Key
Benha University Term: 2 nd (2013/2014) Class: 2 nd Year Students Subject: Object Oriented Programming Faculty of Computers & Informatics Date: 26/4/2014 Time: 1 hours Exam: Mid-Term (A) Name:. Status:
More informationCONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.
Constructor in Java 1. What are CONSTRUCTORs? Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation. It constructs
More informationAgenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java
CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda
More information1 Shyam sir JAVA Notes
1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write
More informationSoftware 1 with Java. Initialization. Initialization. Initialization. Pass by Value. Initialization. Recitation No. 11 (Summary)
Software 1 with Java Recitation No. 11 (Summary) public class Foo { static int bar; public static void main (String args []) { bar += 1; System.out.println("bar = " + bar); The Does output the code is:
More informationChapter 4 Defining Classes I
Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the
More informationCISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.
CISC-124 20180215 These notes are intended to summarize and clarify some of the topics that have been covered recently in class. The posted code samples also have extensive explanations of the material.
More informationCOE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014
1 COE 212 Engineering Programming Welcome to Exam I Tuesday November 11, 2014 Instructors: Dr. Bachir Habib Dr. George Sakr Dr. Joe Tekli Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam
More informationJava Fundamentals (II)
Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class
More informationCS/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/B.TECH/CSE(New)/SEM-5/CS-504D/2013-14 2013 OBJECT ORIENTED PROGRAMMING Time Allotted : 3 Hours Full Marks : 70 The figures in the margin indicate full marks. Candidates are required to give their answers
More informationA Foundation for Programming
2.1 Functions A Foundation for Programming any program you might want to write objects functions and modules build bigger programs and reuse code graphics, sound, and image I/O arrays conditionals and
More informationJava for Non Majors Spring 2018
Java for Non Majors Spring 2018 Final Study Guide The test consists of 1. Multiple choice questions - 15 x 2 = 30 points 2. Given code, find the output - 3 x 5 = 15 points 3. Short answer questions - 3
More informationLesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014
Lesson 10B Class Design By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Encapsulation Inheritance and Composition is a vs has a Polymorphism Information Hiding Public
More informationFinal Exam CS 251, Intermediate Programming December 13, 2017
Final Exam CS 251, Intermediate Programming December 13, 2017 Name: NetID: Answer all questions in the space provided. Write clearly and legibly, you will not get credit for illegible or incomprehensible
More informationOBJECT ORİENTATİON ENCAPSULATİON
OBJECT ORİENTATİON Software development can be seen as a modeling activity. The first step in the software development is the modeling of the problem we are trying to solve and building the conceptual
More informationRules and syntax for inheritance. The boring stuff
Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for
More informationProgramming Languages and Techniques (CIS120)
Programming Languages and Techniques (CIS120) Lecture 26 March 23, 2016 Inheritance and Dynamic Dispatch Chapter 24 Inheritance Example public class { private int x; public () { x = 0; } public void incby(int
More informationCS121/IS223. Object Reference Variables. Dr Olly Gotel
CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223
More informationCH. 2 OBJECT-ORIENTED PROGRAMMING
CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED
More informationRepe$$on CSC 121 Fall 2015 Howard Rosenthal
Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while
More informationJava Intro 3. Java Intro 3. Class Libraries and the Java API. Outline
Java Intro 3 9/7/2007 1 Java Intro 3 Outline Java API Packages Access Rules, Class Visibility Strings as Objects Wrapper classes Static Attributes & Methods Hello World details 9/7/2007 2 Class Libraries
More informationCMSC 433 Section 0101 Fall 2012 Midterm Exam #1
Name: CMSC 433 Section 0101 Fall 2012 Midterm Exam #1 Directions: Test is closed book, closed notes. Answer every question; write solutions in spaces provided. Use backs of pages for scratch work. Good
More informationCOP 3330 Final Exam Review
COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time
More informationWeiss Chapter 1 terminology (parenthesized numbers are page numbers)
Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement
More informationJava How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.
Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;
More informationCS 251 Intermediate Programming Methods and Classes
CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with
More informationCS 251 Intermediate Programming Methods and More
CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with
More informationChapter 1: Introduction to Computers, Programs, and Java
Chapter 1: Introduction to Computers, Programs, and Java 1. Q: When you compile your program, you receive an error as follows: 2. 3. %javac Welcome.java 4. javac not found 5. 6. What is wrong? 7. A: Two
More informationRepe$$on CSC 121 Spring 2017 Howard Rosenthal
Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when
More informationFunctions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)
2.1 Functions Functions Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing) Return one output value Input Arguments x y z f Return Value f (x, y, z)
More informationJava Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding
Java Class Design Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua Objectives Implement encapsulation Implement inheritance
More informationExam Duration: 2hrs and 30min Software Design
Exam Duration: 2hrs and 30min. 433-254 Software Design Section A Multiple Choice (This sample paper has less questions than the exam paper The exam paper will have 25 Multiple Choice questions.) 1. Which
More informationObjects and Classes: Working with the State and Behavior of Objects
Objects and Classes: Working with the State and Behavior of Objects 1 The Core Object-Oriented Programming Concepts CLASS TYPE FACTORY OBJECT DATA IDENTIFIER Classes contain data members types of variables
More informationContainer Vs. Definition Classes. Container Class
Overview Abstraction Defining new classes Instance variables Constructors Defining methods and passing parameters Method/constructor overloading Encapsulation Visibility modifiers Static members 14 November
More informationBirkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions
Birkbeck (University of London) Software and Programming 1 In-class Test 2.1 16 Mar 2017 Student Name Student Number Answer ALL Questions 1. What output is produced when the following Java program fragment
More informationInheritance and Polymorphism
Inheritance and Polymorphism Dr. M. G. Abbas Malik Assistant Professor Faculty of Computing and IT (North Jeddah Branch) King Abdulaziz University, Jeddah, KSA mgmalik@kau.edu.sa www.sanlp.org/malik/cpit305/ap.html
More informationQuestions Answer Key Questions Answer Key Questions Answer Key
Benha University Term: 2 nd (2013/2014) Class: 2 nd Year Students Subject: Object Oriented Programming Faculty of Computers & Informatics Date: 26/4/2014 Time: 1 hours Exam: Mid-Term (C) Name:. Status:
More informationCOMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others
COMP-202 Objects, Part III Lecture Outline Static Member Variables Parameter Passing Scopes Encapsulation Overloaded Methods Foundations of Object-Orientation 2 Static Member Variables So far, member variables
More informationObject Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.
CMSC 131: Chapter 28 Final Review: What you learned this semester The Big Picture Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. Java
More information1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides
1B1b Inheritance Agenda Introduction to inheritance. How Java supports inheritance. Inheritance is a key feature of object-oriented oriented programming. 1 2 Inheritance Models the kind-of or specialisation-of
More informationExample: Count of Points
Example: Count of Points 1 class Point { 2... 3 private static int numofpoints = 0; 4 5 Point() { 6 numofpoints++; 7 } 8 9 Point(int x, int y) { 10 this(); // calling the constructor with no input argument;
More informationDOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS
Chapter 1 : Chapter-wise Java Multiple Choice Questions and Answers Interview MCQs Java Programming questions and answers with explanation for interview, competitive examination and entrance test. Fully
More informationMore on Objects in JAVA TM
More on Objects in JAVA TM Inheritance : Definition: A subclass is a class that extends another class. A subclass inherits state and behavior from all of its ancestors. The term superclass refers to a
More informationNews and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!
True object-oriented programming: Dynamic Objects Reference Variables D0010E Object-Oriented Programming and Design Lecture 3 Static Object-Oriented Programming UML" knows-about Eckel: 30-31, 41-46, 107-111,
More informationProgramming in Java, 2e Sachin Malhotra Saurabh Choudhary
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary Chapter 5 Inheritance Objectives Know the difference between Inheritance and aggregation Understand how inheritance is done in Java Learn polymorphism
More informationLecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.
Lecture 05: Methods AITI Nigeria Summer 2012 University of Lagos. Agenda What a method is Why we use methods How to declare a method The four parts of a method How to use (invoke) a method The purpose
More informationCompuScholar, Inc. 9th - 12th grades
CompuScholar, Inc. Alignment to the College Board AP Computer Science A Standards 9th - 12th grades AP Course Details: Course Title: Grade Level: Standards Link: AP Computer Science A 9th - 12th grades
More informationLecture 3. Lecture
True Object-Oriented programming: Dynamic Objects Static Object-Oriented Programming Reference Variables Eckel: 30-31, 41-46, 107-111, 114-115 Riley: 5.1, 5.2 D0010E Object-Oriented Programming and Design
More informationMIDTERM REVIEW. midterminformation.htm
MIDTERM REVIEW http://pages.cpsc.ucalgary.ca/~tamj/233/exams/ midterminformation.htm 1 REMINDER Midterm Time: 7:00pm - 8:15pm on Friday, Mar 1, 2013 Location: ST 148 Cover everything up to the last lecture
More informationInheritance and Polymorphism
Inheritance and Polymorphism Recitation 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University Project 5 Due Wed, Oct. 22 at 10 pm. All questions on the class newsgroup. Make use of lab
More informationLogistics. Final Exam on Friday at 3pm in CHEM 102
Java Review Logistics Final Exam on Friday at 3pm in CHEM 102 What is a class? A class is primarily a description of objects, or instances, of that class A class contains one or more constructors to create
More informationCSC207H: Software Design. Java + OOP. CSC207 Winter 2018
Java + OOP CSC207 Winter 2018 1 Why OOP? Modularity: code can be written and maintained separately, and easily passed around the system Information-hiding: internal representation hidden from the outside
More informationLecture 2: Java & Javadoc
Lecture 2: Java & Javadoc CS 62 Fall 2018 Alexandra Papoutsaki & William Devanny 1 Instance Variables or member variables or fields Declared in a class, but outside of any method, constructor or block
More informationCompaq Interview Questions And Answers
Part A: Q1. What are the difference between java and C++? Java adopts byte code whereas C++ does not C++ supports destructor whereas java does not support. Multiple inheritance possible in C++ but not
More informationChapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,
Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design
More informationJava Bytecode (binary file)
Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.
More informationCS 61B Discussion 5: Inheritance II Fall 2014
CS 61B Discussion 5: Inheritance II Fall 2014 1 WeirdList Below is a partial solution to the WeirdList problem from homework 3 showing only the most important lines. Part A. Complete the implementation
More informationCreating an object Instance variables
Introduction to Objects: Semantics and Syntax Defining i an object Creating an object Instance variables Instance methods What is OOP? Object-oriented programming (constructing software using objects)
More informationc) And last but not least, there are javadoc comments. See Weiss.
CSCI 151 Spring 2010 Java Bootcamp The following notes are meant to be a quick refresher on Java. It is not meant to be a means on its own to learn Java. For that you would need a lot more detail (for
More informationComputer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary
GATE- 2016-17 Postal Correspondence 1 C-Programming Computer Science & Information Technology (CS) 20 Rank under AIR 100 Postal Correspondence Examination Oriented Theory, Practice Set Key concepts, Analysis
More information