Professional Documents
Culture Documents
class body
/ / body }
Here,
public static : modifier. Methods are also known as Procedures
max = n2;
else
max = n1;
return max;
}
Method Calling:
For using a method, it should be called. There are two ways in which a method is called
E.g
int min;
public class ExampleMinNumber{
if (n1 > n2)
public static void main(String[] args) {
min = n2;
int a = 11;
else
int b = 6;
min = n1;
int c = minFunction (a, b);
return min;
System.out.println("Minimum Value = " + c);
}}
}
This would produce the following result:
/ ** returns the minimum of two numbers */
public static int minFunction(int n1, int n2) { Minimum value = 6
Main Method
In the construction of a Java application program there must be one main method
present in one of the classes.
The main method is a class method with the following signature:
public static void main(String[] args);
Static: modifier implies that the main method is not an instance method requiring an
object for its invocation.
keyword void: implies that the main method does not return a value. The formal
parameter list will allow arguments to be passed to the main method at the time of
giving the command to execute the program.
Notice that the beginning and ending of the main method are denoted by the use of an
open { and a closed } brace respectively.
The void Keyword:
Methods, Variables and Constructors that are declared private can only be accessed
within the declared class itself.
Variables that are declared private can be accessed outside the class if public getter
methods are present in the class.
Using the private modifier is the main way that an object encapsulates itself and
hide data from the outside world.
Example: The following class uses private access control:
return this.format;
this.format = format;
}}
Here, the format variable of the Logger class is private, so there's
no way for other classes to retrieve or set its value directly.
A class, method, constructor, interface etc declared public can be accessed from
any other class.
Therefore fields, methods, blocks declared inside a public class can be accessed
from any class belonging to the Java Universe.
Because of class inheritance, all public methods and variables of a class are
inherited by its subclasses.
Example:
{ // ...
Protected access gives the subclass a chance to use the helper method or
variable, while preventing a nonrelated class from trying to use it.
Working with objects
Creating variables of your class type is similar to creating variables of primitive data
types, such as integer or float.
Each time you create an object, a new set of instance variables comes into existence
which defines the characteristics of that object.
If you want to create an object of the class and have the reference variable associated
with this object, you must also allocate memory for the object by using the new
operator.
•Inheritance.
•Encapsulation.
•Abstraction.
•Polymorphism.
•Modularity
•Reusability
03/15/2024 51
• A primary feature of object-oriented
programming along with encapsulation and
polymorphism
03/15/2024 52
• OOP languages provide specific mechanisms for defining
inheritance relationships between classes.
• Derived (Child) Class - a class that inherits
characteristics of another class.
• Base (Parent) Class - a class from which characteristics
are inherited by one or more other classes.
• A derived class inherits data and function members from
03/15/2024
ALL of its base classes. 53
class B extends A { ….. } A <<class>>
A super class
B sub class B <<class>>
03/15/2024 54
• Extends keyword signifies that properties of the
super class are extended to sub class
• Sub class will not inherit private members of super
class
03/15/2024 55
Types of inheritance.
1. Single Inheritance
When a class extends another one class only then
we call it a single inheritance.
03/15/2024 56
• Single Inheritance example program in Java
Class A
{
public void methodA()
{
System.out.println("Base class method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
03/15/2024 obj.methodB(); //calling local method 57
}}
2) Multiple Inheritance
03/15/2024 58
3) Hierarchical Inheritance
In such kind of inheritance one class is inherited
by many sub classes.
03/15/2024 59
Example of hierarchical inheritance
Class D extends A
Class A {
{ public void methodD()
public void methodA() {
System.out.println("method of Class D");
{
}
System.out.println("method of Class A");
}
} Class MyClass
} {
class B extends A public void methodB()
{ public void methodB() {
{ System.out.println("method of Class B");
}
System.out.println("method of Class B");
public static void main(String args[])
}
{
} B obj1 = new B();
Class C extends A C obj2 = new C();
{ D obj3 = new D();
public void methodC() obj1.methodA();
{ obj2.methodA();
obj3.methodA(); } }
System.out.println("method of Class C");
}
}
03/15/2024 } 60
• The above would run perfectly fine with no
errors and the output would be –
method of Class A
method of Class A
method of Class A
03/15/2024 61
• 4) Multilevel Inheritance
• Multilevel inheritance refers to a mechanism in
OO technology where one can inherit from a
derived class, thereby making this derived class
the base class for the new class.
03/15/2024 62
Multilevel Inheritance example program in Java
Class X Class Z extends Y
{ {
public void methodX() public void methodZ()
{
{
System.out.println("class Z method");
System.out.println("Class X
}
method");
public static void main(String args[])
}
{
}
Z obj = new Z();
Class Y extends X obj.methodX(); //calling grand parent
{ class method
public void methodY() obj.methodY(); //calling parentclass
{ method
System.out.println("class Y obj.methodZ(); //calling localmethod
method"); }
} }
03/15/2024 } 63
5) Hybrid Inheritance
• In simple terms you can say that Hybrid inheritance
is a combination of Single and Multiple inheritance.
03/15/2024 64
• Member Access and Inheritance
Although a subclass includes all of the members of its super class, it cannot
access those members of the super class that have been declared as private.
For example, consider the following simple class hierarchy:
03/15/2024 65
• // Create a superclass.
class A {
int i; // public by default class Access {
private int j; // private to A public static void
void setij(int x, int y) { main(String args[]) {
i = x;
j = y;
B subOb = new B();
} subOb.setij(10, 12);
} subOb.sum();
// A's j is not accessible here.
System.out.println("Total is
class B extends A {
int total;
" + subOb.total);
void sum() { }
total = i + j; // ERROR, j is not }
accessible here
}
03/15/2024 } 66
Using super
• Whenever a subclass needs to refer to its immediate
super class, it can do so by use of the keyword super.
super has two general forms.
The first calls the super class’ constructor.
super();
super(<parameter-list>);
The second is used to access a member of the super
class that has been hidden by a member of a subclass.
super.<super class instance variable/Method>
03/15/2024 67
class A class B extends A
{ {
private int a; private int b;
A( int a) private double c;
{ B(int a,int b,double c)
this.a =a; {
System.out.println("This is constructor of super(a);
class A"); this.b=b;
} this.c=c;
void print() System.out.println("This is constructor of
{ class B");
System.out.println("a="+a); }
} void show()
void display() {
{ print();
System.out.println("hello This is Display in System.out.println("b="+b);
A"); System.out.println("c="+c);
} }
} // End of class A } // End of class B
03/15/2024 68
•Inheritance.
•Encapsulation.
•Abstraction.
•Polymorphism.
•Modularity
•Reusability
03/15/2024 74
• Polymorphism allows us to write generic, reusable code more
easily
03/15/2024 75
Method overloading
• Defining two or more methods within the same class that
share the same name, as long as their parameter
declarations are different 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.
03/15/2024 76
Ex
public class Overloading
{
int square(int intVal)
{
System.out.println("Called square with argument: "+intVal);
return intVal*intVal;
}
double square(double doubleVal)
{
System.out.println("Called square with argument: "+doubleVal);
return doubleVal*doubleVal;
}
}
class OverloadingTest
{
public static void main(String[] args)
{
Overloading first=new Overloading();
System.out.println("Square of integer 8 equals to "+first.square(8));
System.out.println("Square of double 8.5 equals to "+first.square(8.5));
}
}
03/15/2024 77
Overloaded method declarations with identical
signatures cause compilation errors, even if the return
types are different.
03/15/2024 78
Constructor overloading
• Constructors are methods that can be overloaded,
just like any other method in a class.
• Constructors having the same name with different
parameter list is called constructor overloading.
03/15/2024 79
• class Box {
double width; Ex class OverloadCons {
double height;
double depth; public static void main(String args[]) {
Box(double w, double h, double d) { // create boxes using the various constructors
width = w; Box mybox1 = new Box(10.0, 20.0, 15.0);
height = h; Box mybox2 = new Box();
depth = d; Box mycube = new Box(7.0);
}
double vol;
// constructor used when no dimensions specified
// get volume of first box
Box() {
width = -1; // use -1 to indicate vol = mybox1.volume();
height = -1; // an uninitialized System.out.println("Volume of mybox1 is " +
depth = -1; // box vol);
} // get volume of second box
// constructor used when cube is created vol = mybox2.volume();
Box(double len) { System.out.println("Volume of mybox2 is " +
width = height = depth = len; vol);
} // get volume of cube
// compute and return volume
vol = mycube.volume();
double volume() {
return width * height * depth;
System.out.println("Volume of mycube is " +
}
vol);
} }
03/15/2024 } 80
• Method Overriding
03/15/2024 81
// display k – this overrides show() in
// Method overriding.
A
class A {
void show() {
int i, j;
A(int a, int b) { System.out.println("k: " + k);
i = a; }
j = b; }
} class Override {
// display i and j public static void main(String args[]) {
void show() { B subOb = new B(1, 2, 3);
System.out.println("i and j: " + i + " " + subOb.show(); // this calls show() in B
j);
}
}
}
}
The output produced by this program
class B extends A {
is shown here:
int k;
k: 3
B(int a, int b, int c) {
super(a, b);
03/15/2024 k = c; 82
}
•Inheritance.
•Encapsulation.
•Abstraction.
•Polymorphism.
•Modularity
•Reusability
03/15/2024 87
• Categories of operators are as follows:
1. Arithmetic operators
2. Logical operators
3. Relational operators
4. Assignment operators
5. Conditional operators
03/15/2024 89
• Operator Result
+ Addition
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
–= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
03/15/2024 90
–– Decrement
• Example
// Demonstrate the basic arithmetic
// arithmetic using doubles
operators.
class BasicMath { System.out.println("\nFloating Point
public static void main(String args[]) Arithmetic");
{ double da = 1 + 1;
// arithmetic using integers double db = da * 3;
System.out.println("Integer Arithmetic");
double dc = db / 4;
int a = 1 + 1;
int b = a * 3;
double dd = dc - a;
int c = b / 4; double de = -dd;
int d = c - a; System.out.println("da = " + da);
int e = -d; System.out.println("db = " + db);
System.out.println("a = " + a);
System.out.println("dc = " + dc);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("dd = " + dd);
System.out.println("d = " + d); System.out.println("de = " + de);
System.out.println("e = " + e); }
}
03/15/2024 91
2. Increment and Decrement Operators:
/ Demonstrate ++ and --.
class IncDec {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
03/15/2024 92
}
3. Relational operators
• The relational operators determine the relationship
that one operand has to the other.
• Operator Result
• == Equal to
• != Not equal to
• > Greater than
• < Less than
• >= Greater than or equal to
• <= Less than or equal to
03/15/2024 93
4. Boolean logical operators
• The Boolean logical operators operate only on
boolean operands.
• All of the binary logical operators combine
two boolean values to form a resultant
boolean value.
03/15/2024 94
• Operator Result
& Logical AND
| Logical OR
== Equal to
!= Not equal to
03/15/2024 95
5. The Assignment operator
• The assignment operator is the single
equal sign, =.
int x, y, z;
x = y = z = 100; // set x, y, and z to
100
03/15/2024 96
6. The Bite Wise Operator
• Java defines several bitwise operators which can be applied to the integer types,
• Operator Result
• | Bitwise OR
• ^ Bitwise exclusive OR
03/15/2024 99
7. The ? Operator(conditional operator)
• General form:
expression1 ? expression2 : expression3 ;
Here, expression1 can be any expression that evaluates to a boolean value. If
expression1 is true, then expression2 is evaluated; otherwise, expression3 is
evaluated.
// Demonstrate ?.
class Ternary {
public static void main(String args[]) {
int i, k;
i = 10;
k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
i = -10;
k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
03/15/2024 100
}
End of Chap 4.
10 Q!!!!!
If Any ??????? ?
03/15/2024 101