OOPS in Java
Object Oriented Programming
Object-oriented programming has several advantages over procedural programming:
- OOP is faster and easier to execute
- OOP provides a clear structure for the programs
- OOP makes it possible to create full reusable applications with less code and shorter development time
What are Classes and Objects?
Classes and objects are the two main aspects of object-oriented programming.
class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the variables and methods from the class.
Create an Object
In Java, an object is created from a class. We have already created the class named
Main,
so now we can use this to create objects.
To create an object of Main,
specify the class name, followed by the object name, and use the keyword new:
Example
public class Main {
int x = 5;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
Java Constructors
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributesExample
public class Main {
int modelYear;
String modelName;
public Main(int year, String name) {
modelYear = year;
modelName = name;
}
public static void main(String[] args) {
Main myCar = new Main(1969, "Mustang");
System.out.println(myCar.modelYear + " " + myCar.modelName);
}
}Java Inheritance (Subclass and Superclass)
In Java, it is possible to inherit attributes and methods from one class to another.We group the "inheritance concept" into two categories:
- subclass (child) - the class that inherits from another class
- superclass (parent) - the class being inherited from
extends
keyword.PARENT CLASS :
package com.ddjkj.ydh;
public class parent {
public int adder(int x, int y) {
int z = x + y;
return z;
}
public int subtract(int x , int y){
int z = x-y;
return z;
}
public int multi(int x,int y){
int z = x*y;
return z;
}
public float div(int x, int y){
float z = x/y;
return z;
}
}
CHILD CLASS :
package com.ddjkj.ydh;
import java.util.Scanner;
public class child extends parent{
public static void main(String[] args) {
child i = new child();
child j = new child();
child k = new child();
child l = new child();
com.ddjkj.ydh.efsgnh obj = new com.ddjkj.ydh.efsgnh();
int n1,n2;
System.out.println("enter n1");
Scanner num1 = new Scanner(System.in);
int a = num1.nextInt();
System.out.println("enter n2");
Scanner num2 = new Scanner(System.in);
int b = num2.nextInt();
int res1 = i.adder(a,b);
int res2 = j.subtract(a,b);
int res3 = k.multi(a,b);
float res4 = l.div(a,b);
System.out.println(res1);
System.out.println(res2);
System.out.println(res3);
System.out.println(res4);
}
}
Java Polymorphism :
Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
Polymorphism uses inheritance methods to perform different tasks. This allows us to perform a single action in different ways.
Example
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
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(); // Create a Animal object
Animal myPig = new Pig(); // Create a Pig object
Animal myDog = new Dog(); // Create a Dog object
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
Comments
Post a Comment