Oops Unit-2
Oops Unit-2
Oops Unit-2
• Sub classes
• Protected members
E:\javapgm>java Derived4
From Derived, a=20
CS8392 OBJECT ORIENTED PROGRAMMING 19
From Base, a=10
super keyword invokes base class
method
class A class Derived5 extends B
{ {
void display() void display()
{ {
super.display();
System.out.println("This is class A");
System.out.println("This is class Derived5");
} }
} public static void main(String args[])
class B extends A {
{ Derived5 d=new Derived5();
d.display();
void display()
}
{ }
super.display();
System.out.println("This is class B");
}
}
E:\javapgm>java Derived5
This is class A
This is class B
CS8392 OBJECT ORIENTED PROGRAMMING 20
This is class Derived5
Super() Method
• It is used to invoke super or base class
constructor explicitly from derived class
• Note:
– If a subclass constructor does not explicitly
invoke a superclass constructor, the Java
compiler automatically inserts a call to the no-
argument constructor of the superclass.
• Run-time polymorphism
E:\javapgm>javac Derived1.java
E:\javapgm>java Derived1
This is base class constructor
This is derived class constructor 23
CS8392 OBJECT ORIENTED PROGRAMMING
This is base class method
Method Overriding
//Method overriding
class Base
{
void display()
{
System.out.println("This is base class");
}
}
class Derived6 extends Base
{
void display()
{
System.out.println("This is derived class");
}
public static void main(String args[])
{
Derived6 d=new Derived6();
d.display();
} E:\javapgm>java Derived6
This is derived class
}
CS8392 OBJECT ORIENTED PROGRAMMING 24
ABSTRACT CLASSES AND
METHODS
“Prevents Inheritance”
CS8392 OBJECT ORIENTED PROGRAMMING 34
Final Classes
final class A
{
// ...
}
// The following class is illegal.
// ERROR! Can't subclass A
class B extends A
{
// ...
}
2 Multiple Inheritance Multiple Inheritance is not supported. Interface supports Multiple Inheritance.
final, non-final, static and non-static Only static and final variables are
3 Supported Variables
variables supported. permitted.
8 Constructor A class can have constructor methods. Interface can not have a constructor.
E:\javapgm>java GetClassDemo
class java.lang.Integer
• Steps:
E:\javapgm>java ArrayListModify
Before Change:[Volvo, BMW, Ford]
66
After Change:[Volvo, Honda, Ford]
//Removing Elements using remove() and removeAll()
import java.util.ArrayList;
public class ArrayListRemove
{
public static void main(String[] args)
{
ArrayList<String> cars = new ArrayList<>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
System.out.println("Before Remove:"+cars);
cars.remove(1);
System.out.println("After Remove:"+cars);
cars.removeAll(cars);
System.out.println("After RemoveAll:"+cars);
}
}
E:\javapgm>java ArrayListRemove
Before Remove:[Volvo, BMW, Ford]
After Remove:[Volvo, Ford]
67
After RemoveAll:[]
//Sorting Elements
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListSort
{
public static void main(String[] args)
{
ArrayList<String> cars = new ArrayList<>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
System.out.println("Before Sorting:"+cars);
Collections.sort(cars);
System.out.println("After Sorting:"+cars);
}
}
E:\javapgm>java ArrayListSort
Before Sorting:[Volvo, BMW, Ford] 68
After Sorting:[BMW, Ford, Volvo]
//Length of ArrayList
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListSize
{
public static void main(String[] args)
{
ArrayList<String> cars = new ArrayList<>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
System.out.println("Length of ArrayList:"+cars.size());
}
}
E:\javapgm>java ArrayListSize
Size of ArrayList:3 69
Converting ArrayList
import java.util.ArrayList;
import java.util.Arrays;
class ArrayListConvert
{
public static void main(String args[])
{
String[] names={"raja","kumar","senthil"};
ArrayList<String> names1=new ArrayList<>(Arrays.asList(names));
System.out.println("Elements of ArrayList:"+names1);
String names2=names1.toString();
System.out.println("Elements of String:"+names2);
}
} E:\javapgm>java ArrayListConvert
Elements of ArrayList:[raja, kumar, senthil]
Elements of String:[raja, kumar, senthil]
CS8392 OBJECT ORIENTED PROGRAMMING 70
Difference between Arrays and ArrayList
Arrays ArrayList
Array is static in size. ArrayList is dynamic in size.
ArrayList is a variable-length data
An array is a fixed-length data
structure. It can be resized itself when
structure.
needed.
It is mandatory to provide the size of
We can create an instance of ArrayList
an array while initializing it directly or
without specifying its size
indirectly.
We cannot store primitive type in
An array can store
ArrayList. It automatically converts
both objects and primitives type.
primitive type to object.
Array provides a length variable which ArrayList provides the size() method to
denotes the length of an array. determine the size of ArrayList.
E:\javapgm>java StringLength
string length is: 4
string length is: 11
E:\javapgm>java StringCompare
Result of Comparison: -6
E:\javapgm>java StringConcat
After of Concatenation: Java Programming
E:\javapgm>java StringEmpty
Result of empty check on S1: true
Result of empty check on S2: false
E:\javapgm>java StringTrim
Without trim:Java Programming
With trim:JavaProgramming
E:\javapgm>java StringLowerUpper
java
PROGRAMMING
E:\javapgm>java StringEqual
false
true
E:\javapgm>java StringContains
true
false
//Replacing String
String s2=s1.replace("Java","Python");
//Replacing character
String s3=s2.replace("P","p");
System.out.println(s3);
}
}
E:\javapgm>java StringReplace
python programming
CS8392 OBJECT ORIENTED PROGRAMMING 82
//10. substring(int beginIndex, int endIndex)
//return a substring from beginIndex to endIndex-1 position
class SubString
{
public static void main(String args[])
{
String s1="Programming";
System.out.println(s1.substring(0,4));
}
}
E:\javapgm>java SubString
Prog
E:\javapgm>java StringEndsWith
true
E:\javapgm>java StringIndexof
10