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

Java Code Samples

The document discusses implementing polymorphism and inheritance in Java using methods. It provides a code example of a parent and child class where the child class overrides the parent's method. The main method instantiates the child class and calls its overridden method.

Uploaded by

DurbaGhosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views

Java Code Samples

The document discusses implementing polymorphism and inheritance in Java using methods. It provides a code example of a parent and child class where the child class overrides the parent's method. The main method instantiates the child class and calls its overridden method.

Uploaded by

DurbaGhosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Write a program to implement polymorphism, inheritance using methods in Java.

class X
{
X()
{
System.out.println(Inside super class parameterized constructor);
fun();
}
void fun()
{
System.out.println(Parent);
}
}
class Y extends X
{
Y()
{
System.out.println(Inside child class parameterized constructor);
}
void fun()
{
System.out.println(Child);
}
}
class Demo extends Y
{
public static void main(String args[])
{
Y d=new Y();
d.fun();
}
}

OUTPUT

Inside super class parameterized constructor


Parent
Inside child class parameterized constructor
Child
Write a program to draw an image using applet

import java.awt.*;
import java.applet.*;

public class DisplayImage extends Applet


{
Image img;
public void init()
{
img = getImage(getDocumentBase(),"valley.jpg");
}
public void paint(Graphics g)
{
g.drawImage(img, 30,30, this);
}
}

HTML File for Applet

<html>
<body>
<applet code="DisplayImage.class" width="300" height="300">
</applet>
</body>
</html>

OUTPUT
Write a program to read and write data in a file in Java

import java.io.*;
class FileStreamsReadnWrite
{
public static void main(String args[])
{
try
{
File stockInputFile = new File("fileone.txt");
File stockOutputFile = new File("filetwo.txt");
FileInputStream fis = new FileInputStream(stockInputFile);
FileOutputStream fos = new FileOutputStream(stockOutputFile);
int count;
while ((count = fis.read()) != -1)
{
fos.write(count);
}
fis.close();
fos.close();
}
catch (FileNotFoundException e)
{
System.err.println("FileStreamsReadnWrite: " + e);
}
catch (IOException e)
{
System.err.println("FileStreamsReadnWrite: " + e);
}
}
}

You might also like