Java Grade 11 Part 3
Java Grade 11 Part 3
Java Grade 11 Part 3
Language-
JAVA
Sonal
Maskeen
Declaring a class
• package
• Class name
• Constructor
• Fields
• methods
2
Class and Objects
class OtherClass {
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
Global and local variables
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}
}
Types of Inheritance
• Why And When To Use "Inheritance"?
• - It is useful for code reusability: reuse
attributes and methods of an existing
class when you create a new class.
Static Binding & Dynamic
Binding
Polymorphism
class Adder{
static int add(int a,int b){
return a+b;
}
static int add(int a,int b,int c){
return a+b+c;
}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
Run time Polymorphism
class Shape{
void draw(){System.out.println("drawing...");}
}
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle...");}
}
class Circle extends Shape{
void draw(){System.out.println("drawing circle...");}
}
class Triangle extends Shape{
void draw(){System.out.println("drawing triangle...");}
}
class TestPolymorphism2{
public static void main(String args[]){
Shape s;
s=new Rectangle();
s.draw();
s=new Circle();
s.draw();
s=new Triangle();
s.draw();
}
Polymorphism
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
interface SecondInterface {
public void myOtherMethod();
}
class MyMainClass {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
}
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep(); // interface method (does not have a body)
}
class MyMainClass {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.animalSound();
myDog.sleep();
}
}
Notes on Interfaces:
45
An array is an object
• Person mary = new Person ( );
• int myArray[ ] = new int[5];
• int myArray[ ] = {1, 4, 9, 16, 25};
• String languages [ ] = {"Prolog",
"Java"};
• Since arrays are objects they are allocated
dynamically
• Arrays, like all objects, are subject to garbage
collection when no more references remain
– so fewer memory leaks
– Java doesn’t have pointers!
46
Array length
• When dealing with arrays, it is advantageous to
know the number of elements contained within the
array, or the array's "length". This length can be
obtained by using the array name followed by
.length. If an array named numbers contains 10
values, the code numbers.length will be 10.
http://mathbits.com/MathBits/Java/arrays/Declare.htm
Arrays
• Arrays are used to store multiple values in a single
variable, instead of declaring separate variables for
each value.
• To declare an array, define the variable type
with square brackets:
• String[] cars;
• String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
• System.out.println(cars.length); //to find array length
as 4
• Multidimensional Arrays
int x = myNumbers[1][2];
System.out.println(x); // Outputs 7
2D arrays
Declaring a 2D array in Java
int[ ][ ] arrNumbers = new int[6][5];
Nothing!
You only put
Output data in, not
printed it!
Common mistake: printing a 2D array
Output
Correct way: printing a 2D array
2 D array
Common 2D array tasks
• How to search the minimum and the
maximum element in an array ?
Algorithmic development
Example 1
Given the following array NAMES
and the following algorithm, which is constructed to reverse the contents of array NAMES
1.trace the algorithm, showing contents of the array after each execution of the loop [2 marks]
2.state the number of times the loop is executed [1 mark]
3.outline why the algorithm does not reverse the contents of the array NAMES, and how
this could be corrected. [2 marks]
1)
2) 4 times