JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.

Slides:



Advertisements
Similar presentations
OOP: Inheritance By: Lamiaa Said.
Advertisements

Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
Constructors And Instantiation. Constructor Basics Every class must have a constructor Even abstract classes!! No return types Their names must exactly.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Initialization George Blank Subba Reddy Daka. Importance of Initialization Java classes are initialized and have predictable default values. These values.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Programming Languages and Paradigms Object-Oriented Programming.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
ECE122 Feb. 22, Any question on Vehicle sample code?
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Constructors & Garbage Collection Ch. 9 – Head First Java.
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.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Inheritance ndex.html ndex.htmland “Java.
Methods.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
J AVA P ROGRAMMING 2 CH 04: C LASSES, O BJECTS AND M ETHODS (II) 0.
Design issues for Object-Oriented Languages
CS 432: Compiler Construction Lecture 15
Modern Programming Tools And Techniques-I
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Chapter 5 Hierarchies IS-A associations superclasses subclasses
ATS Application Programming: Java Programming
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
METHOD OVERRIDING in JAVA
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections 5, 6 Chapter 5: Sections 1 - 5

JVM-2 Class Initialization This is the process of setting class variables to their proper initial values - initial values desired by the programmer. 1.class Example1{ 2.static double rate = 3.5; 3.static int size = 3*(int)(Math.random()*5); } Initialization of a class consists of two steps: –Initializing its direct superclass (if any and if not already initialized) –Executing its own initialization statements The above implies that, the first class that gets initialized is Object. Note that static final variables are not treated as class variables but as constants and are assigned their values at compilation. 1.class Example2{ 2.static final int angle = 35; 3.static final int length = angle * 2; }

JVM-3 Class Instantiation After a class is loaded, linked, and initialized, it is ready for use. Its static fields and static methods can be used and it can be instantiated. When a new class instance is created, memory is allocated for all its instance variables in the heap. Memory is also allocated recursively for all the instance variables declared in its super class and all classes up in inheritance hierarchy. All instance variables in the new object and those of its super classes are then initialized to their default values. The constructor invoked in the instantiation is then processed according to the rules shown on the next page. Finally, the reference to the newly created object is returned as the result.

JVM-4 Class Instantiation – Cont’d Rules for processing a constructor: 1.Assign the arguments for the constructor to its parameter variables. 2.If this constructor begins with an explicit invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively. 3.If this constructor is for a class other than Object, then it will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively. 4.Initialize the instance variables for this class with their proper values. 5.Execute the rest of the body of this constructor.

JVM-5 Example of Class Instantiation 1 1.class GrandFather{ 2.int grandy = 70; 3.public GrandFather(int grandy){ 4.this.grandy = grandy; 5.System.out.println("Grandy: "+grandy); 6.} 7.} 8.class Father extends GrandFather{ 9.int father = 40; 10.public Father(int grandy, int father){ 11.super(grandy); 12.this.father = father; 13.System.out.println("Grandy: "+grandy+" Father: "+father); 14.} 15.} 16.class Son extends Father{ 17.int son = 10; 18.public Son(int grandy, int father, int son){ 19.super(grandy, father); 20.this.son = son; 21.System.out.println("Grandy: "+grandy+" Father: "+father+" Son: "+son); 22.} 23.} 24.public class Instantiation{ 25.public static void main(String[] args){ 26.Son s = new Son(65, 35, 5); 27.} 28.}

JVM-6 Example of Class Instantiation 2 Calling method of a subclass before the subclass is initialized 1.class Super { 2. Super() { printThree(); } 3. void printThree() { 4. System.out.println("three"); 5. } 6.} 7.class Test extends Super { 8. int three = (int)Math.PI; // That is, 3 9. public static void main(String[] args) { 10. Test t = new Test(); 11. t.printThree(); 12. } 13. void printThree() { 14. System.out.println(three); 15. } 16.}

JVM-7 Example of Class Instantiation 3 1.class Base{ 2. Base(){ 3. this(102); 4. System.out.println("Inside Base()"); 5. } 6. Base(int x){ 7. this("ICS ",x); 8. System.out.println("Inside Base(int x)"); 9. } 10. Base(String s, int x){ 11. super(); 12. System.out.println(s+x); 13. } 14.} 15.public class Derived extends Base{ 16. public Derived(){ 17. this("ICS 201"); 18. System.out.println("From Derived()"); 19. } 20. public Derived(String s){ 21. System.out.println("From Derived(String s)"); 22. System.out.println(s); 23. } 24. public static void main(String g[]){ 25. new Derived(); 26. } 27.}

JVM-8 Exercises 1.Write a program to show that each loaded type has only one instance of java.lang.Class associated with it, irrespective of how many times it is used in a class. 2.Write a program to show that Loading may be caused either by creating an instance of a class or by accessing a static member. 3.Write a class to show that class variables are initialized with their default values when loaded. Also show that instance variables are initialized with their default values when an object is created. 4.Demonstrate that Verification actually takes place in the Loading process. To do this, write a class, Base, and a class, SubClass, that extends Base. Compile both classes. Then modify Base by changing the signature of one of its methods and compile it alone. Now write a test program that uses Subclass and try to compile and run it.