OOP JAVA M2 Ktunotes - in
OOP JAVA M2 Ktunotes - in
Module 2
Inheritance - Super Class, Sub Class, The Keyword super, protected Members, Calling Order
of Constructors, Method Overriding, the Object class, Abstract Classes and Methods, using
final with Inheritance.
https://www.youtube.com/channel/UCTBjhZB3ni3_NCT9M0XNOpQ
byte age = 10 ;
To find out how many elements an array has, use the length property:
String[] cars = {"Volvo", "BMW", "Ford"};
System.out.println(cars.length );// Outputs 3
char[] ch={'h','e','l','l','o'};
String s=new String(ch);
This is same as that of
String s="hello";
Downloaded from Ktunotes.in
String literals vs objects
● String objects are created using new keyword
● String s1 = new String("Java");
● String s2 = new String("Java");
● System.out.println(s1 == s2); //false
● String literals are created by placing the string in
double quotes.
● String s3 = "Java";
● String s4 = "Java";
● System.out.println(s3 == s4);//true
● Here == (equality) is not checking the content of the
stings.
● String literals are stored in a special area called String
Constant Pool in program heap.
Downloaded from Ktunotes.in
Immutability of string
● Strings are constant; their values cannot be changed(immutable) after they
are created. StringBuffer support mutable strings.
● String objects are immutable, so they can be shared.
● In Java, String is a final and immutable class.
● It cannot be inherited, and once created, we can not alter the object.
result
String s1 = "Hello";
String s2 = "Hello";
s1.equals(s2); // true
String s1 = "Hello";
String s2 = "HELLO" ;
s1.equals(s2); // false
s1.equalsIgnoreCase(s2); // true
For example
A = 5, B =2
C = A%B
C=?
Downloaded from Ktunotes.in
++ / -- ● A++ is same as A = A + 1
● A-- is same as A = A - 1
● ++A is pre increment, change
first then use the value when
there is an assignment.
● A++ post increment
● Use the value then change
(when there is an assignment)
1. if statement
2. if-else statement
3. nested if statement
4. if-else if statement (if else if ladder)
5. switch statement
break statement
continue statement
● Causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating.
● Has two forms: labeled and unlabeled.
return statement
Downloaded from Ktunotes.in
break;
OBJECT
● When you assign null or another value to an object reference, you are not making any
change to the object. Change is only to the reference (link).
b1 = null; // removes the link between the object and b1.
•Thus, value of mybox1 object’s width, height, and depth will be set as 10, 20,
and 15 respectively.
Downloaded from Ktunotes.in
Check the output of this program??
● Instance variables are hidden
(not accessible) due to the
function parameters with same
name.
double width
double length;
double height
Box(double width, double height, double length)
{
this.width = width;
this.length = length;
this. height = length;
}} Downloaded from Ktunotes.in
Method Overloading
● It is possible to define two or more methods with same name within the same
class, but their parameter declarations should be different.
● This is called method overloading.
● This is a form of polymorphism (many forms)
● Overloaded methods must differ in the type and/or number of their
parameters. (return types is not significant.)
● When an overloaded method is invoked, Java uses the type and/or number of
arguments to determine which version of the overloaded method to actually
call.
–
Downloaded from Ktunotes.in
Overloading -through automatic type conversions
Effect:
PRIVATE
PROTECTED DEFAULT /
PACKAGE
PUBLIC
PRIVATE
DEFAULT /
PACKAGE
PROTECTED
PUBLIC
When a class is declared as final then it cannot be subclassed i.e. no any other
class can extend it
● GeeksforGeeks - www.geeksforgeeks.org
● Wikipedia - www.wikipedia.org
● Tutorialspoint - www.tutorialspoint.com