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

Java Programming Concepts Examples

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)
6 views12 pages

Java Programming Concepts Examples

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

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()

[Link](name);

[Link](age);

Student()

[Link]("constructor call");

Student(int asd)

[Link](asd);

public class Classexamstudent

public static void main(String[] args)


{

Student obj=new Student(6);

[Link]("Ram",20);

[Link]();

#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()
{
[Link]("Age is: "+age);
[Link]("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()
{
[Link]("enter roll no,name and marks");
}
}
class Ayag extends Student2
{
void show()
{
roll=12;
name="Ayush";
marks=90;
[Link](roll+" "+name+" "+marks);
}
}
public class Simpleinheritance
{
public static void main(String[] agrs)
{
Ayag s1=new Ayag();
[Link]();
[Link]();
}
}
#Output-
4. WAP to illustrate association in java programming.

// Class representing a Person


class Person {
private String name;

public Person(String name) {


[Link] = 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) {


[Link] = model;
[Link] = 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);

[Link]([Link]().getName() + " owns a " + [Link]());


}
}
#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=[Link](radius);//code reusability (i.e. delegates the method call).
return pi*rsquare;
}

public static void main(String args[]){


Aggregationforfile c=new Aggregationforfile();
double result=[Link](5);
[Link](result);
}
}
#Output-
6. WAP to illustrate inheritance(“Part of” relationship) in java programming.
class SolarSystem {
void abc()
{
[Link]("8 planets");
}
}
class Earth extends SolarSystem {
void abd()
{
[Link]("1 moon");
}
}
class Mars extends SolarSystem {
void abe()
{
[Link]("2 moons");
}
}
class Moon extends Earth
{
void abf()
{ [Link]("It is a natural satellite");
}
}
public class Inheri
{
public static void main(String args[])
{
Moon mo = new Moon();
Mars m = new Mars();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
#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;

[Link](name);
[Link](age);
[Link](salary);
}
}
// Base class
public class Base {
public static void main(String args[])
{
Sunstar s = new Employee();
[Link]();
}
}
#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) {
[Link]("The area of the rectangle is " + (length * breadth));
}
}

public class Main {


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

Common questions

Powered by AI

Aggregation is preferred over inheritance when two classes share a 'has a' relationship rather than a 'is a' relationship, promoting greater modularity and loose coupling. For instance, if class `Operation` is used to perform calculations (as seen in `Aggregationforfile`), and is not needed to form a subclass, aggregation allows it to be a part of another entity without a strictly hierarchical link. This ensures that changes in one class (Operation) do not directly impact the other (Aggregationforfile), promoting flexible system architecture and reducing dependencies .

Static methods belong to the class rather than any instance of the class and can be called without creating an object of the class. They can access static data members and can modify them. Instance methods require an object of the class to be created before they can be called and can access both static and instance data members. In the `StaticExample`, the `display()` method is static, meaning it can be invoked using the class name and it utilizes static variables `age` and `name` .

The primary difference between an interface and an abstract class in Java lies in their use cases and structural capabilities. Interfaces are implemented to provide full abstraction and multiple inheritance, allowing a class to implement multiple interfaces. They exclusively contain abstract methods until Java 8 introduced default and static methods. Abstract classes, on the other hand, can have both abstract and non-abstract methods, allowing shared implementation across subclasses. Interfaces lack state or instance variables, while abstract classes can maintain a state, exemplified by the `Polygon` interface versus the `Sunstar` abstract class .

A constructor in Java is a special method that is called when an object is instantiated. It is primarily used to initialize the newly created object and allocate resources needed for the object. For example, in the provided class `Student`, constructors are used to perform these initializations. The parameterized constructor `Student(int asd)` initializes objects with an integer, while the default constructor prints "constructor call" when an object is created without parameters .

Association is a relationship between objects where one object uses or is connected to another. It helps model real-world relationships by allowing objects to have references to other objects as attributes. For instance, in the given example, a `Car` has an `owner` of type `Person`. This non-intrusive association allows both `Car` and `Person` to function as independent entities while maintaining a flexible relationship, accurately modeling real-world scenarios where an owner's information needs to be linked with their car data .

Inheritance in Java allows a new class to inherit properties and methods from an existing class, promoting code reuse and establishing a hierarchy between classes. The `Ayag` class extends `Student2`, meaning `Ayag` is a subtype of `Student2`, and hence, it reuses `Student2's` properties and methods. This 'is a' relationship suggests that any `Ayag` instance is also a `Student2`, facilitating effective code reuse and making the system easily extensible .

Java implements polymorphism through method overloading, where multiple methods have the same name but differ in parameter types, number, or both. This allows the same method name to be used to perform different functions. In the `Student1` class, the `printdet` method is overloaded with different parameter signatures: a single `String`, a single `int`, and a combination of `String` and `int`. When invoked, the JVM determines which method to call based on the arguments provided, exemplifying compile-time polymorphism .

Abstract classes in Java provide a way to achieve abstraction by allowing methods to be declared without implementations, forcing subclasses to define those methods. They enable developers to define a common protocol for a family of subclasses while sharing implemented features. In the example, the `Sunstar` class contains an abstract method `printInfo`, which must be implemented by any subclass like `Employee`. This requirement ensures adherence to a particular contract and facilitates the creation of more adaptable and scalable software designs .

Encapsulation is crucial in object-oriented programming as it protects an object's state and behavior, making a system more secure and easier to maintain and understand. In Java, encapsulation involves using private data fields and public getters and setters to control access levels. This ensures data integrity by preventing unauthorized access and modification, thereby reducing system brittleness. Furthermore, encapsulation helps in achieving a clear separation of an object's interface from its implementation, which promotes modular design and code reusability [Not explicitly in sources but inferred through programming knowledge].

Method overriding allows runtime polymorphism in Java by enabling a subclass to provide a specific implementation of a method already defined in its superclass. This allows a subclass to define its behavior for inherited methods, which is determined at runtime by the object instance type, not the reference type, providing dynamic method dispatch. For example, while not directly illustrated, in a polymorphic setup, calling an overridden method on a subclass object via a superclass reference invokes the subclass's version. This facilitates flexible and extensible program designs by allowing methods to be redefined without changing client code [Implied from general Java concepts].

You might also like