Unit-2 Objects and Classes

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
OOP: Inheritance By: Lamiaa Said.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
{ int fun = 99; } Scope refers to whether the variable is seen within a certain context Any variable defined inside of curly brackets, only exists within.
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Computer Programming Lab(4).
Saravanan.G.
UML Basics & Access Modifier
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
1 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Session 7 Methods Strings Constructors this Inheritance.
Objects and Classes Mostafa Abdallah
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Page 4.1 – Autumn 2010Steffen Vissing Andersen SDJ I1, Autumn 2010 Agenda – Session 4 – 7. September 2009 Java fundamentals, checkpoint from chapter 2.
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Software development For large and complex software use divide and conquer rule. Software design methods: Structured design Object-Oriented design.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Classes - Intermediate
Methods.
Topics Instance variables, set and get methods Encapsulation
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Import javax.swing.JOptionPane; public class Rectangle { public static void main(String[] args) { double width, length, area, perimeter; String lengthStr,
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Staples are our staple Building upon our solution.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
J AVA P ROGRAMMING 2 CH 04: C LASSES, O BJECTS AND M ETHODS (II) 0.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
GC211 Data structure Lecture 3 Sara Alhajjam.
Sachin Malhotra Saurabh Choudhary
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Exceptions, Interfaces & Generics
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Review Session.
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Constructor Overloading
Overloading and Constructors
TO COMPLETE THE FOLLOWING:
Contents Introduction to Constructor Characteristics of Constructor
Chapter 8 Slides from GaddisText
Assignment 7 User Defined Classes Part 2
S.VIGNESH Assistant Professor/CSE, SECE
Method Overloading in JAVA
CS2011 Introduction to Programming I Methods (II)
JAVA Constructors.
class PrintOnetoTen { public static void main(String args[]) {
Object-Oriented Programming and class Design
Unit-1 Introduction to Java
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
MIS 222 – Lecture 12 10/9/2003.
class Box { double width; double height; double depth; }
Presentation transcript:

Unit-2 Objects and Classes Constructors

Constructors Java has a mechanism, known as constructor, for automatically initializing the values for an object, as soon as the object is created. Constructors have the same name as the class it resides in and is syntactically similar to a method. It is automatically called immediatey after the object for the class is created by new operator. Contructors have no return type, not even void, as the implicit return type of a class’ constructor is the class type itself.

Types of Constructors Implicit/Default Explicit Parameterized

Default Constructor Example

Explicit Constructor Example class Room{ double length, breadth, height, volume; Room( ) { length = 14; breadth = 12; height = 10; } double volComp( ) { volume = length * breadth * height; return volume; } public static void main (String args[ ]) { Room r1 = new Room(); Room r2 = new Room(); System.out.println("The volume of the room is "+r1.volComp( )); System.out.println("The volume of the room is "+r2.volComp( )); }

Parameterized Constructor Example

Constructor Overloading Constructors for a class have the same name as the class but they can have different signature i.e. different types of arguments or different number of arguments. Such constructors can be termed as overloaded constructors. In the example given on next slide, we have two different classes, Rectangle and ConstOverloading. Rectangle class has two constructors, both with same names but different signatures. Second class ConstOverloading, has the main( ) method inside it.

Constructor Overloading Example class Rectangle{ int l, b; Rectangle(){ l = 10; b = 20; } Rectangle(int x, int y){ l = x; b = y; int area() { return l*b; }} class ConstOverloading { public static void main(String args[]) { Rectangle rectangle1=new Rectangle(); System.out.println("The area of a rectangle using first constructor is: "+rectangle1.area()); Rectangle rectangle2=new Rectangle(4,5); System.out.println("The area of a rectangle using second constructor is: "+rectangle2.area(); } }

Constructor Overloading (Output) The area of a rectangle using first constructor is: 200 The area of a rectangle using second constructor is: 20