0% found this document useful (0 votes)
17 views12 pages

24java

Uploaded by

rathordeepak2685
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)
17 views12 pages

24java

Uploaded by

rathordeepak2685
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/ 12

1. WAP to illustrate constructor in java programming.

class Student

String name;

int age;

public void setname(String n,int a)

name=n;

age=a;

public void show()

System.out.println(name);

System.out.println(age);

Student()

System.out.println("constructor call");

Student(int asd)

System.out.println(asd);

public class Classexamstudent

public static void main(String[] args)


{

Student obj=new Student(6);

obj.setname("Ram",20);

obj.show();

#Output-
2. WAP to illustrate static member of class in java programming.
public class StaticExample
{
static int age;
static String name;
//This is a Static Method
static void display()
{
System.out.println("Age is: "+age);
System.out.println("Name is: "+name);
}
// This is also a static method
public static void main(String [] args)
{
age = 30;
name = "Steve";
display();
}
}
#Output-
3. WAP to illustrate inheritance(“is a” relationship) in java programming.
class Student2
{
int roll,marks;
String name;
void input()
{
System.out.println("enter roll no,name and marks");
}
}
class Ayag extends Student2
{
void show()
{
roll=12;
name="Ayush";
marks=90;
System.out.println(roll+" "+name+" "+marks);
}
}
public class Simpleinheritance
{
public static void main(String[] agrs)
{
Ayag s1=new Ayag();
s1.input();
s1.show();
}
}
#Output-
4. WAP to illustrate association in java programming.

// Class representing a Person


class Person {
private String name;

public Person(String name) {


this.name = name;
}

public String getName() {


return name;
}
}
// Class representing a Car
class Car {
private String model;
private Person owner; // Association: Car has an owner (Person)

public Car(String model, Person owner) {


this.model = model;
this.owner = owner;
}
public String getModel() {
return model;
}

public Person getOwner() {


return owner;
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person("Ram");
Car car = new Car("Toyota", person);

System.out.println(car.getOwner().getName() + " owns a " + car.getModel());


}
}
#Output-
5. WAP to illustrate aggregation(“Has a” relationship) in java programming.
class Operation{
int square(int n){
return n*n;
}
}

public class Aggregationforfile{


Operation op;//aggregation
double pi=3.14;

double area(int radius){


op=new Operation();
int rsquare=op.square(radius);//code reusability (i.e. delegates the method call).
return pi*rsquare;
}

public static void main(String args[]){


Aggregationforfile c=new Aggregationforfile();
double result=c.area(5);
System.out.println(result);
}
}
#Output-
6. WAP to illustrate inheritance(“Part of” relationship) in java programming.
class SolarSystem {
void abc()
{
System.out.println("8 planets");
}
}
class Earth extends SolarSystem {
void abd()
{
System.out.println("1 moon");
}
}
class Mars extends SolarSystem {
void abe()
{
System.out.println("2 moons");
}
}
class Moon extends Earth
{
void abf()
{ System.out.println("It is a natural satellite");
}
}
public class Inheri
{
public static void main(String args[])
{
Moon mo = new Moon();
Mars m = new Mars();
mo.abc();
mo.abd();
mo.abf();
m.abc();
m.abe();
}
}
#Output-
8. WAP to illustrate abstract class in java programming.
// Abstract class
abstract class Sunstar {
abstract void printInfo();
}

// Abstraction performed using extends


class Employee extends Sunstar {
void printInfo()
{
String name = "Avinash";
int age = 21;
float salary = 222.2f;

System.out.println(name);
System.out.println(age);
System.out.println(salary);
}
}
// Base class
public class Base {
public static void main(String args[])
{
Sunstar s = new Employee();
s.printInfo();
}
}
#Output-
9. WAP to illustrate interface in java programming.
interface Polygon {
void getArea(int length, int breadth);
}

// implement the Polygon interface


class Rectangle implements Polygon {

// implementation of abstract method


public void getArea(int length, int breadth) {
System.out.println("The area of the rectangle is " + (length * breadth));
}
}

public class Main {


public static void main(String[] args) {
Rectangle r1 = new Rectangle();
r1.getArea(5, 6);
}
}
#Output-
10. WAP to illustrate polymorphism in java programming.
class Student1
{
String name;
String n;
int age;
public void printdet(String name)
{
System.out.println(name);
}
public void printdet(int age)
{
System.out.println(age);
}
public void printdet(String n,int age)
{
System.out.println(n+" "+age);
}
}
public class Polymorphism
{
public static void main(String[] args)
{
Student1 s1=new Student1();
s1.printdet("Ram");
s1.printdet(19);
s1.printdet("Ram",19);
}
}
#Output-

You might also like