Download presentation
Presentation is loading. Please wait.
Published byTerence Rice Modified over 9 years ago
1
Object orientation and Packaging in Java
2
2 4.0 Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify objects and make you think in a object oriented way. Objective: After completing this chapter you will able to, create objects Implement Inheritance Create abstract Class Create Interfaces Implement overriding and overloading
3
3 Introduction to Objects Software bundles of data –Methods: Procedures act on that data. –Objects: The merger of data and methods Objects have two characteristics: –State –Behavior State: –Determined by its data Behavior: –Defined by its methods
4
4 Introduction to Objects Sample Java Bean: public class Animal { private String name; public String getName(){ return name; } public void setName(String name) { this.name = name; }
5
5 Inheritance Process of creating a new class with the characteristics of an existing class, along with additional characteristics unique to the new class Features: –Provides a powerful and natural mechanism for organizing and structuring programs –Provides a means to create classes based on other classes –When a class is based on another class, it inherits all the properties of that class, including the data and methods for the class –The class doing the inheriting is referred to as the subclass (child class), and the class providing the information to inherit is referred to as the superclass (parent class) –subclasses can add variables and methods to the ones they inherited from the superclass
6
6 Abstract Class Abstract Class: –Super classes that act purely as templates for more usable subclasses –Serves as nothing more than an abstraction for the common class functionality shared by the subclasses –Cannot be instantiated Reason: - Parts of it have been specifically left unimplemented - More specifically, these parts are made up of methods that have yet to be implemented—abstract methods
7
7 Interfaces Is a prototype for a class Useful from a logical design perspective Are abstract classes that are left completely unimplemented Interface member data is limited to static final variables, which means that they are constant A class can implement multiple interfaces If a class implements multiple interfaces, that class must provide all of the functionality for the methods defined in the interfaces
8
8 Interfaces Benefits: –Provide a means to define the protocols for a class without worrying with the implementation details –Once interfaces have been designed, the class development can take place without worrying about communication among classes Syntax: –The syntax for creating interfaces follows: interface Identifier { InterfaceBody } Identifier is the name of the interface InterfaceBody refers to the abstract methods and static final variables that make up the interface It isn’t necessary to use the abstract keyword for the methods
9
9 Interfaces Implementing Interfaces: To implement an interface, you use the implements keyword class Identifier implements Interface { ClassBody } –Identifier refers to the name of the new class –Interface is the name of the interface you are implementing, and ClassBody is the new class body –The class must implement all the methods declared in the interface or must be itself an interface extending the first interface
10
10 Overriding A subclass can give a different implementation for a method already defined in the super class. Rules: –The signature of the method in the subclass must exactly be the same as that of the super class –The access modifier of the overriding method of the subclass cannot be more narrow than that allowed by super class –The overriding method of the subclass cannot throw a broader exception than that thrown by the super class unless it’s a runtime exception –Method overriding is a simple, yet powerful usage of object-oriented design
11
11 Overloading Enables you to specify different types of information (parameters) to send to a method Rules: –To overload a method, you declare another version with the same name but different parameters –You cannot differentiate two overloaded methods by their return types alone –The access modifiers and the exceptions thrown can be of any type for these two overloaded methods The compiler keeps up with the parameters for each method along with the name When a call to a method is encountered in a program, the compiler checks the name and the parameters to determine which overloaded method is being called
12
12 Object Orientation and Packaging: Summary Software bundles of data –Methods: Procedures act on that data. –Objects: The merger of data and methods Inheritance - Process of creating a new class with the characteristics of an existing class, along with additional characteristics unique to the new class Abstract Class - Super classes that act purely as templates for more usable subclasses Interface - Is a prototype for a class Overriding - A subclass can give a different implementation for a method already defined in the super class. Overloading - Enables you to specify different types of information (parameters) to send to a method
13
13 Test Your Understanding What is Inheritence? Where can it be used? How do you decide as to whether to use an interface or an abstract class? What is overriding and overloading?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.