Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes and Objects CGS3416 Spring 2019.

Similar presentations


Presentation on theme: "Classes and Objects CGS3416 Spring 2019."— Presentation transcript:

1 Classes and Objects CGS3416 Spring 2019

2

3

4 Classes Class is a blueprint for objects. A class is a user-defined type that describes and defines objects of the same type. A class contains data values and methods.

5 Data values

6 Objects and References

7 Objects

8 Creating an object from a class

9 References

10

11

12

13 Objects and References
Object: an instance of a class Class: a template or blueprint from which individual objects are created Instantiation: the creation of an object class objName = new Constructor(); The constructor will always have the same name as the class Person sally = new Person();

14 Constructor A constructor is used to initialize an object (variables and call methods). Get objects of a class ready for use Same name as the class No return type

15 Type of constructor Default constructor Parameterized constructor

16 Method overloading Demo
In Java it is possible to define two or more methods within the same class that are having the same name, but their parameter declarations are different. In the case, the methods are said to be overloaded, and the process is referred to as method overloading. Method overloading is one of the ways of java implementation of polymorphism. Demo

17 Constructors overloading vs Method overloading
Strictly speaking, constructor overloading is somewhat similar to method overloading. If we want to have different ways of initializing an object using different number of parameters, then we must do constructor overloading as we do method overloading when we want different definitions of a method based on different parameters.

18 Constructor overloading
Demo (Box and BoxRunner)

19 Protection levels in a class

20 Data Hiding

21 Demo (Student and StudentRunner)

22 Accessor and mutator methods
Instance variables should be declared with the keyword private Data hiding is the main reason for the private declaration Accessor methods allow an outside class to READ instance variables Accessor methods often times start with the word get and can be called getters Mutator methods allow an outside class to WRITE instance variables Mutator methods often times start with the word set and can be called setters

23 Demo (Account and AccountRunner)

24 The ‘this’ keyword Many times it is necessary to refer to its own object in a method or a constructor. To allow this Java defines the ‘this’ keyword. The ‘this’ is used inside the method or constructor to refer its own object That is, ‘this’ is always a reference to the object of the current class type.


Download ppt "Classes and Objects CGS3416 Spring 2019."

Similar presentations


Ads by Google