Download presentation
Presentation is loading. Please wait.
Published byReginald Gardner Modified over 8 years ago
1
OOPM
2
Java Array Array is a collection of similar type of elements that have contiguous memory location. Java array is an object the contains elements of similar data type. It is a data structure where we store similar elements. We can store only fixed set of elements in a java array.
3
Advantage of Java Array –Code Optimization: It makes the code optimized, we can retrieve or sort the data easily. –Random access: We can get any data located at any index position. Disadvantage of Java Array –Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in java. Types of Array in java There are two types of array. –Single Dimensional Array –Multidimensional Array
4
Syntax to Declare an Array in java dataType[] arr; (or) dataType arr[]; Instantiation of an Array in java arrayRefVar=new datatype[size];
5
class Testarray { public static void main(String args[]) { int a[]=new int[5];//declaration and instantiation a[0]=10;//initialization a[1]=20; a[2]=70; a[3]=40; a[4]=50; //printing array for(int i=0;i<a.length;i++)//length is the property of array System.out.println(a[i]); }
6
Array Example class Testarray1 { public static void main(String args[]) { int a[]={33,3,4,5};//declaration, instantiation and initialization //printing array for(int i=0;i<a.length;i++) /length is the property of array System.out.println(a[i]); }
7
Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters (determined by the number, types and order of the parameters) – this is called method overloading. When an overloaded method is called, the Java compiler selects the appropriate method by examining the number, types and order of the arguments in the call. Method overloading is commonly used to create several methods with the same name that perform the same or similar tasks, but on different types or different numbers of arguments.
8
Argument lists could differ in 1. Number of parameters. 2. Data type of parameters. 3. Sequence of Data type of parameters. Method overloading is also known as Static Polymorphism.
9
When methods name are same but number of arguments are different. class Calculation { void sum(int a,int b) {System.out.println(a+b);} void sum(int a,int b,int c) {System.out.println(a+b+c);} public static void main(String args[]) { Calculation obj=new Calculation(); obj.sum(10,10,10); obj.sum(20,20); } Example 1
10
When methods name are same but number of arguments are different. class DisplayOverloading { public void disp(char c) { System.out.println(c); } public void disp(char c, int num) { System.out.println(c + " "+num); } } class Sample { public static void main(String args[]) { DisplayOverloading obj = new DisplayOverloading(); obj.disp('a'); obj.disp('a',10); } OUTPUT a a 10 Example 2
11
Difference in data type of arguments class Calculation2 { void sum(int a,int b){System.out.println(a+b);} void sum(double a,double b){System.out.println(a+b);} public static void main(String args[]) { Calculation2 obj=new Calculation2(); obj.sum(10.5,10.5); obj.sum(20,20); } Example 1
12
Difference in data type of arguments class DisplayOverloading2 { public void disp(char c) { System.out.println(c); } public void disp(int c) { System.out.println(c ); } class Sample2 { public static void main(String args[]) { DisplayOverloading2 obj = new DisplayOverloading2(); obj.disp('a'); obj.disp(5); } OUTPUT a 5 Example 2
13
Sequence of data type of arguments class DisplayOverloading3 { public void disp(char c, int num) { System.out.println("I’m the first definition of method disp"); } public void disp(int num, char c) { System.out.println("I’m the second definition of method disp" ); } class Sample3 { public static void main(String args[]) { DisplayOverloading3 obj = new DisplayOverloading3(); obj.disp('x', 51 ); obj.disp(52, 'y'); } OUTPUT I’m the first definition of method disp I’m the second definition of method disp
14
Valid/Invalid cases of overloading Case 1: int mymethod(int a, int b, float c) int mymethod(int var1, int var2, float var3) Case 2: int mymethod(int a, int b) int mymethod(float var1, float var2) Case 3: int mymethod(int a, int b) int mymethod(int num) Case 4: float mymethod(int a, float b) float mymethod(float var1, int var2) Case 5: int mymethod(int a, int b) float mymethod(int var1, int var2)
15
Result 1: Compile time error. Argument lists are exactly same. Both methods are having same number, data types and same sequence of data types in arguments. Result 2: Perfectly fine. Valid case for overloading. Here data types of arguments are different. Result 3: Perfectly fine. Valid case for overloading. Here number of arguments are different. Result 4: Perfectly fine. Valid case for overloading. Sequence of the data types are different, first method is having (int, float) and second is having (float, int). Result 5: Compile time error. Argument lists are exactly same. Even though return type of methods are different, it is not a valid case. Since return type of method doesn’t matter while overloading a method.
16
Method Overloading Cont. The compiler distinguishes overloaded methods by their signature – a combination of the method’s name and the number, types and order of its parameters. If the compiler looked only at method names during compilation, the code in previous example would be ambiguous. Overloaded method calls cannot be distinguished by return type. Overloaded method declarations with identical signatures cause compilation errors, even if the return types are different. That is understandable, because the return type is not necessarily apparent when you call a method.
17
Inheritance Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class, or superclass, or base class The derived class is called the child class or subclass. As the name implies, the child inherits characteristics of the parent That is, the child class inherits the methods and data defined for the parent class
18
Why use inheritance in java For Method Overriding (so runtime polymorphism can be achieved). For Code Reusability.
19
To tailor a derived class, the programmer can add new variables or methods, or can modify the inherited ones Software reuse is at the heart of inheritance By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software Inheritance is a compile-time mechanism. A super-class can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance. Inheritance
20
Inheritance relationships often are shown graphically in a UML(Unified modeling language) class diagram, with an arrow with an open arrowhead pointing to the parent class Inheritance should create an is-a relationship, meaning the child is a more specific version of the parent Vehicle Car public class ChildClass extends BaseClass { // derived class methods extend and possibly override }
21
Example 1 Inheritance class Employee { float salary=40000; } class Programmer extends Employee { int bonus=10000; public static void main(String args[]) { Programmer p=new Programmer(); System.out.println("Programmer salary is:"+p.salary); System.out.println("Bonus of Programmer is:"+p.bonus); }
22
Types of inheritance in java On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical. In java programming, multiple and hybrid inheritance is supported through interface only.
23
Why No multiple Inheritance in java class A { void msg() {System.out.println("Hello");} } class B { void msg() {System.out.println("Welcome");} } class C extends A,B//suppose if it were { public Static void main(String args[]) { C obj=new C(); obj.msg();//Now which msg() method would be invoked? } //Compile time error
24
Example Inheritance class Vehicle { String color; int speed; int size; void attributes() { System.out.println("Color : " + color); System.out.println("Speed : " + speed); System.out.println("Size : " + size); }
25
Example cont. // A subclass which extends for vehicle class Car extends Vehicle { int CC; int gears; void attributescar() { // The subclass refers to the members of the superclass System.out.println("Color of Car : " + color); System.out.println("Speed of Car : " + speed); System.out.println("Size of Car : " + size); System.out.println("CC of Car : " + CC); System.out.println("No of gears of Car : " + gears); }
26
Example cont public class Test { public static void main(String args[]) { Car b1 = new Car(); b1.color = "Blue"; b1.speed = 200 ; b1.size = 22; b1.CC = 1000; b1.gears = 5; b1.attributescar(); } output Color of Car : Blue Speed of Car : 200 Size of Car : 22 CC of Car : 1000 No of gears of Car : 5
27
Constructor in Java Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.
28
Rules for creating java constructor There are basically two rules defined for the constructor. –Constructor name must be same as its class name –Constructor must have no explicit return type Types of java constructors –Default constructor –no-arg constructor –Parameterized constructor
29
Default constructor: If you do not define any constructor in your class, java generates one for you by default. This constructor is known as default constructor. You would not find it in your source code but it would present there. It would look like this if you could see it. Syntax: public Demo() { }
30
No-arg constructor: Constructor with no arguments is known as no-arg constructor. The signature is same as default constructor, however body can have any code unlike default constructor where the body does nothing. The constructor is called default only when it has been generated by java. class Demo { public Demo() { System.out.println("This is a default constructor"); } }
31
Parameterized constructor Constructor with arguments is known as parameterized constructor. class Demo { public Demo(int num, String str) { System.out.println("This is a parameterized constructor"); }
32
Default Constructor class Example { public void demoMethod() { System.out.println("hello"); } public static void main(String args[]) { Example obj = new Example(); obj.demoMethod(); }
33
class Example2 { private int var; public Example2() { //code for default one var = 10; } public Example2(int num) { //code for parameterized one var = num; } public int getValue() { return var; } public static void main(String args[]) { Example2 obj2 = new Example2(); System.out.println("var is: "+obj2.getValue()); } No-arg and Parameterized Constructor
34
class Example2 { private int var; public Example2() { //code for default one var = 10; } public Example2(int num) { //code for parameterized one var = num; } public int getValue() { return var; } public static void main(String args[]) { Example2 obj2 = new Example2(77); System.out.println("var is: "+obj2.getValue()); } }
35
class Example2 { private int var; public Example2() { //code for default one var = 10; } public Example2(int num) { //code for parameterized one var = num; } public int getValue() { return var; } public static void main(String args[]) { Example2 obj3 = new Example2(); Example2 obj2 = new Example2(77); System.out.println("var is: "+obj3.getValue()); System.out.println("var is: "+obj2.getValue()); }
36
class Example3 { private int var; public Example3(int num) { var=num; } public int getValue() { return var; } public static void main(String args[]) { Example3 myobj = new Example3(); System.out.println("value of var is: "+myobj.getValue()); }
37
class Example { Example(int i, int j) { System.out.print("parameterized constructor"); } Example(int i, int j, int k) { System.out.print("parameterized constructor"); } public static void main(String args[]) { Example obj = new Example(); } Output: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor Example() is undefined
38
Difference Between Java constructor and Java Method
39
Method Overriding
40
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java. In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.
41
Usage of Java Method Overriding –Method overriding is used to provide specific implementation of a method that is already provided by its super class. –Method overriding is used for runtime polymorphism Rules for Java Method Overriding –method must have same name as in the parent class –method must have same parameter as in the parent class. –must be IS-A relationship (inheritance).
42
No overriding class Vehicle { void run() { System.out.println("Vehicle is running");} } class Bike extends Vehicle { public static void main(String args[]) { Bike obj = new Bike(); obj.run(); }
43
Example of method overriding class Vehicle { void run() { System.out.println("Vehicle is running");} } class Bike2 extends Vehicle { void run() {System.out.println("Bike is running safely");} public static void main(String args[]) { Bike2 obj = new Bike2(); obj.run(); }
44
Real example of Java Method Overriding
45
class Bank { int getRateOfInterest(){return 0;} } class SBI extends Bank { int getRateOfInterest(){return 8;} } class ICICI extends Bank { int getRateOfInterest(){return 7;} } class AXIS extends Bank { int getRateOfInterest(){return 9;} } class Test2 { public static void main(String args[]){ SBI s=new SBI(); ICICI i=new ICICI(); AXIS a=new AXIS(); System.out.println("SBI Rate of Interest: "+s.getRateOfInterest()); System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest()); System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest()); }
46
Difference between Method Overloading and Method Overriding
47
Controlling Access to Members of a Class Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control: –At the top level—public, or package-private (no explicit modifier). –At the member level —public, private, protected, or package-private (no explicit modifier).
48
A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes
49
The first data column indicates whether the class itself has access to the member defined by the access level.A class always has access to its own members. The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The third column indicates whether subclasses of the class declared outside this package have access to the member. The fourth column indicates whether all classes have access to the member.
50
Example
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.