STATIC VARIABLE/ METHODS, INHERITANCE, INTERFACE AND COMMAND LINE ARGUMENTS



Similar documents
Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

Introduction to Object-Oriented Programming

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

LAB4 Making Classes and Objects

Java Interview Questions and Answers

History OOP languages Year Language 1967 Simula Smalltalk

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Java CPD (I) Frans Coenen Department of Computer Science

Building Java Programs

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Introduction to Java

Building Java Programs

Construction of classes with classes

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

J a v a Quiz (Unit 3, Test 0 Practice)

JAVA ARRAY EXAMPLE PDF

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Part I. Multiple Choice Questions (2 points each):

Inheritance, overloading and overriding

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

public static void main(string[] args) { System.out.println("hello, world"); } }

This explanations are for absolute beginners. Skilled programmers should (and probably will) use more effective approach.

Install Java Development Kit (JDK) 1.8

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Tutorial on Writing Modular Programs in Scala

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Short Introduction to the Concepts of Programming in Java Overview over the most important constructs

AP Computer Science Java Subset

Chapter 2 Introduction to Java programming

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

1 Hour, Closed Notes, Browser open to Java API docs is OK

In this Chapter you ll learn:

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

Chapter 13 - Inheritance

Description of Class Mutation Mutation Operators for Java

VB.NET - CLASSES & OBJECTS

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Object-Oriented Programming in Java

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

Programmierpraktikum

Comp 248 Introduction to Programming

Java Program Coding Standards Programming for Information Technology

Sample CSE8A midterm Multiple Choice (circle one)

CS170 Lab 11 Abstract Data Types & Objects

The Interface Concept

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Web Development and Core Java Lab Manual V th Semester

Introducing Variance into the Java Programming Language DRAFT

Many applications consist of one or more classes, each containing one or more methods. If you become part of a development team in industry, you may

Polymorphism. Why use polymorphism Upcast revisited (and downcast) Static and dynamic type Dynamic binding. Polymorphism.

CS193j, Stanford Handout #10 OOP 3

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Java Programming Language

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Java: overview by example

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

AP Computer Science Static Methods, Strings, User Input

Collections.sort(population); // Método de ordenamiento

Laboratory Assignments of OBJECT ORIENTED METHODOLOGY & PROGRAMMING (USING C++) [IT 553]

Object-Oriented Programming: Polymorphism

Smallest Java Package? Java.applet.* having 1 class and 3 interfaces. Applet Class and AppletContext, AppletStub, Audioclip interfaces.

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Advanced Java Client API

Contents. 9-1 Copyright (c) N. Afshartous

Moving from CS 61A Scheme to CS 61B Java

Object Oriented Software Design

INPUT AND OUTPUT STREAMS

Introduction to programming

Chapter 1 Java Program Design and Development

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Homework/Program #5 Solutions

Getting Started with the Internet Communications Engine

Lecture J - Exceptions

Building Java Programs

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

AP Computer Science A 2010 Scoring Guidelines

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

Introduction to Java Applets (Deitel chapter 3)

15-214: Principles of Software Construction 8 th March 2012

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

COMPUTER SCIENCE. Paper 1 (THEORY)

Programming Languages Featherweight Java David Walker

Transcription:

