Opp TH

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Suppose Human and Man are two classes where Man extends the class

Human.Label the classification of the feature by identifying the relationship


between the classes.

class Human {
String name;
int age;
void speak() {
System.out.println("Speaking...");
}
}
class Man extends Human {
void shave() {
System.out.println("Shaving...");
}
}

public class Main {


public static void main(String[] args) {
// Create an instance of the subclass
Man man = new Man();
man.name = "John";
man.age = 30;

man.speak(); // Method from Human class


man.shave(); // Method from Man class
}
}

Suppose there are two classes: Grand-Father and Grand-Mother.Grand-Child class


inherit both Grand-Father class and Grand-Mother class.Determine the
classification of inheritance in this scenario showing that is possible or not.

interface GrandFather {
void showGrandFather();
}interface GrandMother {
void showGrandMother();
}
public void showGrandFather() {
System.out.println("This is Grand-Father.");
}
public void showGrandMother() {
System.out.println("This is Grand-Mother.");
}
public void showGrandChild() {
System.out.println("This is Grand-Child.");
}
public static void main(String[] args) {
GrandChild gc = new GrandChild();
gc.showGrandFather();
gc.showGrandMother();
gc.showGrandChild();
}
}
Read the following code carefully to answer the question:
interface Assignment

{
void marks();
}
class Final_Assignment
{

}
Illustrate the difference between the interface and class.
Construct a relationship between Assignment and Final_Assignment to override the mentioned
method.

Difference between an Interface and a Class


Interface:
Abstract Type: An interface is an abstract type used to specify a behavior that classes must
implement.
Methods: Methods in an interface are abstract by default (they do not have a body).
Multiple Inheritance: A class can implement multiple interfaces, allowing multiple inheritance
of type.
Fields: Fields in an interface are implicitly public, static, and final.
Class:

Concrete Type: A class is a blueprint from which objects are created.


Methods: Methods in a class can have implementations (method bodies).
Single Inheritance: A class can inherit from one superclass only (single inheritance).
Fields: Fields in a class can have any access modifier (public, protected, private) and can be
instance variables or static variables.
Constructing a Relationship Between Assignment and Final_Assignment
To construct a relationship where Final_Assignment implements the Assignment interface and
overrides the marks method, you can modify Final_Assignment to implement the Assignment
interface.
Code Example.
interface Assignment {
void marks();
}

class Final_Assignment implements Assignment {


public void marks() {
System.out.println("Final assignment marks calculated.");
}

public static void main(String[] args) {


Final_Assignment finalAssignment = new Final_Assignment();

finalAssignment.marks();
}
}

Read the following code carefully to answer the question:

class A{
try{
String a=null;
a=”Hi”;
}
catch(Exception e)
{
System.out.println(e);
}
}
Recall the hierarchy of the mechanism with examples by examining the above code.

class A {
public A() {
try {
String a = null;
a = "Hi"; } catch (Exception e) {
System.out.println(e);
}
}

public static void main(String[] args) {


new A();
}
}

Read the following code carefully to answer the question:

abstract class Assessment


{ abstract void show();
public void assignment()
{
System.out.println(“This is my assignment”);
}
}
Compare between show() and assignment() method.
show() Method:
Abstract Method: The show() method is declared as abstract. This means:

It does not have a body; only the method signature is provided.


It must be overridden and implemented by any non-abstract subclass of Assessment.
The presence of an abstract method makes the class itself abstract, meaning Assessment
cannot be instantiated directly.
assignment() Method:
Concrete Method: The assignment() method is a concrete method, meaning:

It has a body with an implementation.


It provides specific functionality that subclasses inherit and can use directly.
Implementation: The assignment() method includes code that will be executed when the
method is called. In this case, it prints a message to the console.

1. Construct a java program with the following instruction:


Create a class named after Class Test with a private attribute marks.
Write a setter and a getter method to set and get the attribute.
Create an object ct1 and set the attribute values accordingly 10,20and display the value.

public class Test {

private int marks;

public void setMarks(int marks) {


this.marks = marks;
} public int getMarks() {
return marks;
}
public static void main(String[] args) {
Test ct1 = new Test();
ct1.setMarks(10);
System.out.println("Marks after setting to 10: " + ct1.getMarks());

ct1.setMarks(20);
System.out.println("Marks after setting to 20: " + ct1.getMarks());
}
}

You might also like