[Go to site: main page, start]

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

Java

The document contains multiple Java programs demonstrating key concepts such as methods, constructors, encapsulation, inheritance, interfaces, abstraction, and polymorphism. Each program illustrates a specific concept with a simple example, showcasing how to implement these features in Java. The examples include class definitions, method implementations, and the use of main methods to execute the code.

Uploaded by

noorcomputer1000
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)
2 views3 pages

Java

The document contains multiple Java programs demonstrating key concepts such as methods, constructors, encapsulation, inheritance, interfaces, abstraction, and polymorphism. Each program illustrates a specific concept with a simple example, showcasing how to implement these features in Java. The examples include class definitions, method implementations, and the use of main methods to execute the code.

Uploaded by

noorcomputer1000
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

1.

Java Program Using Method


class MethodExample {

// Method to display message


void display() {
[Link]("Hello, this is a method.");
}

public static void main(String[] args) {


MethodExample obj = new MethodExample();
[Link]();
}
}

2. Java Program Using Constructor


class Student {
String name;

// Constructor
Student() {
name = "John";
}

void show() {
[Link]("Student Name: " + name);
}

public static void main(String[] args) {


Student s = new Student();
[Link]();
}
}

3. Java Program Using Encapsulation


class Employee {
private String name;

// Setter
public void setName(String name) {
[Link] = name;
}

// Getter
public String getName() {
return name;
}
public static void main(String[] args) {
Employee emp = new Employee();
[Link]("Alice");
[Link]("Employee Name: " + [Link]());
}
}
4. Java Program Using Inheritance
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}

class Dog extends Animal {


void bark() {
[Link]("Dog barks");
}

public static void main(String[] args) {


Dog d = new Dog();
[Link]();
[Link]();
}
}

5. Java Program Using Interface


interface Animal {
void sound();
}

class Dog implements Animal {

public void sound() {


[Link]("Dog barks");
}

public static void main(String[] args) {


Dog d = new Dog();
[Link]();
}
}

6. Java Program Using Abstraction


abstract class Animal {

abstract void sound();

void sleep() {
[Link]("Animal is sleeping");
}
}

class Dog extends Animal {

void sound() {
[Link]("Dog barks");
}
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
}
}

7. Java Program Using Polymorphism


class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}

class Dog extends Animal {


@Override
void sound() {
[Link]("Dog barks");
}
}

public class PolymorphismExample {

public static void main(String[] args) {


Animal a = new Dog(); // Upcasting
[Link](); // Calls Dog's sound()
}
}

You might also like