STATIC VARIABLE/ METHODS, INHERITANCE, INTERFACE AND COMMAND LINE ARGUMENTS Example 1: Static Member Variable 1. /* 2. This Java Example shows how to declare and use static member variable inside a java class. 3. */ 4. 5. public class StaticMemberExample 6. { 7. public static void main(string[] args) 8. { 9. ObjectCounter object1 = new ObjectCounter(); 10. System.out.println(object1.getNumberOfObjects()); 11. 12. ObjectCounter object2 = new ObjectCounter(); 13. System.out.println(object2.getNumberOfObjects()); 14. } 15. } 16. 17. class ObjectCounter 18. { 19. /* 20. * Static members are class level variables and shared by all 21. * the objects of the class. 22. * To define static member, use static keyword 23. * e.g. static int i=0; 24. * 25. * Please note that static member variables can be accessed 26. * inside non static methods because they are class level 27. * variables. 28. */ 29. 30. static int counter=0; 31. 32. public ObjectCounter() 33. { 34. 35. /*increase the object counter. Since only one varible 36. *is shared between all objects of this class, it 37. *always return number of objects till now. 38. */ 39. counter++; 40. } 41. 42. //returns number of objects created till now 43. public int getnumberofobjects() 44. { 45. return counter; 46. } 47. } Page 1

Example 2: Static Method 1. /* 2. This Java Example shows how to declare and use static methods inside 3. a java class. 4. */ 5. 6. public class StaticMethodExample 7. { 8. 9. public static void main(string[] args) 10. { 11. 12. int result = MathUtility.add(1, 2); 13. System.out.println("(1+2) is : " + result); 14. } 15. 16. } 17. 18. class MathUtility 19. { 20. 21. /* 22. * To declare static method use static keyword. 23. * Static methods are class level methods and can not access 24. * any instance 25. * member directly. However, it can access member's of a 26. * particular object 27. * using its reference. 28. * 29. * Static methods are generally written as a utility method or 30. * it performs task for all objects of the class. 31. * 32. */ 33. 34. public static int add(int first, int second) 35. { 36. return first + second; 37. } 38. } Page 2

Example 3: Inheritance 1. //Inheritance, In Java You Can Only Inherit from One Class, 2. //Multiple Inheritance is not allowed 3. 4. class Parent 5. { 6. //data members 7. private int a; 8. public int b; 9. protected int c; 10. 11. //default constructor 12. public Parent() 13. { 14. a = b = c = 0; 15. System.out.println("I am class A's default constructor"); 16. } 17. 18. //parameterized constructor 19. public Parent(int a, int b, int c) 20. { 21. this.a = a; 22. this.b = b; 23. this.c = c; 24. } 25. 26. //get and set methods for a 27. public int geta() 28. { 29. return a; 30. } 31. 32. public void seta(int a) 33. { 34. this.a = a; 35. } 36. 37. //get and set methods for b 38. public int getb() 39. { 40. return b; 41. } 42. 43. public void setb(int b) 44. { 45. this.b = b; 46. } 47. 48. //get and set methods for c 49. public int getc() 50. { 51. return c; 52. } Page 3

53. public void setc(int c) 54. { 55. this.c = c; 56. } 57. } 58. 59. //child class inherit the properties of Parent 60. public class Child extends Parent 61. { 62. //default constructor 63. public Child() 64. { 65. System.out.println("I am class B's default constructor"); 66. } 67. 68. //parameterized constructor 69. public Child(int a, int b, int c) 70. { 71. //calling base class (A's) parameterized constructor 72. super(a,b,c); 73. } 74. 75. public static void main(string args[]) 76. { 77. Child obj1 = new Child(); 78. Child obj2 = new Child(1,2,3); 79. 80. //Assigning Child's Reference To Parent 81. Parent objp = new Child(); 82. 83. System.out.println("\nThe value of a can-not be accessed directly in child bcz its private in parent"); 84. System.out.println("\nThe value of b = " + obj2.b); 85. System.out.println("\nThe value of c = " + obj2.c); 86. } 87. } Page 4

Example 4: Constructor Call Sequence in Inheritance 1. //Constructor call sequence in inheritance 2. 3. class GrandFather 4. { 5. //Default Constructor 6. public GrandFather() 7. { 8. System.out.println("Grand Father's Constructor"); 9. } 10. } 11. 12. class Father extends GrandFather 13. { 14. //Default Constructor 15. public Father() 16. { 17. System.out.println("Father's Constructor"); 18. } 19. } 20. 21. class Child1 extends Father 22. { 23. //Default Constructor 24. public Child1() 25. { 26. System.out.println("Child1's Constructor"); 27. } 28. } 29. 30. class Child2 extends Father 31. { 32. //Default Constructor 33. public Child2() 34. { 35. System.out.println("Child2's Constructor"); 36. } 37. } 38. 39. class CallingCons 40. { 41. //Main function 42. public static void main(string []args) 43. { 44. Child1 objc1 = new Child1(); 45. System.out.println(); 46. Child2 objc2 = new Child2(); 47. } 48. } Page 5

