1 Class Constructor Is a specific method Used to initialize the class’s fields Having the same name as the declaring classclass Doesn’t have return type(even.

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Final and Abstract Classes
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Object Oriented Programming
INHERITANCE BASICS Reusability is achieved by INHERITANCE
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.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CSE S. Tanimoto Java Classes and Inheritance 1 Java Classes and Inheritance Object (again): A computational unit consisting of some data elements.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
UML Class Diagram: class Rectangle
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
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.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
CITA 342 Section 1 Object Oriented Programming (OOP)
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
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 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Topics Instance variables, set and get methods Encapsulation
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
DR. NERMIN HAMZA Java: Lecture 2 Java life cycle 2 Java programs normally undergo four phases  Edit  Programmer writes program (and stores program.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
OOP: Encapsulation &Abstraction
Final and Abstract Classes
Interface.
Review Session.
UML Class Diagram: class Rectangle
Interface.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Polymorphism CT1513.
Classes & Objects: Examples
Interfaces.
Inheritance Inheritance is a fundamental Object Oriented concept
Agenda About Homework for BPJ lesson 36 About practice of last class
Objects and Classes Creating Objects and Object Reference Variables
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
Agenda About Homework for BPJ lesson 15 Overloading constructor
Chapter 11 Inheritance and Encapsulation and Polymorphism
CSG2H3 Object Oriented Programming
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

1 Class Constructor Is a specific method Used to initialize the class’s fields Having the same name as the declaring classclass Doesn’t have return type(even void) Called automatically when an Object is created Can be overlaoded it is not inherited

2 Consructor Example public class Cube { public int length; public int breadth; public int height; public int getVolume() { return (length * breadth * height); } Public Cube() { length = 10; breadth = 10; height = 10; } Public Cube(int l, int b, int h) { length = l; breadth = b; height = h; } }//class

3 Creating objects of Cube ClassName objectName; objectName= new ConstructorName(parameterList ); Public class Test{ Public static void main(String args[]){ Cube c1=new Cube(); C1.breadth=25; Cube c2=new Cube(32,25,32); Cube c3=new Cube(); System.out.println(“volume of c1=“+c1.getVolume()); System.out.println(“volume of c2=“+c1.getVolume()); System.out.println(“volume of c3=“+c1.getVolume()); }

Constructor Example public class Cube { public int length; public int breadth; Public int height; public int getVolume() { return (length * breadth * height); } public Cube() { this(10, 10); } Cube(int l, int b) { this(l, b, 10); } Cube(int l, int b, int h) { length = l; breadth = b; height = h;} } 4

5 Class Modifioer modifier Description publicis accessible anywhere and by anyone protectedis accessible within the defining package and its subclasses blank(omitted)is accessible within the package privatemember is only accessible to the class that defines it staticused to declare it as class field abstract cannot be instantiated, must be a superclass, used whenever one or more methods are abstract final can’t be subclassed

6 Field Modifier ModifierDescription public is accessible by anyone, anywhere protected is accessible by methods of that class or its subclasses or by methods of classes which are in the package of class blankis accessible by methods within the same package only private is accessible by methods within the same class only staticthere is only one per class but still there could be more than one per JVM transientfield should not be serialized

7 Method Modifier ModifierDescription public is accessible by anyone, anywhere protected is accessible by methods within the same class or its subclasses blank is accessible by methods within the same package only private is accessible by methods within the same class only staticcannot be instantiated, are called by classname.method, can only access static variables abstractMethod is defined but contains no implementation code (implementation code is included in the subclass). If a method is abstract then the entire class must be abstract. synchronizedacquire a lock on the class for static methods acquires a lock on the instance for non-static classes

Abstract Class public abstract class Shape { public abstract double area(); public abstract double circumference(); } We can’t create obeject from this class 8

Final fields public final class Math { //… public static final double E = ; public static final double PI = ; //… } 9