[Go to site: main page, start]

0% found this document useful (0 votes)
61 views3 pages

Java Code Samples for Key Concepts

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)
61 views3 pages

Java Code Samples for Key Concepts

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

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

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

OUTPUT

Inside super class parameterized constructor


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

import [Link].*;
import [Link].*;

public class DisplayImage extends Applet


{
Image img;
public void init()
{
img = getImage(getDocumentBase(),"[Link]");
}
public void paint(Graphics g)
{
[Link](img, 30,30, this);
}
}

HTML File for Applet

<html>
<body>
<applet code="[Link]" width="300" height="300">
</applet>
</body>
</html>

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

import [Link].*;
class FileStreamsReadnWrite
{
public static void main(String args[])
{
try
{
File stockInputFile = new File("[Link]");
File stockOutputFile = new File("[Link]");
FileInputStream fis = new FileInputStream(stockInputFile);
FileOutputStream fos = new FileOutputStream(stockOutputFile);
int count;
while ((count = [Link]()) != -1)
{
[Link](count);
}
[Link]();
[Link]();
}
catch (FileNotFoundException e)
{
[Link]("FileStreamsReadnWrite: " + e);
}
catch (IOException e)
{
[Link]("FileStreamsReadnWrite: " + e);
}
}
}

You might also like