Example 5: Usage of super 1. /*usage of super, method overriding and member overriding*/ 2. 3. class Parent 4. { 5. int num; 6. 7. //Function to tell about the class 8. void TellAbout() 9. { 10. System.out.println("Hello, I am Parent called by super"); 11. } 12. } 13. 14. class Child extends Parent 15. { 16. //hidding num1 of Parent 17. int num; 18. 19. //paremetrized constructor of child 20. public Child(int num1, int num2) 21. { 22. super.num = num1; 23. num = num2; 24. } 25. 26. //function to display the vlaues 27. void Display() 28. { 29. System.out.println("num in Parent Class = " + super.num); 30. System.out.println("num in Child Class = " + num); 31. } 32. 33. //function to tell about the class, (overridden method) 34. void TellAbout() 35. { 36. super.tellabout(); 37. System.out.println("Hello, I am child"); 38. } 39. } 40. 41. class UsingSuper 42. { 43. public static void main(string []args) 44. { 45. Child obj = new Child(2,3); 46. obj.tellabout(); 47. obj.display(); 48. } 49. } Page 6

Example 6: Abstract Class 1. /*abstract classes and abstract method. 2. *You cant intansiate the object of abstract class. 3. *To access the functionality of the abstract class 4. *you have to inherit it into sub-class 5. *you can provide only declaration of methods with abstract key-word 6. *you can provide implementation of methods in it 7. *you have to provide the defination of abstract methods in sub-class 8. *otherwise the sub-class becomes abstract as well*/ 9. abstract class DrawingShapes 10. { 11. //data member 12. String ShapeType; 13. //abstract mehtod whose implementation required in sub-class 14. abstract void draw(); 15. //con-crete methods are still allowed in abstract class 16. void BackGroundColor(String color) 17. { 18. System.out.println(ShapeType + " Drawn With Back-Ground-Color " + color); 19. } 20. } 21. //Circle class, extending the DrawingShapes 22. class Circle extends DrawingShapes 23. { 24. //implementation of abstract method 25. void draw() 26. { 27. ShapeType = "Circle"; 28. } 29. } 30. //Rectangle class, extending the Drawing Shpaes 31. class Rectangle extends DrawingShapes 32. { 33. //implementation of abstract method 34. void draw() 35. { 36. ShapeType = "Rectangle"; 37. } 38. } Page 7

39. class UsingAbstract 40. { 41. public static void main(string []args) 42. { 43. //object of type Circle 44. Circle c = new Circle(); 45. 46. //object of type Rectangle 47. Rectangle r = new Rectangle(); 48. 49. //obtain a reference of type DrawingShapes (abstract class) 50. DrawingShapes ds; 51. 52. //calling draw method of Circle class 53. c.draw(); 54. 55. //calling BackGroundColor method of Circle class 56. c.backgroundcolor("yellow"); 57. 58. //ds of abstract class, refers to Rectangle class's object 59. ds = r; 60. 61. //calling draw mehtod of Rectangle class 62. ds.draw(); 63. 64. //calling BackGroundColor method of Rectangle class 65. ds.backgroundcolor("blue"); 66. } 67. } Page 8

Example 7: Creating and Using Interface 1. //declaring a simple interface that contains five abstract mehtods 2. interface ISimpleMath 3. { 4. float Add(float num1, float num2); 5. float Sub(float num1, float num2); 6. float Mul(float num1, float num2); 7. float Div(float num1, float num2); 8. float Rem(float num1, float num2); 9. } 10. 11. //implementing the ISimpleMath interface (declared above) in //Calculator class 12. class Calculator implements ISimpleMath 13. { 14. //implementation of the interface methods 15. //implementing Add(Addition) function 16. public float Add(float num1, float num2) 17. { 18. return num1 + num2; 19. } 20. 21. //implementing Sub(Subtraction) function 22. public float Sub(float num1, float num2) 23. { 24. return num1 - num2; 25. } 26. 27. //implementing Mul(multiply) function 28. public float Mul(float num1, float num2) 29. { 30. return num1 * num2; 31. } 32. 33. //implementing Div(Division) function 34. public float Div(float num1, float num2) 35. { 36. return num1 / num2; 37. } 38. 39. //implementing Rem(Remainder) function 40. public float Rem(float num1, float num2) 41. { 42. return num1 % num2; 43. } 44. 45. //get and set property methods for num1 46. public float GetNum1() 47. { 48. return num1; 49. } 50. 51. 52. 53. Page 9

54. public void SetNum1(float num1) 55. { 56. this.num1 = num1; 57. } 58. 59. //get and set property methods for num2 60. public float GetNum2() 61. { 62. return num2; 63. } 64. 65. public void SetNum2(float num2) 66. { 67. this.num2 = num2; 68. } 69. 70. //class function to take input, by using Keyboard class 71. void TakeInput() 72. { 73. System.out.print(" Enter First Number = "); 74. num1 = Keyboard.readFloat(); 75. 76. System.out.print(" Enter Second Number = "); 77. num2 = Keyboard.readFloat(); 78. } 79. 80. //class function to display results 81. void Display(float a, float s, float m, float d, float r) 82. { 83. System.out.println(" Sum = " + a); 84. System.out.println(" Sub = " + s); 85. System.out.println(" Mul = " + m); 86. System.out.println(" Div = " + d); 87. System.out.println(" Rem = " + r); 88. } 89. 90. //Main Function 91. public static void main(string args[]) 92. { 93. //Creating an object of Calculator class 94. Calculator objc = new Calculator(); 95. 96. //Calling input function of Calculator class 97. objc.takeinput(); 98. 99. //calling calculation methods of Calculator class 100. float a = objc.add(objc.getnum1(), objc.getnum2()); 101. float s = objc.sub(objc.getnum1(), objc.getnum2()); 102. float m = objc.mul(objc.getnum1(), objc.getnum2()); 103. float d = objc.div(objc.getnum1(), objc.getnum2()); 104. float r = objc.rem(objc.getnum1(), objc.getnum2()); 105. 106. //Displaying the result of calculation 107. objc.display(a, s, m, d, r); 108. } Page 10

109. //class data members 110. private float num1; 111. private float num2; 112. } Example 8: Displaying Command Line Arguments Passed to the Program 1. class CommandLineArgs 2. { 3. public static void main(string []args) 4. { 5. //getting the length of passed arguments 6. int len = args.length; 7. if(len==0) 8. { 9. System.out.println("No Argument(s) Have Been Passed"); 10. } 11. else 12. { 13. //displaying the passed arguments 14. for(int i=0; i<len; i++) 15. System.out.println("The Argument At Location args[ "+ i + " ] is " + args[i]); 16. } 17. } 18. } THE END Page 11