logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

Java is an object oriented language and some concepts may be new.

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

1. Overview:

Java is an object oriented language and some concepts may be new. Take breaks when needed, and go over the examples as many times as needed.

1.1. Encapsulation

Encapsulation is one of the four fundamental OOP Concepts. The other three are inheritance, polymorphism and abstraction. Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.

To achieve encapsulation in Java –

Declare the variables of a class as private.

Provide public setter and getter methods to modify and view the variables values.

1.2. Encapsulation Program Example

//File name EncapExample

public class EncapExample { private String name; private int age;

 

public int getAge() { return age;

}

public String getName() { return name;

}

public void setAge( int newAge) { age = newAge;

}

public void setName(String newName) { name = newName;

}

}

//File Name MainClass public class MainClass{

public static void main(String args[]) { EncapExample en = new EncapExample (); en.setName("Asim");

en.setAge(20);

System.out.print("Name: " + en.getName() + "Age: " + en.getAge());

}

}

 1.3. Inheritance

Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.

The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class). extends Keyword extends is the keyword used to inherit the properties of a class.

Following is the syntax of extends keyword.

class Super {

.....

.....

}

class Sub extends Super {

.....

.....

}

 

1.4. Inheritance Program Example

 

class Vehicle {

protected String brand = "Ford"; public void honk() {

System.out.println("Tuut, tuut!");

}

}

 

class Car extends Vehicle {

private String modelName = "Mustang"; public static void main(String[] args) {

 

Car myCar = new Car();

 

myCar.honk();

System.out.println(myCar.brand + " " + myCar.modelName);

}

}

 

 

1.5. Polymorphism

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.

 

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class.

Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways. For example, think of a superclass called Animal that has a method called animalSound().

Subclasses of Animals could be Pigs, Cats, Dogs, Birds - And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.):

1.6. Polymorphism Program Example

public class Animal {

public void animalSound() { System.out.println("The animal makes a sound");

}

}

public class Pig extends Animal { public void animalSound() {

System.out.println("The pig says: wee wee");

}

}

class Dog extends Animal { public void animalSound() {

System.out.println("The dog says: bow wow");

}

}

class Main {

public static void main(String[] args) { Animal myAnimal = new Animal();

Animal myPig = new Pig(); Animal myDog = new Dog(); myAnimal.animalSound(); myPig.animalSound(); myDog.animalSound();

}

}

1.7. Abstract Classes and Methods

Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces (which you will learn more about in the next section).

The abstract keyword is a non-access modifier, used for classes and methods:

 

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).

Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

An abstract class can have both abstract and regular methods:

1.8. Abstract Program Example

abstract class Animal {

// Abstract method (does not have a body) public abstract void animalSound();

// Regular method public void sleep() {

System.out.println("Zzz");

}

}

 

class Pig extends Animal { public void animalSound() {

System.out.println("The pig says: wee wee");

}

}

 

public class Main {

public static void main(String[] args) {

Pig myPig = new Pig(); // Create a Pig object myPig.animalSound();

myPig.sleep();

}

}

1.9. Interface Program Example

An interface is a completely "abstract class" that is used to group related methods with empty bodies:

interface Animal { public void animalSound public void run();

}

To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends). The body of the interface method is provided by the "implement" class:

 

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Atharva PatilComputer science

668 Answers

Hire Me
expert
Chrisantus MakokhaComputer science

586 Answers

Hire Me
expert
AyooluwaEducation

642 Answers

Hire Me
expert
RIZWANAMathematics

754 Answers

Hire Me

Get Free Quote!

430 Experts Online