Oops Unit Ii
Oops Unit Ii
2.1 INHERITANCE
When a class extends a class, which extends another class then this is called multilevel
inheritance. For example class C extends class B and class B extends class A then this type of
inheritance is known as multilevel inheritance.
Syntax:
class SuperClass
{
//body of the class
}
class SubClassOne extends SuperClass
{
//body of the class
}
class SubClassTwo extends SubClassOne
{
//body of the class
}
Example
class A public void methodC()
{ {
public void methodA() System.out.println("class C method");
{ }
System.out.println("Class A method"); }
}
} class Demo {
class B extends A public static void main(String args[])
{ {
public void methodB() C obj = new C();
{ obj.methodA(); OUTPUT:
System.out.println("class B method"); obj.methodB(); Class A Method
} obj.methodC(); Class B Method
} } Class C Method
class C extends B }
{
void displaymethodA()
Example 2 {
import java.io.*; System.out.println("Name: " + name);
import java.util.Scanner; System.out.println("Register Number: " + regno);
}
class StudentBasicInfo }
{ class StudentHSCMarks extends StudentBasicInfo
int regno; {
String name; int tamil, english, maths, biology, phy, chem;
Scanner sc=new Scanner(System.in); void getmethodB()
void getmethodA() {
{ getmethodA();
System.out.println("Enter the Name: "); System.out.println("Enter the Marks: ");
name=sc.next(); tamil= sc.nextInt();
System.out.println("Enter the Register Number: "); english= sc.nextInt();
regno= sc.nextInt(); maths= sc.nextInt();
} biology= sc.nextInt();
phy= sc.nextInt();
chem= sc.nextInt(); {
} StudentAvg obj = new StudentAvg();
} obj.getmethodB();
obj.calculate();
class StudentAvg extends StudentHSCMarks obj.displaymethodB();
{ }
int total; }
float avg;
INPUT:
void calculate()
{ Enter the Name: Raj
total=tamil+english+maths+biology+phy+chem; Enter the Register Number: 12345
avg=total/12; Enter the Marks:
} 120
120
void displaymethodB() 120
{ 120
displaymethodA(); 120
System.out.println("Total: " +total); 120
System.out.println("Average: " + avg);
} OUTPUT:
} Name: Raj
Register Number: 12345
class StudDemo Total: 720
{ Average: 60.00
public static void main(String args[])throws
IOException
2. boolean equals(Object obj) This method indicates whether some other object is "equal to"
this one.
3. protected void finalize() This method is called by the garbage collector on an object when
garbage collection determines that there are no more references to the object.
4. Class<?> getClass() This method returns the runtime class of this Object.
5. int hashCode() This method returns a hash code value for the object.
6. void notify() This method wakes up a single thread that is waiting on this object's monitor.
7. void notifyAll() This method wakes up all threads that are waiting on this object's monitor.
8. String toString() This method returns a string representation of the object.
9. void wait() This method causes the current thread to wait until another thread invokes the
notify() method or the notifyAll() method for this object.
10. void wait(long timeout) This method causes the current thread to wait until either another
thread invokes the notify() method or the notifyAll() method for this object, or a specified
amount of time has elapsed.
11. void wait(long timeout, int nanos) This method causes the current thread to wait until another
thread invokes the notify() method or the notifyAll() method for this object, or some other
thread interrupts the current thread, or a certain amount of real time has elapsed.
Example: Object Clone
import java.util.GregorianCalendar; Output
public class ObjectDemo { Mon Sep 17 04:51:41 EEST 2017
public static void main(String[] args)Mon { Sep 17 04:51:41 EEST 2017
GregorianCalendar cal = new GregorianCalendar();
GregorianCalendar y = (GregorianCalendar) cal.clone();
System.out.println("" + cal.getTime());
System.out.println("" + y.getTime());
}
}
Example: boolean equals(Object obj)
public class ObjectDemo {
public static void main(String[] args) {
Integer x = new Integer(50); Output
Float y = new Float(50f); false
System.out.println("" + x.equals(y)); true
System.out.println("" + x.equals(50));
}
}
Example: protected void finalize()
import java.util.*;
public class ObjectDemo extends GregorianCalendar {
public static void main(String[] args) {
try {
ObjectDemo cal = new ObjectDemo();
System.out.println("" + cal.getTime()); Output
System.out.println("Finalizing...");Sat Sep 22 00:27:21 EEST 2012
cal.finalize(); Finalizing...
System.out.println("Finalized."); Finalized.
} catch (Throwable ex) {
ex.printStackTrace();
} }}
void display2()
{
System.out.println("This is from interface B ");
}
}
class Demo
{
public static void main(String args[])throws IOException
{
C obj = new C();
obj.display1();
obj.display1();
}}
2.6.1EXTENDING INTERFACES
An interface can extend another interface in the same way that a class can extend another class. The
extends keyword is used to extend an interface, and the child interface inherits the methods of the
parent interface.
Example:
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class TestInterface4 implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
class NestedInterfaceDemo1
implements MyInterfaceA.MyInterfaceB{
public void myMethod(){
System.out.println("Nested interface method");
}
2.10 STRINGS
Generally, string is a sequence of characters. But in java, string is an object that represents a
sequence of characters. String class is used to create string object.
Java String provides a lot of concepts that can be performed on a string such as compare, concat,
equals, split, length, replace, compareTo, intern, substring etc
Strings can be create using string class.
Example: String s1="Welcome";
Strings can also be created using constructors
String s=new String("Welcome");
2.10.1 Java String class methods
The java.lang.String class provides many useful methods to perform operations on sequence
of char values.
`
No. Method Description
1 char charAt(int index) returns char value for the particular index
2 int length() returns string length
3 String substring(int beginIndex) returns substring for given begin index
returns substring for given begin index and
4 String substring(int beginIndex, int endIndex)
end index
5 boolean equals(Object another) checks the equality of string with object
6 boolean isEmpty() checks if string is empty
7 String concat(String str) concatinates specified string
replaces all occurrences of specified char
8 String replace(char old, char new)
value
String replace(CharSequence old, replaces all occurrences of specified
9
CharSequence new) CharSequence
returns trimmed string omitting leading and
10 String trim()
trailing spaces
11 String toLowerCase(String str) Covert lowercase to uppercase.
12 String toUpperCase(String str) Covert uppercase to lowercase.
Example:
import java.io.*;
import java.lang.String;
public class Demo OUTPUT:
{ C:\Program Files\Java\jdk1.5.0\bin>java Demo
public static void main(String[] args) hello java
HELLO JAVA
{
11
String obj = " Hello Java"; Hello Java
System.out.println(obj.toLowerCase()); 11
System.out.println(obj.toUpperCase()); J
System.out.println(obj.length()); Hello World
System.out.println(obj.trim()); Jav
System.out.println(obj.length());
System.out.println(obj.charAt(7));
System.out.println(obj.replace("Java", "World"));
System.out.println(obj.substring(6, 10));
}
}
2.10.2 STRING BUFFER CLASS
The StringBuffer is a class which is alternative to the String class. But StringBuffer class is more flexible to
use than String class.That means, using StringBuffer we can insert some component to the existing string or
modify the existing string but in case of String class once the string is defined then it remains fixed.
Java StringBuffer class methods
No. Method Description
1 append(String str) Appends the String to the buffer
2 capacity() It returns the capacity of the string buffer
3 insert(int offset, char ch) It insert the character at the position specified by the offset
replace(int Start,int end,
4 It replaces the character specified by the new string
String str)
It deletes the character from the string specified by the starting
5 delete(int start,int end)
and ending index.
6 reverse() The character sequence is reversed
7 length() It returns the length of the string buffer
It returns a specific character from the sequence which is
8 charAt(int index)
specified by the index.
The character specified by the index from the stringbuffer is set
9 setCharAt(int index, char ch)
to ch
Write a java program to create a student examination database system that prints the mark
sheet of students. Input student name, marks in 6 subjects . This mark should be between 0 and
100
If the average of marks is >= 80 then print Grade ‘A’.
If the average is <80 and >=60 then prints Grade ‘B’.
If the average is <60 and >=40 then prints Grade ‘C’
Else print Grade ‘D’.
import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
int mark[] = new int[6];
int i;
float sum=0, avg;
Scanner scan = new Scanner(System.in);
avg = sum/6;
PART-B
1. Define Polymorphism. [Marks 2] N/D 2010
2. Explain the concept of polymorphism with a example. (8) N/D 2011
3. What is meant by polymorphism? Discuss the types of polymorphism with suitable examples. (16)
N/D 2014
12. Write briefly on Abstract classes with an example. [Marks 6] N/D 2010
13. Write in detail about abstract class. (8) N/D 2012
14. What is abstract class? Write a program for abstract class example. (8) A/M 2013
15. Explain the concept of abstract class with example. (8) N/D 2013
16. Explain briefly abstract class and method. (8) A/M 2014
17. What is abstract class? State the purpose of it. (8) N/D 2015
18. Write in detail about the following: i)Abstract classes (8) A/M 2016
19. Write short notes on abstract classes (8) A/M 2018
20. Describe the finalize method with an example (8) A/M 2011
21. Write note on final keyword. (4) N/D 2013
22. What is final keyword explain with an example. (8) A/M 2015
23. Describe the need to declare the method as final (4) A/M 2018
24. Explain dynamic binding and final keyword with an example. (16) N/D 2012
25. Explain Dynamic Binding and Final keyword with example. (16) A/M 2016
26. Explain the concept of dynamic binding with a suitable example. (8) A/M 2018
27. Differentiate method overloading and method overriding. Explain both with an example. A/M
2012
28. State the properties of interface. (8) A/M 2011
29. Write in detail about interface (8) N/D 2012`
30. What is meant by interface? How it is declared and implement in Java. Give example. (12) N/D
2013
31. Explain in detail about the term interface and list out its properties. (8) A/M 2015
32. Describe the concept of reflection and interface with example. (16) N/D 2015
33. Write in detail about the following:. ii)Interface (16) A/M 2016
34. Explain how interface are handled in java with suitable example. (8) A/M 2018
35. Explain the following with examples: The clone able interface & The property interface. N/D
2010
36. What is meant by object cloning? Explain with an example. (8) N/D 2011
37. Explain the concept of object cloning and inner classes with example. (16) N/D 2014
38. What is a static Inner class? Explain with example. [Marks 8] N/D 2010
39. Discuss in detail about inner class, with its usefulness. (8) N/D 2011
40. Define inner classes. How to access object state using inner classes? Give an example. (8) A/M
2013
41. Discuss the object and inner classes with example. (8) N/D 2013
42. Describe in detail about inner classes in Java. (8) A/M 2014
43. Illustrate the concept of inner class with example. (8). N/D 2015
44. Explain the following in Strings: Concatenation & Substrings. [Marks 4] N/D 2010
45. Explain any four methods available in string handling. [Marks 4] N/D 2010
46. Explain any four string operation in java with an example (8) N/D 2011
47. Explain string handling classes in java with example. (16) A/M 2012
48. Discuss about string. (8) N/D 2012
49. Explain java building string function with an example. (8) A/M 2013
50. Write a Java program to reverse the given number. (6) N/D 2013
51. String in Java (3) A/M 2015 A/M 2016
2017 Regulation
N/D 2018
1. Define Inheritance. With diagrammatic illustration and java programs illustrate the different types
of inheritance with an example. (13)
2. Write a java program to create a student examination database system that prints the mark sheet of
students. Input student name, marks in 6 subjects . This mark should be between 0 and 100
If the average of marks is >= 80 then print Grade ‘A’.
If the average is <80 and >=60 then prints Grade ‘B’.
If the average is <60 and >=40 then prints Grade ‘C’
Else print Grade ‘D’.
A/M 2019
3. Explain hierarchical and multi level inheritance supported by Java and demonstrate the execution
order of constructors in these types. (13)
4. Explain simple interface and nested interface with example. (7)
5. Presented a detailed comparison between classes and interfaces. (6)