ObjectOriented Programming Concepts in Java MSIT 550 Special
Object-Oriented Programming Concepts in Java MSIT 550 Special Topics in IT Prof. Jehad Al Dallal Fall 2019 -2020
Outlines n n n n Encapsulation Inheritance Attributes in Java Methods in Java Modifiers Interfaces and abstract classes Polymorphism Packages
1. Encapsulation n n n All information related to an object is packaged under one name Each object has a name, set of attributes (data), and operations (methods). A class is a template for multiple objects with similar features. An instance of a class is called an object. The process of creating an object is called instantiation. The goal is to write modular and reusable code. Examples: What are the classes required in n n A library system An airline reservation system
2. Inheritance n n n Inheritance is a mechanism for organizing classes Supreclass-subclass relationship In Java, by default, any class inherits the Object class The subclass inherits all the attributes and methods of the superclass Examples? Why is inheritance useful?
2. Inheritance (Cont’d) n n When creating an object of the subclass, a slot for each variable defined in the subclass and the superclass is allocated. When creating an object of the subclass, the object has an access to all methods in both the sub- and super-class Method overriding: creating a method in the subclass that has the same signature (name, number and type of arguments) as the method in the superclass. The new method hides the superclass’s method. In Java: n n only single inheritance is allowed. In case of method overriding, the overrided method can be accessed by using the keyword super To inherit a class, use the keyword extends Examples …
3. Attributes in Java n n There are two types of class attributes: instance variables and class variables. Instance variables n n n Declared outside a method definition. The values are not shared among the objects of the class Class variables n n Declared outside a method definition. The values are shared among the objects of the class static keyword is used Examples n n static int current. Date; static String current. Month;
3. Attributes in Java (Cont’d) n Constants n n n Shared values for all objects of a class Values that will never change Can be an instance or a class variable Declared using the final keyword Initial value has to be included Examples n n final float pi=3. 141592; static final int max. Objects=10;
4. Methods in Java n n Methods define the object’s behavior The method definition includes: n n n The name of the method The type of the returned object or primitive A list of parameters The body of the method Method overloading: declaring different methods with the same name but different parameters either in type or number) Inside the method body, you can refer to the current object using this keyword n n n t=this. x; this. my. Method(this); return this;
4. Methods in Java (Cont’d) n When using a variable inside a method, Java checks for the variable in the following order: n n Inside the method Inside the class In each superclass in turn Example: class A { int v=1; void print() { int v=2; System. out. println(v); //how to print v of the class? } }
4. Methods in Java (Cont’d) n Class method n Declared using the static keyword n n Can be used without instantiating the class n n Example: static int sqrt(float arg 1) { … } Example: float no=Math. sqrt(30. 4); Constructor method: n n n a method that determines how the object is initialized. Has the same name as the class name Does not have a return type
5. Modifiers n n Modifiers are prefixes that can be applied to the methods, attributes, and classes. Recommended order n n n <access>static abstract synchronized <unusual> final native <access> can be public, protected, or private <unusual> can be volatile or transient
5. 1. Method and variable access control n Public n n n Widest possible visibility Anyone can see it and use it Package n n Can be seen and used within the package (subdirectory) The default access modifier
5. 1. Method and variable access control (Cont’d) n Protected n n Private n n Allows access of both subclasses in any package and classes in the same package Can be seen and used only with the class in which it is defined Good rule: Always use private instance variable unless it is constant. Access private instance variables using accessor methods.
5. 2. The final modifier n n Final class cannot be sub-classed Final method cannot be overridden Final variable value cannot be changed (constant) Local variables cannot have modifiers
6. Interfaces (1) n n Provide templates of behaviors that other classes are expected to implement. Interfaces are expected to be extended but cannot be instantiated. Syntax public interface. Name extends Interface 1, Interface 2, …{ … //all methods here are public and abstract //all attributes are public, static, and final } Example public interface my. Interface { int i=1; void search(String s); }
6. Interfaces (2) n n n A class can implements more than one interface Syntax public class. Name extends a. Class implements Interface 1, Interface 2, …{ … /*all methods in the interfaces must be implemented and declared public */ Example public class. A implements my. Interface { public void search(String s) { //do something } }
6. Abstract classes (1) n n The class is declared abstract if it is expected to be subclassed only Abstract classes cannot be instantiated. Abstract classes can contain anything a normal class can contain. Only abstract classes can contain abstract methods. These methods must be implemented in the subclasses.
6. Abstract classes (2) n Example public abstract class. A { int var 1; public void m 1() { //do something} public abstract void m 2(); } public class. B extends class. A { public void m 2() {//do something } }
7. Polymorphism n n An object is polymorphic if it can take different forms. Example class shape { String type; String get. Type() { return “no type”; } } class circle extends shape { String get. Type() { return “circle”; } } class square extends shape { String get. Type() { return “square”; } } class a. Class { public static void main(String args[]) { shape s; circle c=new circle(); square sq=new square(); s=c; System. out. println(s. get. Type()); //prints circle s=sq; System. out. println(s. get. Type()); //prints square } }
8. Packages (1) Application Package 1 Class 2 Attributes Methods Package 2
8. Packages (2) n n Package: set of classes defined in a subdirectory. Every file in the package starts with the line: package_name; The subdirectory has to have the same name as the package name. To use classes in a package, use import keyword n n Example: import TA. Measure. Thermometer; To include all classes in a package, use * n Example: import TA. Measure. *;
8. Packages (3) n n In case of importing packages that have classes of the same name, you must specify the package for the used class. Example: class C is defined in both packages A and B. import A. *; import B. *; class D { void m() { A. C object 1=new A. C(); B. C object 2=new B. C(); } }
- Slides: 22