0% found this document useful (0 votes)
11 views

Session14 16

The document contains code snippets and explanations of various Java concepts like inheritance, interfaces, generics, collections like ArrayList, LinkedList, HashSet, TreeSet and maps. It discusses concepts like multiple inheritance in Java using interfaces, generic classes and methods, autoboxing/unboxing and usage of different collection classes like ArrayList, LinkedList, HashSet and TreeSet with examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Session14 16

The document contains code snippets and explanations of various Java concepts like inheritance, interfaces, generics, collections like ArrayList, LinkedList, HashSet, TreeSet and maps. It discusses concepts like multiple inheritance in Java using interfaces, generic classes and methods, autoboxing/unboxing and usage of different collection classes like ArrayList, LinkedList, HashSet and TreeSet with examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

SESSION 14):-

===============================

Slide 2):-
===========================================
EmployeeDAO dao = new EmployeeDAOMemoryImpl();

dao.delete(1);

The following code snippet shows the reference typed to EmployeeDAOMemoryImpl


class:

EmployeeDAOMemoryImpl dao = new EmployeeDAOMemoryImpl();

dao.delete(1);

Slide 3 & slide 7):-


------------------------------------

By default java does not support Multiple Inheritance.

ie. SUBCLASS cannot be created form multiple BASECLASSES. ( because of Diamond


problem in code. )

MULTIPLE INHERITANCE is supported in java through Interface .

Multiple Inheritance :- In java a class only extends from one base class and
implement multiple Interfaces.
--------------------

import java.io.*;

class Shape {

public void area()


{
System.out.println("area of shape");
}

interface Box {

public void perimeter();


}

class Rectangle extends Shape implements Box {

int l,b;

public Rectangle(int x, int y)


{
l = x;
b = y;
}

@Override
public void area()
{
System.out.println("area of rectangle = "+ (l*b) );

@Override
public void perimeter()
{
System.out.println("Perimeter of rectangle = "+ 2*(l+b) );

public class RectangleDemo


{
public static void main(String args[])
{
Rectangle ob = new Rectangle(10 , 20);

ob.area();
ob.perimeter();

Slide 22-24):-
========================================================
public class SpyCarWithSunRoof

{
private BasicCar car = new BasicCar();

private SpyCarAddon spyAddon = new SpyCarAddon();

private SunRoofAddon roofAddon = new SunRoofAddon();

public void start()


{
car.start();
}
// other forwarded methods
}

SESSION 16 ):-

------------------

Slide 4):-
===========================================

Generic class :-

public class CacheAny <T>

private T t;

public void add(T t){

this.t = t;

Public T get(){

return this.t;

Slide 5):-
===========================================
public static void main(String args[]){

CacheString myMessage = new CacheString();//Type

CacheShirt myShirt = new CacheShirt();//Type

//Generics

CacheAny<String> myGenericMessage = new CacheAny<String>();

CacheAny<Shirt> myGenericShirt = new CacheAny<Shirt>();

myMessage.add("Save this for me");//Type

myGenericMessage.add("Save this for me");


//Generic

Slide 11):-
===========================================
import java.util.*;
import java.io.*;

public class ArrayListDemo


{

public static void main(String args[])

List<Integer> partList = new ArrayList<>(3);

partList.add(new Integer(1111));

partList.add(new Integer(2222));

partList.add(new Integer(3333)); //ArrayList auto

partList.add(new Integer(4444)); //grows

System.out.println("First Part:" + partList.get(0)); // First item

System.out.println("partList : "+ partList );

partList.add(0, new Integer(5555));//Insert an item by index

System.out.println("partList : "+ partList );

System.out.println("size : "+ partList.size() );

Slide 12):-
==============================================

import java.io.*;
import java.util.*;

public class OldStyleArrayList

public static void main(String args[])

{
List partList = new ArrayList(3);

partList.add(new Integer(1111));

partList.add(new Integer(2222));

partList.add(new Integer(3333));

partList.add("Oops a string!"); // put comments to avoid Casting of strings to


Integer.

Iterator elements = partList.iterator();

while (elements.hasNext())

Integer partNumberObject =

(Integer)(elements.next()); // error? // can not convert a string into Integer


Object.

int partNumber = partNumberObject.intValue();

System.out.println("Part number: " + partNumber);

// If the preceding list was only for Integer objects, a runtime error would occur
on the following line:
// Integer partNumberObject = (Integer)(elements.next());

Slide 13 ):-

=======================================

import java.io.*;
import java.util.*;

public class GenericArrayList

public static void main(String args[])

List<Integer> partList = new ArrayList<>(3);


partList.add(new Integer(1111));

partList.add(new Integer(2222));

partList.add(new Integer(3333));

partList.add("Bad Data"); // compile error now

Iterator<Integer> elements = partList.iterator();

while (elements.hasNext())
{

Integer partNumberObject = elements.next();

int partNumber = partNumberObject.intValue();

System.out.println("Part number: " + partNumber);

Slide 15 ):-

=========================================================

import java.io.*;
import java.util.*;

public class GenericArrayList1

public static void main(String args[])

// ArrayList is dynamically growable array.

List<Integer> partList = new ArrayList<>(3);

// Auto boxing

partList.add(1111); // partList.add(new Integer(1111));

partList.add(2222);

partList.add(3333);
for (Integer partNumberObj:partList)
{
int partNumber = partNumberObj;//Auto unboxing

System.out.println("Part number: " + partNumber);

Slide 16):-

==========================================

import java.io.*;

import java.util.*;

public class AutoBox


{
public static void main(String[] args)
{

Integer intObject = new Integer(1);

int intPrimitive = 2;

Integer tempInteger;

int tempPrimitive;

tempInteger = new Integer(intPrimitive);

tempPrimitive = intObject.intValue();

tempInteger = intPrimitive; // Auto box

tempPrimitive = intObject; // Auto unbox

. . .

Slide 18):-

============================================
eg 1):-

// Program to sort the string objects


import java.io.*;

import java.util.*;

public class SetExample {

public static void main(String[] args){

Set<String> set = new TreeSet<>();

set.add("one");

set.add("two");

set.add("three");

set.add("three"); // not added, only unique

for (String item:set){

System.out.println("Item: " + item);

// Program to sort the Integer values.

eg 2):-

import java.io.*;

import java.util.*;

public class SetExample1 {

public static void main(String[] args){

Set<Integer> set = new TreeSet<>();

set.add(2);

set.add(3);

set.add(1);

set.add(1); // not added, only unique

for (Integer item:set){

System.out.println("Item: " + item);

}
}

Slide 21):-

===============================================================

import java.util.*;
import java.io.*;

public class MapExample1


{
public static void main(String[] args)
{
Map <String, String> partList = new TreeMap<>();

partList.put("S001", "Blue Polo Shirt");

partList.put("S002", "Black Polo Shirt");

partList.put("H001", "Duke Hat");

partList.put("S002", "Black T- Shirt");//Overwrite value

Set<String> keys = partList.keySet();

System.out.println("=== Part List ===");

for (String key:keys)

System.out.println("Part#: "+ key + " " + partList.get(key));


}
}
}

You might also like